cesurapp / pd-menu

Simple fast object-oriented menu maker for Symfony 5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow setting menu defaults

timwhite opened this issue · comments

It's great being able to set Link and List attributes on individual items, however it would be great if we could set defaults for them.
The classes I need on the links and lists are different to you, and currently I need to set them on each menu item. Either being able to set defaults at render time, or at menu build time would be good. (And if you already can, improving the docs would be good)

        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            ;

Could either become

        $menu->setDefaultListAttr(['class' => 'nav-item'])
            ->setDefaultLinkAttr(['class' => 'nav-link'])
        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ;

Or It could become

        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ;

and at render time

{{ pd_menu_render('App\\Menu\\FirstMenu', {
    'defaultLinkAttr': {'class': 'nav-link'},
    'defaultListAttr': {'class': 'nav-item'}
}) }}

To further add to this. If Item was able to be extended, then we could set defaults. But all the "defaults" are private variables. If they are set to protected variables then they could be overridden in a class that extends Item and used to set the defaults.