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

Easy Divider in KNP Menu #323

Open
leroy0211 opened this issue Nov 14, 2014 · 3 comments
Open

Easy Divider in KNP Menu #323

leroy0211 opened this issue Nov 14, 2014 · 3 comments

Comments

@leroy0211
Copy link

When using dividers you need to use unique divider names as child-elements.

$menu['Settings']->addChild('d1')->setAttribute('divider', true);
$menu['Settings']->addChild('d2')->setAttribute('divider', true);
etc...

Would it also be posible to set an attribute to add a divider to an existing normal child-element, like this:

$menu['Settings']->addChild('Data Management', array('uri' => '#')->setAttribute('divider_append', true);

Or

$menu['Settings']->addChild('Data Management', array('uri' => '#')->setAttribute('divider_prepend', true);
@florianeckerstorfer
Copy link
Member

Dividers are child elements, because that semantically best matches how dividers are represented in the HTML by Bootstrap. Of course it would be possible to add dividers to items, but I don't really see the use. It would be a little bit less code, but logically it is nicer to have them as separate items.

Look at this simplified example:

$menu->addChild('item1');
$menu->addChild('item2');
$menu->addChild('divider', ['divider' => true]);
$menu->addChild('item3');
$menu->addChild('item3');

matches nicely the generated HTML

<li>item 1</li>
<li>item 2</li>
<li class="divider"></li>
<li>item 3</li>
<li>item 4</li>

@leroy0211
Copy link
Author

It would be more "human" logic to put those in de code directly, but those dividers still need to be uniquely named.
In fact, I am trying to create a little bit of configuration for the users in a user-backend, so they can choose if there must be a divider present or not. In this case I need to write more code to increment that number (even if it's just in a for-loop)

Sure, it is possible to auto-increment the divider-nr. but I think it's really easier just to code in a true or false on the append or prepend option.

@soullivaneuh
Copy link

You can also create your own DividerMenuItem with default stuff:

declare(strict_types = 1);

namespace AppBundle\Menu\Item;

use Knp\Menu\FactoryInterface;
use Knp\Menu\MenuItem;

final class DividerMenuItem extends MenuItem
{
    /**
     * @param FactoryInterface $factory
     */
    public function __construct(FactoryInterface $factory)
    {
        parent::__construct('divider', $factory);

        $this
            ->setExtra('divider', true)
            ->setAttribute('class', 'separator')
        ;
    }
}

Then, on your builder:

$menu->addChild(new DividerMenuItem($this->factory));

And on my personal theme override:

{% block spanElement %}
    {% import 'knp_menu.html.twig' as knp_menu %}
    {% if not item.extra('divider') %} {% Don't show menu content if it's a divider %}
        <span>{{ knp_menu.attributes(item.labelAttributes) }}>
            {% if item.extra('icon') is not empty  %}
                <i class="{{ item.extra('icon') }}"></i>
            {% endif %}
            {{ block('label') }}
        </span>
    {% endif %}
{% endblock %}

Not directly linked to this bundle this might help.

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

3 participants