Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option or hook to remove "Link to entry" from dropdown #146

Closed
lindseydiloreto opened this issue Jun 12, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@lindseydiloreto
Copy link

commented Jun 12, 2019

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

@andris-sevcenko

This comment has been minimized.

Copy link
Member

commented Jun 13, 2019

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)

@lindseydiloreto

This comment has been minimized.

Copy link
Author

commented Jun 13, 2019

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! 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.