Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Added badges on labels for KNP-menus (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbordinat authored Apr 1, 2020
1 parent a09115b commit 0037bf7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Resources/docs/knp_menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,24 @@ class KnpMenuBuilderSubscriber implements EventSubscriberInterface
$menu->addChild('blogId', [
'route' => 'item_symfony_route',
'label' => 'Blog',
'childOptions' => $event->getChildOptions()
'childOptions' => $event->getChildOptions(),
'extras' => [
'badge' => [
'color' => 'yellow',
'value' => 4,
],
],
])->setLabelAttribute('icon', 'fas fa-tachometer-alt');
$menu->getChild('blogId')->addChild('ChildOneItemId', [
'route' => 'child_1_route',
'label' => 'ChildOneDisplayName',
'extras' => [
'badges' => [
[ 'value' => 6, 'color' => 'blue' ],
[ 'value' => 5, ],
],
],
'childOptions' => $event->getChildOptions()
])->setLabelAttribute('icon', 'fas fa-rss-square');
Expand Down
20 changes: 20 additions & 0 deletions Resources/views/Partials/_menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@
<span>{% if options.allow_safe_labels and item.getExtra('safe_label', false) %}{{ item.label|trans|raw }}{% else %}{{ item.label|trans }}{% endif %}</span>
{% endif %}
{% if item.labelAttribute('data-image') %}<img src="{{ item.labelAttribute('data-image') }}" alt="{{ item.name }}" class="menu-thumbnail"/>{% endif %}

{% import _self as selfMacros %}
{% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
<span class="pull-right-container">
{{ selfMacros.badges(item) }}
<i class="fas fa-angle-left pull-right"></i>
</span>
{% else %}
{{ selfMacros.badges(item) }}
{% endif %}
{% endblock %}

{% macro badges(item) %}
{% import _self as selfMacros %}
{% if item.getExtra('badge') is not null %}
{{ selfMacros.badge(item.getExtra('badge')) }}
{% elseif item.getExtra('badges') is not null %}
{% for badge in item.getExtra('badges') %}
{{ selfMacros.badge(badge) }}
{% endfor %}
{% endif %}
{% endmacro %}

{% macro badge(badge) %}
<small class="label pull-right bg-{{ badge.color|default('green') }}">{{ badge.value }}</small>
{% endmacro %}

{% block list %}
{% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
{% import "knp_menu.html.twig" as macros %}
Expand Down

0 comments on commit 0037bf7

Please sign in to comment.