Skip to content

Commit d1fa245

Browse files
author
Thomas Jarrand
committed
Better active resolver
1 parent c1a1908 commit d1fa245

2 files changed

Lines changed: 44 additions & 19 deletions

File tree

Menu/Builder.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,33 @@ public function isActive(array $item): bool
3838
return (bool) $item['active'];
3939
}
4040

41-
return (isset($item['root']) && $this->isCurrentRoot($item['root']))
42-
|| (isset($item['branch']) && $this->isCurrentBranch($item['branch']))
43-
|| (isset($item['route']) && $this->isCurrentRoute($item['route']));
41+
return $this->isCurrentBranch($this->resolve($item, ['branch', 'root', 'route']))
42+
|| $this->isCurrentRoot($this->resolve($item, ['root', 'route']))
43+
|| $this->isCurrentRoute($this->resolve($item, ['route']));
4444
}
4545

46-
public function isCurrentRoot(string $root): bool
46+
private function resolve(array $item, array $keys)
47+
{
48+
foreach ($keys as $key) {
49+
if (isset($item[$key])) {
50+
return $item[$key];
51+
}
52+
}
53+
54+
return null;
55+
}
56+
57+
public function isCurrentRoot(?string $root): bool
4758
{
4859
return $this->getCurrentAttribute('_root') === $root;
4960
}
5061

51-
public function isCurrentBranch(string $branch): bool
62+
public function isCurrentBranch(?string $branch): bool
5263
{
5364
return $this->getCurrentAttribute('_branch') === $branch;
5465
}
5566

56-
public function isCurrentRoute(string $route): bool
67+
public function isCurrentRoute(?string $route): bool
5768
{
5869
return $this->getCurrentAttribute('_route') === $route;
5970
}

Resources/views/base.html.twig

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,32 @@
3636
</nav>
3737
{% endblock %}
3838
{% block nav_user %}
39-
<div class="user-menu drop drop--down">
40-
<button class="user-menu__button drop__button">
41-
{% block user_menu_label app.user|default('Jane doe') %}
42-
</button>
43-
<ul class="user-menu__content drop__menu">
44-
{#% set item_class = '' %#}
45-
{#% set item_active_class = '' %#}
46-
{% for item in menu_user|default([]) %}
47-
{{ block('menu_item') }}
48-
{% endfor %}
49-
</ul>
50-
</div>
39+
{% if app.user %}
40+
{% block nav_user_authenticated %}
41+
<div class="user-menu drop drop--down">
42+
<button class="user-menu__button drop__button">
43+
{% block user_menu_label app.user|default('Jane doe') %}
44+
</button>
45+
<ul class="user-menu__content drop__menu">
46+
{#% set item_class = '' %#}
47+
{#% set item_active_class = '' %#}
48+
{% for item in menu_user|default([]) %}
49+
{{ block('menu_item') }}
50+
{% endfor %}
51+
</ul>
52+
</div>
53+
{% endblock %}
54+
{% else %}
55+
{% block nav_user_anonymous %}
56+
<ul class="user-menu">
57+
{% set item_class = 'user-menu__button' %}
58+
{% set item_active_class = 'user-menu__button--active' %}
59+
{% for item in menu_anonymous|default([]) %}
60+
{{ block('menu_item') }}
61+
{% endfor %}
62+
</ul>
63+
{% endblock %}
64+
{% endif %}
5165
{% endblock %}
5266
<button class="menu-button"></button>
5367
</header>
@@ -126,7 +140,7 @@
126140
</nav>
127141
{% endif %}
128142
{% endblock %}
129-
<div class="container">
143+
<div class="container {% block content_class '' %}">
130144
{% block content '' %}
131145
</div>
132146
</div>

0 commit comments

Comments
 (0)