Skip to content

Commit a779a1f

Browse files
committedJan 2, 2025
bug #6678 Fix some issues with menu item badges that have no value (javiereguiluz)
This PR was merged into the 4.x branch. Discussion ---------- Fix some issues with menu item badges that have no value Fixes #5847. Commits ------- afc9261 Fix some issues with menu item badges that have no value
2 parents 0a025ee + afc9261 commit a779a1f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed
 

‎src/Dto/MenuItemDto.php

+12
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ public function setBadge(mixed $content, string $style, array $htmlAttributes =
220220
$this->badge = new MenuItemBadgeDto($content, trim($style), $htmlAttributes);
221221
}
222222

223+
/**
224+
* Tells if the badge HTML element should be displayed or not. This is used
225+
* e.g. to not display a badge when its value is null or false.
226+
*/
227+
public function hasVisibleBadge(): bool
228+
{
229+
$badgeContent = $this->getBadge()?->getContent();
230+
231+
// the number 0 is not included here because it's valid to display a badge with the value 0
232+
return null !== $badgeContent && false !== $badgeContent && '' !== $badgeContent;
233+
}
234+
223235
/**
224236
* @return MenuItemDto[]
225237
*/

‎templates/menu.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
{{ item.label|trans|raw }}
6060
</span>
6161
{% if item.hasSubItems %}<twig:ea:Icon name="internal:chevron-right" class="submenu-toggle-icon" />{% endif %}
62-
{% if item.badge %}
62+
{% if item.hasVisibleBadge %}
6363
<span class="menu-item-badge rounded-pill badge {{ item.badge.cssClass }}" {{ _self.render_html_attributes(item.badge) }} style="{{ item.badge.htmlStyle }}">{{ item.badge.content }}</span>
6464
{% endif %}
6565
</a>

0 commit comments

Comments
 (0)