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 · 3 comments
Closed

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

lindseydiloreto opened this issue Jun 12, 2019 · 3 comments

Comments

@lindseydiloreto
Copy link
Contributor

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
Copy link
Contributor

andris-sevcenko 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
Copy link
Contributor Author

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

@dharnell-abluyen
Copy link

dharnell-abluyen commented Aug 28, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants