Redactor: Option or hook to remove "Link to entry" from dropdown

Created on 12 Jun 2019  ·  3Comments  ·  Source: craftcms/redactor

It would be nice if we could remove the "Link to entry" option from the dropdown menu. Ditto for "Link to category".

Ω 2019-06-12 at 12 49 47 PM

I don't even care _how_ it's implemented (whether it's a field option, Redactor config, or requires a module hook). But it would be nice to have a bit more control over what users can link to.

Worth noting that this is not a new request...

https://craftcms.stackexchange.com/questions/17567/how-to-remove-link-to-category-from-redactor

Most helpful comment

Sweet, that worked like a charm!

use craft\redactor\events\RegisterLinkOptionsEvent;
use craft\redactor\Field as RedactorField;
use yii\base\Event;

// Remove "Link to..." Redactor links
Event::on(
    RedactorField::class,
    RedactorField::EVENT_REGISTER_LINK_OPTIONS,
    function(RegisterLinkOptionsEvent $event) {

        // Only apply to a specific Redactor config
        if ('Message.json' == $event->sender->redactorConfig) {

            // Remove Craft's injected links
            $event->linkOptions = [];

        }

    }
);

I used $event->sender->redactorConfig to identify the specific Redactor config which I wanted to target.

If you only want to remove _one specific source_, you can pick it out manually. For reference, here's how Craft configures those extra links...

[
    0 => [
        'optionTitle' => 'Link to an entry'
        'elementType' => 'craft\\elements\\Entry'
        'refHandle' => 'entry'
        'sources' => [
            0 => 'section:19870dc1-a51e-4134-bd5c-de55b11ddf2c'
            1 => 'section:eca834a6-d8a1-4797-a552-e310f946eafb'
            2 => 'section:128d6e4e-d02f-4247-83eb-a3841ca7ade5'
            3 => 'section:c422ed50-9844-4531-854f-f8c505d92a05'
        ]
    ]
    1 => [
        'optionTitle' => 'Link to a category'
        'elementType' => 'craft\\elements\\Category'
        'refHandle' => 'category'
        'sources' => [
            0 => 'group:95e58ebd-aba6-48b8-adaf-ebae28ffbb7a'
            1 => 'group:9bd9e19d-e53a-46eb-af47-2ce9a12a0ade'
        ]
    ]
]

Thanks @andris-sevcenko! 🍺

All 3 comments

Speaking of module hooks, you can already use the REGISTER_LINK_OPTIONS event and just remove some of the options from the $event->linkOptions array. (https://github.com/craftcms/redactor/blob/v2/src/Field.php#L563)

Sweet, that worked like a charm!

use craft\redactor\events\RegisterLinkOptionsEvent;
use craft\redactor\Field as RedactorField;
use yii\base\Event;

// Remove "Link to..." Redactor links
Event::on(
    RedactorField::class,
    RedactorField::EVENT_REGISTER_LINK_OPTIONS,
    function(RegisterLinkOptionsEvent $event) {

        // Only apply to a specific Redactor config
        if ('Message.json' == $event->sender->redactorConfig) {

            // Remove Craft's injected links
            $event->linkOptions = [];

        }

    }
);

I used $event->sender->redactorConfig to identify the specific Redactor config which I wanted to target.

If you only want to remove _one specific source_, you can pick it out manually. For reference, here's how Craft configures those extra links...

[
    0 => [
        'optionTitle' => 'Link to an entry'
        'elementType' => 'craft\\elements\\Entry'
        'refHandle' => 'entry'
        'sources' => [
            0 => 'section:19870dc1-a51e-4134-bd5c-de55b11ddf2c'
            1 => 'section:eca834a6-d8a1-4797-a552-e310f946eafb'
            2 => 'section:128d6e4e-d02f-4247-83eb-a3841ca7ade5'
            3 => 'section:c422ed50-9844-4531-854f-f8c505d92a05'
        ]
    ]
    1 => [
        'optionTitle' => 'Link to a category'
        'elementType' => 'craft\\elements\\Category'
        'refHandle' => 'category'
        'sources' => [
            0 => 'group:95e58ebd-aba6-48b8-adaf-ebae28ffbb7a'
            1 => 'group:9bd9e19d-e53a-46eb-af47-2ce9a12a0ade'
        ]
    ]
]

Thanks @andris-sevcenko! 🍺

Hello,

In the Link to an Entry, would you know on how to filter the section entries based on user's tagged category? I only want to see all entries that is tagged to that category.

Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cstudios-slovakia picture cstudios-slovakia  ·  6Comments

stenvdb picture stenvdb  ·  16Comments

sandissauka picture sandissauka  ·  16Comments

lindseydiloreto picture lindseydiloreto  ·  4Comments

lukeyouell picture lukeyouell  ·  26Comments