Search code examples
phppluginsimpresspages

Plugin Submenu Admin


How do you create Submenu items for a plugin.

I've checked System Module, That have Submenu class, and Filter on Admin. Is this possible without hacking or Overriding class.

Thanks.


Solution

  • Sure you can.

    Layout response object has method 'setLayoutVariable'. One of accepted variables is 'submenu'. If you supply an array of Ip\Menu\Item objects, a submenu will be generated. Here is an example. Put it in Plugin\Application\Event.php file:

    <?php
    
    namespace Plugin\Application;
    
    
    class Event
    {
        public static function ipBeforeController()
        {
    
            if (true) { //find out some kind of a condition when you want a submenu. (eg. ipRequest()->getControllerClass() == 'Plugin\XXX\AdminController')
                $submenu = array();
                $submenuItem = new \Ip\Menu\Item();
                $submenuItem->setTitle('Test');
                $submenuItem->setUrl('http://www.example.com');
                $submenu[] = $submenuItem;
    
                if (is_object(ipResponse()) && method_exists(ipResponse(), 'setLayoutVariable')) {
                    ipResponse()->setLayoutVariable('submenu', $submenu);
                }
    
            }
        }
    
    }
    

    You can filter main menu too using filter. Add following code to Plugin\Application\Filter.php file:

    <?php
    
    namespace Plugin\Application;
    
    
    class Filter {
        /**
         * @param \Ip\Menu\Item[] $menu
         * @param $info
         */
        public static function ipAdminMenu ($menu, $info)
        {
            //modify $menu
    
            return $menu;
        }
    }