1

This is a follow up from How to remove Link To Category from Redactor.

I've been able to setup the module and remove all links with this:

Event::on(
  RedactorField::class,
  RedactorField::EVENT_REGISTER_LINK_OPTIONS,
  function(RegisterLinkOptionsEvent $event) {

          $event->linkOptions = [];
  }
);

Which removes all Craft links, but how do I remove the categories links only?

1 Answer 1

2

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));
    }
);
1
  • That works, and learned a bit more about setting op a Module, thanks!
    – noregt
    Mar 15, 2021 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.