Support for parent-child (drop down) menu structure

Hi,

It would be nice if pages generated while creating blank YII application supports a parent-child (drop down) menu structure.

Default menu can be kept as it is. If users want a parent-child menu structure, it is better if users can extend it.

For example menu items declared in protected/views/layouts/main.php can be created like this.




<div id="mainmenu"  align="center">

<?php $this->widget('application.components.MainMenu',array(

	'items'=>array(

		array('label'=>'Parent menu', 'url'=>array('/site/index'),'id'=>'idParent'),

		array('label'=>'Child menu', 'url'=>array('/site/contact'), 'parent'=>'idParent','id'=>'idChild'),

		array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest,'id'=>'idLogin'),

		array('label'=>'Logout', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest,'id'=>'idLogout')

	),[b]'menuView'=>'dropDownMenu'[/b]

)); ?>

</div><!-- mainmenu -->



Here two new elements are added to menu item array called parent and id.

Id is a unique identifier for menu item and parent contains the id of parent menu item.

We can also introduce another parameter to array called ‘menuView’=>‘dropDownMenu’. This will allow user to set a different view other than mainMenu view used in protected/components/MainMenu.php.

Finally we can modify protected/components/MainMenu.php as this.




        public $items=array();

        public $menuView = 'mainMenu';//New variable to hold menu view


	public function run()

	{

		$items=array();

		$controller=$this->controller;

		$action=$controller->action;

		foreach($this->items as $item)

		{

			if(isset($item['visible']) && !$item['visible'])

				continue;

			$item2=array();

			$item2['label']=$item['label'];

         

                        //Sets id and parent

                        $item2['id']=$item['id'];


                        if (isset($item['parent']))

                        {

                              $item2['parent']=$item['parent'];

                        }


			if(is_array($item['url']))

				$item2['url']=$controller->createUrl($item['url'][0],array_splice($item['url'],1));

			else

				$item2['url']=$item['url'];

			$pattern=isset($item['pattern'])?$item['pattern']:$item['url'];

			$item2['active']=$this->isActive($pattern,$controller->uniqueID,$action->id);

			$items[]=$item2;

		}

                //Set view set by user

		$this->render($this->menuView,array('items'=>$items));

	}



Regards,

Chamal.

We do not plan on modifying the menu widget that yiic generates for you. However, a separate menu widget is currently being developed for the Zii extension library which has many more features, as well as nested items.