Render custom attribute KNP Menu

5.2k views Asked by At

Is there a way to render a custom attribute in the KNP Menu Bundle, something like this:

$menu = $factory->createItem(Role::ROLE_PROGRAM_EVENT_PLANNER, array(
    'route' => 'show_form_events',
    'attributes' => array('class' => 'menu pe_planner'),
    'extra' => array(
      'content' => 'my custom content'
    )
));

I have overriden the linkElement by adding an extra div after the a-tag. In that div I would like to render extra content

{% block linkElement %}
    {% import _self as knp_menu %}
    <a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>{{ block('label') }}</a>
    {% if item.hasChildren == false %}
        <div class="custom">{{ item.getExtra('content') }}</div>
    {% endif %}
{% endblock %}
1

There are 1 answers

0
nixoschu On

Actually i had to do quite the same for today ;)

MenuBuilder

$menu->addChild(
  'Dashboard',
  array(
    'route'      => 'dashboard',
    'attributes' => array(
      'class' => 'navigation-entry'
    ),
    'extras' => array(
      'icon' => '6'
    )
  )
);

menuTemplate

{% block linkElement %}
  {% import "knp_menu.html.twig" as macros %}    
  <a href="{{ item.uri }}"{{ macros.attributes(item.linkAttributes) }}>
    <span class="icon">{{ item.getExtra('icon') }}</span>
    <span class="entry">{{ block('label') }}</span>
  </a>
{% endblock %}

Dont be confused be the icon content because i use an icon font.