luyadev / luya

LUYA is a scalable web framework and content management system with the goal to please developers, clients and users alike.
https://luya.io
MIT License
812 stars 207 forks source link

Multi Level Menu in LUYA using Bootstrap 4 #1906

Closed testt23 closed 5 years ago

testt23 commented 5 years ago

I need help in my code as i am unable to create multi-level menu. Here is my code.


<?php foreach (Yii::$app->menu->findAll(['depth' => 1, 'container' => 'default']) as $item): /* @var $item \luya\cms\menu\Item */ ?>
            <li class="nav-item <?= $item->isActive ? ' active' : '' ?> <?= $item->hasChildren ? ' dropdown' : '' ?>">
                <a class="nav-link <?= $item->hasChildren ? 'dropdown-toggle' : '' ?>" href="<?= $item->link; ?>"  <?= $item->hasChildren ? ' id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : '' ?> title="<?= $item->title; ?>"><?= $item->title; ?></a><?php
                if ($item->hasChildren) {
                    echo '<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">';
                    foreach ($item->children as $subChild) {
                        echo '<li class="dropdown-item"><a href="'.$subChild->link.'" title="'.$subChild->title.'">'.$subChild->title.'</a></li>';
                    }
                    echo '</ul>';
                }
                ?>
            </li>
            <?php endforeach; ?>

I want it to be the three level menu but currently it is showing only the level 2 menu.

example: menu 1 menu 2 menu 3 -- menu 3.1 -- menu 3.2 ----- menu 3.2.1 ----- menu 3.2.2 ----- menu 3.3.3 --------- menu 3.3.3.1 --------- menu 3.3.3.2 menu 4 menu 5 -- menu 5.1 -- menu 5.2 ---- menu 5.2.1

luya-bot commented 5 years ago

Thank you for your question. In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

This is an automated comment, triggered by adding the label question.

nadar commented 5 years ago

You can iterate trough every children by accessing ->getChildren() in the item - its what your example actually does. Regarding bootstrap see bootstrap 4 documentations.

foreach($item->getChildren() as $sub) {
    foreach($sub->getChildren() as $subSub) {
      // etc.
    }
}
testt23 commented 5 years ago

I don't agree that this is the solution. on this logic if we have 10 sub menus we need 10 times to call foreach..

We need to find another solution together.

JohnnyMcWeed commented 5 years ago

You can do something like this...

<?php
namespace app\helpers;

use Yii;
use luya\cms\menu\Item;

class MenuTreeHelper
{
    public static function getTreeByNavItem(Item $item) {
        $arr[] = $item;
        foreach (Yii::$app->menu->getLevelContainer($item->depth+1, $item) as $it) {
            $arr = array_merge($arr, MenuTreeHelper::getTreeByNavItem($it));
        }
        return $arr;
    }

    public static function getTreeByContainer($container) {
        $arr=[];
        foreach (Yii::$app->menu->findAll(['depth' => 1, 'container' => $container]) as $item):
            $arr = array_merge($arr, MenuTreeHelper::getTreeByNavItem($item));
        endforeach;
        return $arr;
    }
}

Like this you get an array by container: app\helpers\MenuTreeHelper::getTreeByContainer('default')

nadar commented 5 years ago

We could make helper methods for navigations/menu directly into luya cms.

BlockHelper::a($item, ['activeClass' => 'is_active']);

BlockHelper::list(Yii::$app->menu->find(), ['depth' => 3]);