October CMS resources and help articles

Simple and to the point. Optimized by the community.

Multiple active menu items in Rainlab.Pages

1
by rizhenkov, last modified on November 28th, 2019

Rainlab.Pages has an official way to set menu item as active:

[viewBag]
activeMenuItem = "news"

But some designs require multiple active menu items. By default you can define active items with the code news or news-side but you can't set both active.

With this trick you can do this.

In your plugins boot section define event listener:

    public function boot()
    {
        Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) {
            if($type == 'cms-page'){
                $resolved = \Cms\Classes\Page::resolveMenuItem($item, $url, $theme);

                $currentPO = \Cms\Classes\Controller::getController()->getPageObject();
                if($currentPO && $currentPO->page && $currentPO->page->viewBag
                    && isset($currentPO->page->viewBag['activeMenuItems'])){

                    $arr = explode(',', $currentPO->page->viewBag['activeMenuItems']);
                    if(in_array($item->code, $arr)){
                        $resolved['isActive'] = true;
                    }
                }

                return $resolved;
            }
        });
    }

You are now able to define mulitple active menu items:

[viewBag]
activeMenuItems = "news,news-side"

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?