Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu with subitems didn't show up on Bootstrap5 #474

Open
numitec opened this issue Oct 27, 2022 · 1 comment
Open

Menu with subitems didn't show up on Bootstrap5 #474

numitec opened this issue Oct 27, 2022 · 1 comment

Comments

@numitec
Copy link

numitec commented Oct 27, 2022

Hello there,

I installed a vanilla Yii2 advanced template and then follow this guideline: https://www.yiiframework.com/wiki/848/installation-guide-yii-2-advanced-template-with-rbac-system to implement yii2-admin.

But when I try to show the menu, the options with sub items don't show at all: I can only click on the top item.

I instantiate the menu using:

use yii\bootstrap5\Breadcrumbs;
use yii\bootstrap5\Html;
use yii\bootstrap5\Nav;
use yii\bootstrap5\NavBar;

use mdm\admin\components\MenuHelper;

[...]

$menuItems = MenuHelper::getAssignedMenu(Yii::$app->user->id);

		echo Nav::widget([
			'options' => ['class' => 'navbar-nav ml-auto'], // navbar-right'],
			'items' => $menuItems,
			'encodeLabels' => False,
		]);

Any clue?

Thank you!

S.

@numitec
Copy link
Author

numitec commented Nov 3, 2022

In order to the reach new requirements present at BS5 (kindly see /yiisoft/yii2-bootstrap5/src/Nav.php):

 * Note: Multilevel dropdowns beyond Level 1 are not supported in Bootstrap 5.
 *
 * @see https://getbootstrap.com/docs/5.1/components/navs/
 * @see https://getbootstrap.com/docs/5.1/components/dropdowns/
 *
 * @author Antonio Ramirez <[email protected]>

I needed to change the static method normalizeMenu() on the helper: mdmsoft/yii2-admin/components/MenuHelper.php; here it runs with no subitems:

    /**
     * Normalize menu
     * @param  array $assigned
     * @param  array $menus
     * @param  Closure $callback
     * @param  integer $parent
	 * @param  boolean $root
     * @return array
     */
	private static function normalizeMenu( &$assigned, &$menus, $callback, $parent_id = Null, $root = True )
	{
		$children = [];
		$subitem = [];
        $result = [];
        $order = [];
		$item = [];

		// Recorre todas las opciones de menú asignadas al rol
        foreach( $assigned as $id )
		{
            $menu = $menus[$id];

			// Obtiene todas las opciones en el nivel vigente
            if( $menu['parent_id'] == $parent_id )
			{
				// Recupera todos los subitems
				$children = static::normalizeMenu( $assigned, $menus, $callback, $id, False );

				// Procesa los items del nivel vigente y anexa sus subitems
                if( !is_null( $callback ) )
                    $item = call_user_func( $callback, $menu, $root );
                else
				{
					// Only root levels should have items
					if( $root )
					{
						$item = [
							'label' => $menu[ 'name' ],
							'url' => static::parseRoute( $menu['route'] ),
						];

						foreach( $children as $child )
							$item['items'][] = $child;
					}
					else
					{
						if( is_null( $menu['route'] ) )
						{
							$subitem = [
								'<div class="dropdown-divider"></div>',
								'<div class="dropdown-header">'. $menu['name'] . '</div>',
							];

							foreach( $children as $child )
								$subitem[] = $child;
						}
						else
							$item = [
								'label' => $menu[ 'name' ],
								'url' => static::parseRoute( $menu['route'] ),
								'icon' => $menu[ 'icon' ],
							];
					}
				}

				if( count( $item ) )
				{
					$result[] = $item;
					$order[] = $menu['order'];
					$item = [];
				}
				else if( count( $subitem ) )
					$result = array_merge( $result, $subitem );
			}
		}

		if( count( $result ) == count( $order ) )
			array_multisort( $order, $result );

		return $result;
	}

Hope it helps!

S.

@numitec numitec closed this as completed Nov 3, 2022
@numitec numitec reopened this Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant