1

I need to add some custom condition to the top siteMenu selector.

Template file with top menu:

vendor/craftcms/cms/src/templates/_elements/sitemenu.twig

Block with this menu:

vendor/craftcms/cms/src/templates/_layouts/elementindex.twig

How can I extend elementindex.twig to use my own template from module or override sitemenu.twig without changing any of the vendor files?

1 Answer 1

0

Well, I don't see how this can be done easily(without changing vendor files), mainly because there is no CP template hook for such case.

But what you can try to do instead is:

  1. Register JS file(s) for the CP part of website and modify DOM elements as needed via JavaScript.
  2. Use EVENT_BEFORE_RENDER_TEMPLATE and modify variables, of course, if it'll be useful in your case

...

use yii\base\Event;

use craft\events\TemplateEvent;

use craft\web\View;

...


Event::on(
    View::class,
    View::EVENT_BEFORE_RENDER_TEMPLATE,
    function (TemplateEvent $event) {
        if ($event->templateMode === 'cp' && $event->template === '_layouts/_elements/sitemenu.twig') {
            $variables = $event->variables;
            $variables['groups'] = ...;
            $event->variables = $variables;
        }
        // ...
    }
);

Perhaps you can provide more context so the community can help you find a more elegant solution.

1
  • I need just remove all non-English sites from this list.
    – DuxaBilii
    Sep 28, 2023 at 19:03

Your Answer

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

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