This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

keevitaja8117
keevitaja8117

Hi,

how can i access static menu with php? What is the variable name? From the twig i can do it with

{% for item in staticMenu.menuItems %}

but there is no $staticMenu variable

Crazymodder
Crazymodder

Hi,

Did you try with $this? Maybe you should dump it out to see whats inside;)

Regards Crazymodder

jan-vince
jan-vince

I used this:

Access by component name:

function onStart(){
    $primaryMenuItems = $this->controller->findComponentByName('primaryMenu')->menuItems();    
    $secodaryMenuItems = $this->controller->findComponentByName('secondaryMenu')->menuItems();        
}

Access by variable:
works only in onEnd() method, not sooner

function onEnd(){
    $secondaryMenuItems = $this->menuItems;    // Outputs items of last added StaticMenu component !
}
chocolata
chocolata

Hi guys, quick question. In the meta / menu's folder, there is a yaml file representing each menu. In this file, there is a mention of "type" for static pages for examples. In the partial that outputs my menu, I'd like to test for this variable. Unfortunately, the menu variable in twig doesn't contain this variable. Also, the above methods also don't contain this variable. Does anyone have an idea on how to test for this?

chocolata
chocolata

I was a bit too quick. I've found a way to do it. Below is my way to get the translated titles of the static pages in the menus.

function onStart() {
    $theme = Cms\Classes\Theme::getEditTheme();
    $menuList = RainLab\Pages\Classes\Menu::loadCached($theme, 'primary');
    $translatedMenuList = [];
    $count = 0;
    foreach($menuList->items as $myItem) {
        if($myItem->type == 'static-page') {
            $myStaticPage = RainLab\Pages\Classes\Page::load($theme, $myItem->reference.'.htm');
            array_push($translatedMenuList,['title'=>$myStaticPage['viewBag']['title']]);
        } else {
            array_push($translatedMenuList,['title'=>'']);
        }
        $count++;
    }
    $this['translatedMenuList'] = $translatedMenuList;
}

Afterwards, in twig, I do something like this:

<ul class="navigation">
{% for item in items if not item.viewBag.isHidden %}
    <li>{% if translatedMenuList[loop.index0].title %}{{ translatedMenuList[loop.index0].title }}{% else %}{{ item.title }}{% endif %}</li>
{% endfor %}
</ul>

The above twig is a simplified example. I thought I'd share it for anyone who'd find it useful...

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.