Yii2 Menu widget disable parent item link

578 Views Asked by At

How can I disable only the parent item link and leave its children clickable ? For example - when I click on my parent it should open the submenu and not redirect to somewhere else. And if I click on children it should redirect me. P.S I red the documentation, searched around google but nothing comes out. This is my code:

<?php

use backend\models\PageAdmin;
use backend\helpers\BackendPrefix;
use yii\widgets\Menu;

$menu_pages = PageAdmin::find()->where('active=1 AND id_in IS NULL')->all();

$widget_menu_content = [];

foreach ($menu_pages as $key => $page){

    $related_pages = PageAdmin::find()->where(['active' => 1, 'id_in' => $page->id])->all();
    $widget_menu_content[$key]['label'] = "<i class='{$page->icon}'></i>";
    $widget_menu_content[$key]['icon'] = $page->icon;
    $widget_menu_content[$key]['url'] = BackendPrefix::PREFIX .'/'. $page->url;

    if(!empty($related_pages)){

        foreach ($related_pages as $key2 => $rel_page) {

            $widget_menu_content[$key]['items'][$key2]['label'] = $rel_page->title;
            $widget_menu_content[$key]['items'][$key2]['icon'] = "caret-right";
            $widget_menu_content[$key]['items'][$key2]['url'] = BackendPrefix::PREFIX . "/" . $rel_page->url;

        }

    }
}

?>

<div class="side-mini-panel">
    <?=

        Menu::widget([
            'encodeLabels' => false,
            'options' => [
                'class' => 'mini-nav',
            ],
            'activeCssClass' => 'selected',
            'items' => $widget_menu_content,
            'submenuTemplate' => "
                                \n<div class='sidebarmenu'>
                                    \n<h3 class='menu-title'>{label}</h3>
                                    \n<div class='searchable-menu'>
                                    \n<form role='search' class='menu-search'>
                                        \n<input type='text' placeholder='Search...' class='form-control' />
                                        \n<a href='javascript:void(0)'><i class='fa fa-search'></i></a>
                                    \n</form>
                                    \n<div>
                                    \n<ul class='sidebar-menu'>
                                        \n{items}
                                    \n</ul>
                                \n</div>",
            'linkTemplate' => "<a href='javascript:void(0)'>{label}</a>",
            'activateParents' => true,
        ])

    ?>
</div

Whit my 'linkTemplate' I am disabling all the links which obviously is not my goal :) ( It should disable the parents only ). Alsom, with what token ( or some other option, because I think items is the only token for the children ) I should append the child item label on this line : \n<h3 class='menu-title'>{label}</h3>. In my case what I get echoed is just {label} like a string. Many thanks!

0

There are 0 best solutions below