At the moment, your are overriding $event->linkOptions
which contains an array with all options to an empty array with $event->linkOptions = []
.
First, add the below to your module and refresh any page on your site so you see what you are working with:
Event::on(
RedactorField::class,
RedactorField::EVENT_REGISTER_LINK_OPTIONS,
function(RegisterLinkOptionsEvent $event) {
Craft::dd($event->linkOptions);
}
);
In the array you saw above, you want to remove the object containing a 'elementType' => 'craft\\elements\\Category'
.
You can do this by searching $event->linkOptions
for craft\\elements\\Category
, remove it but still returning the rest of the options.
Here's one way of doing so:
Event::on(
\craft\redactor\Field::class,
\craft\redactor\Field::EVENT_REGISTER_LINK_OPTIONS,
function(\craft\Redactor\events\RegisterLinkOptionsEvent $event) {
$defaultOptions = $event->linkOptions;
// Search for category element
$categoryLink = \craft\helpers\ArrayHelper::where($defaultOptions, 'elementType', 'craft\\elements\\Category');
// If present, remove the option and return the rest
$event->linkOptions = \craft\helpers\ArrayHelper::without($defaultOptions, key($categoryLink));
}
);