{# @see https://gist.github.com/lsv/24a9e9d7d961d22226f8e220d2c4a7d0 #} {% extends 'knp_menu.html.twig' %} {% macro setCssClassAttribute(item, type, add) %} {% set getter = 'get' ~ type %} {% set setter = 'set' ~ type %} {% set value = attribute(item, getter, ['class']) %} {% if value is iterable %} {% set value = value|join(' ') %} {% endif %} {% do attribute(item, setter, ['class', value ~ ' ' ~ add]) %} {% endmacro %} {% block item %} {% import "knp_menu.html.twig" as macros %} {# As multiple level is not currently supported by bootstrap 4 This requires you to install https://github.com/bootstrapthemesco/bootstrap-4-multi-dropdown-navbar And set the the use_multilevel = true #} {% set use_multilevel = false %} {% if item.displayed %} {%- set attributes = item.attributes %} {%- set is_dropdown = attributes.dropdown|default(false) %} {%- set divider_prepend = attributes.divider_prepend|default(false) %} {%- set divider_append = attributes.divider_append|default(false) %} {% if item.attribute('icon') is not empty %} {% set icon = item.attribute('icon') %} {% endif %} {# unset bootstrap specific attributes #} {%- set attributes = attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null,'icon': null}) %} {%- if divider_prepend %} {{ block('dividerElement') }} {%- endif %} {# building the class of the item #} {%- set classes = item.attribute('class') is not empty ? [item.attribute('class'), 'nav-item'] : ['nav-item'] %} {%- if matcher.isCurrent(item) %} {%- set classes = classes|merge([options.currentClass]) %} {%- elseif matcher.isAncestor(item, options.depth) %} {%- set classes = classes|merge([options.ancestorClass]) %} {%- endif %} {%- if item.actsLikeFirst %} {%- set classes = classes|merge([options.firstClass]) %} {%- endif %} {%- if item.actsLikeLast %} {%- set classes = classes|merge([options.lastClass]) %} {%- endif %} {# building the class of the children #} {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %} {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %} {# adding classes for dropdown #} {%- if is_dropdown %} {%- set classes = classes|merge(['dropdown']) %} {%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %} {%- endif %} {# putting classes together #} {%- if classes is not empty %} {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} {%- endif %} {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %} {# displaying the item #} {%- if is_dropdown %} {{ block('dropdownElement') }} {%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %} {{ block('linkElement') }} {%- else %} {{ block('spanElement') }} {%- endif %} {%- if divider_append %} {{ block('dividerElement') }} {%- endif %} {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %} {{ block('dropdownlinks') }} {% endif %} {% endif %} {% endblock %} {% block dropdownlinks %} {% if use_multilevel %} {% endif %} {% endblock %} {% block renderDropdownlink %} {% import _self as ownmacro %} {%- set divider_prepend = item.attributes.divider_prepend|default(false) %} {%- set divider_append = item.attributes.divider_append|default(false) %} {%- set attributes = item.attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %} {% if use_multilevel %}
  • {% endif %} {%- if divider_prepend %} {{ block('dividerElementDropdown') }} {%- endif %} {%- if item.uri is not empty and (not item.current or options.currentAsLink) %} {{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'dropdown-item') }} {{ block('linkElement') }} {%- else %} {{ block('spanElementDropdown') }} {%- endif %} {%- if divider_append %} {{ block('dividerElementDropdown') }} {%- endif %} {% if use_multilevel %}
  • {% endif %} {% endblock %} {% block spanElementDropdown %} {% import "knp_menu.html.twig" as macros %} {% import _self as ownmacro %} {{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'dropdown-header') }}
    {% if icon is defined %} {% endif %} {{ block('label') }}
    {% endblock %} {% block dividerElementDropdown %} {% endblock %} {% block dividerElement %} {% if item.level == 1 %}
  • {% else %}
  • {% endif %} {% endblock %} {% block linkElement %} {% import "knp_menu.html.twig" as macros %} {% import _self as ownmacro %} {{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'nav-link') }} {% if icon is defined %} {% endif %} {{ block('label') }} {% endblock %} {% block spanElement %} {% import "knp_menu.html.twig" as macros %} {% import _self as ownmacro %} {{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'navbar-text') }} {% if icon is defined %} {% endif %} {{ block('label') }} {% endblock %} {% block dropdownElement %} {% import "knp_menu.html.twig" as macros %} {%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %} {%- set classes = classes|merge(['dropdown-toggle', 'nav-link']) %} {%- set attributes = item.linkAttributes %} {%- set attributes = attributes|merge({'class': classes|join(' ')}) %} {%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %} {% if icon is defined %} {% endif %} {{ block('label') }} {% endblock %} {% block label %}{{ item.label|trans }}{% endblock %}