Commit 105518a2 authored by webchick's avatar webchick

Issue #2547159 by mdrummond, mortendk, davidhernandez, lauriii, joelpittet,...

Issue #2547159 by mdrummond, mortendk, davidhernandez, lauriii, joelpittet, Wim Leers, Cottser: menu class generating should be in the template not in a function
parent 5cb07837
...@@ -243,26 +243,28 @@ protected function buildItems(array $tree, CacheableMetadata &$tree_access_cache ...@@ -243,26 +243,28 @@ protected function buildItems(array $tree, CacheableMetadata &$tree_access_cache
if ($data->access instanceof AccessResultInterface && !$data->access->isAllowed()) { if ($data->access instanceof AccessResultInterface && !$data->access->isAllowed()) {
continue; continue;
} }
$element = [];
$class = ['menu-item']; // Set a variable for the <li> tag. Only set 'expanded' to true if the
// Set a class for the <li>-tag. Only set 'expanded' class if the link // link also has visible children within the current tree.
// also has visible children within the current tree. $element['is_expanded'] = FALSE;
$element['is_collapsed'] = FALSE;
if ($data->hasChildren && !empty($data->subtree)) { if ($data->hasChildren && !empty($data->subtree)) {
$class[] = 'menu-item--expanded'; $element['is_expanded'] = TRUE;
} }
elseif ($data->hasChildren) { elseif ($data->hasChildren) {
$class[] = 'menu-item--collapsed'; $element['is_collapsed'] = TRUE;
} }
// Set a class if the link is in the active trail. // Set a helper variable to indicate whether the link is in the active
// trail.
$element['in_active_trail'] = FALSE;
if ($data->inActiveTrail) { if ($data->inActiveTrail) {
$class[] = 'menu-item--active-trail'; $element['in_active_trail'] = TRUE;
} }
// Note: links are rendered in the menu.html.twig template; and they // Note: links are rendered in the menu.html.twig template; and they
// automatically bubble their associated cacheability metadata. // automatically bubble their associated cacheability metadata.
$element = array();
$element['attributes'] = new Attribute(); $element['attributes'] = new Attribute();
$element['attributes']['class'] = $class;
$element['title'] = $link->getTitle(); $element['title'] = $link->getTitle();
$element['url'] = $link->getUrlObject(); $element['url'] = $link->getUrlObject();
$element['url']->setOption('set_active_class', TRUE); $element['url']->setOption('set_active_class', TRUE);
......
...@@ -539,30 +539,33 @@ protected function buildItems(array $tree) { ...@@ -539,30 +539,33 @@ protected function buildItems(array $tree) {
$items = []; $items = [];
foreach ($tree as $data) { foreach ($tree as $data) {
$class = ['menu-item']; $element = [];
// Generally we only deal with visible links, but just in case. // Generally we only deal with visible links, but just in case.
if (!$data['link']['access']) { if (!$data['link']['access']) {
continue; continue;
} }
// Set a class for the <li>-tag. Since $data['below'] may contain local // Set a class for the <li> tag. Since $data['below'] may contain local
// tasks, only set 'expanded' class if the link also has children within // tasks, only set 'expanded' to true if the link also has children within
// the current book. // the current book.
$element['is_expanded'] = FALSE;
$element['is_collapsed'] = FALSE;
if ($data['link']['has_children'] && $data['below']) { if ($data['link']['has_children'] && $data['below']) {
$class[] = 'menu-item--expanded'; $element['is_expanded'] = TRUE;
} }
elseif ($data['link']['has_children']) { elseif ($data['link']['has_children']) {
$class[] = 'menu-item--collapsed'; $element['is_collapsed'] = TRUE;
} }
// Set a class if the link is in the active trail. // Set a helper variable to indicate whether the link is in the active
// trail.
$element['in_active_trail'] = FALSE;
if ($data['link']['in_active_trail']) { if ($data['link']['in_active_trail']) {
$class[] = 'menu-item--active-trail'; $element['in_active_trail'] = TRUE;
} }
// Allow book-specific theme overrides. // Allow book-specific theme overrides.
$element = [];
$element['attributes'] = new Attribute(); $element['attributes'] = new Attribute();
$element['attributes']['class'] = $class;
$element['title'] = $data['link']['title']; $element['title'] = $data['link']['title'];
$node = $this->entityManager->getStorage('node')->load($data['link']['nid']); $node = $this->entityManager->getStorage('node')->load($data['link']['nid']);
$element['url'] = $node->urlInfo(); $element['url'] = $node->urlInfo();
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
* - below: The book item child items. * - below: The book item child items.
* - title: The book link title. * - title: The book link title.
* - url: The book link URL, instance of \Drupal\Core\Url. * - url: The book link URL, instance of \Drupal\Core\Url.
* - is_expanded: TRUE if the link has visible children within the current
* book tree.
* - is_collapsed: TRUE if the link has children within the current book tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
* *
* @ingroup themeable * @ingroup themeable
*/ */
...@@ -31,14 +36,14 @@ ...@@ -31,14 +36,14 @@
{% else %} {% else %}
<ul> <ul>
{% endif %} {% endif %}
{% for item in items %} {% for item in items %}
<li{{ item.attributes }}> <li{{ item.attributes }}>
{{ link(item.title, item.url) }} {{ link(item.title, item.url) }}
{% if item.below %} {% if item.below %}
{{ book_tree.book_links(item.below, attributes, menu_level + 1) }} {{ book_tree.book_links(item.below, attributes, menu_level + 1) }}
{% endif %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
* - title: The menu link title. * - title: The menu link title.
* - url: The menu link url, instance of \Drupal\Core\Url * - url: The menu link url, instance of \Drupal\Core\Url
* - localized_options: Menu link localized options. * - localized_options: Menu link localized options.
* - is_expanded: TRUE if the link has visible children within the current
* menu tree.
* - is_collapsed: TRUE if the link has children within the current menu tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
* *
* @ingroup themeable * @ingroup themeable
*/ */
...@@ -31,14 +36,14 @@ ...@@ -31,14 +36,14 @@
{% else %} {% else %}
<ul> <ul>
{% endif %} {% endif %}
{% for item in items %} {% for item in items %}
<li{{ item.attributes }}> <li{{ item.attributes }}>
{{ link(item.title, item.url) }} {{ link(item.title, item.url) }}
{% if item.below %} {% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }} {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
...@@ -140,14 +140,29 @@ public function providerTestBuildCacheability() { ...@@ -140,14 +140,29 @@ public function providerTestBuildCacheability() {
] ]
]; ];
$get_built_element = function(MenuLinkTreeElement $element, array $classes) { $get_built_element = function(MenuLinkTreeElement $element) {
return [ $return = [
'attributes' => new Attribute(['class' => array_merge(['menu-item'], $classes)]), 'attributes' => new Attribute(),
'title' => $element->link->getTitle(), 'title' => $element->link->getTitle(),
'url' => new Url($element->link->getRouteName(), $element->link->getRouteParameters(), ['set_active_class' => TRUE]), 'url' => new Url($element->link->getRouteName(), $element->link->getRouteParameters(), ['set_active_class' => TRUE]),
'below' => [], 'below' => [],
'original_link' => $element->link, 'original_link' => $element->link,
'is_expanded' => FALSE,
'is_collapsed' => FALSE,
'in_active_trail' => FALSE,
]; ];
if ($element->hasChildren && !empty($element->subtree)) {
$return['is_expanded'] = TRUE;
}
elseif ($element->hasChildren) {
$return['is_collapsed'] = TRUE;
}
if ($element->inActiveTrail) {
$return['in_active_trail'] = TRUE;
}
return $return;
}; };
// The three access scenarios described in this method's documentation. // The three access scenarios described in this method's documentation.
...@@ -195,7 +210,7 @@ public function providerTestBuildCacheability() { ...@@ -195,7 +210,7 @@ public function providerTestBuildCacheability() {
$tree[0]->access = $access; $tree[0]->access = $access;
if ($access === NULL || $access->isAllowed()) { if ($access === NULL || $access->isAllowed()) {
$expected_build = $base_expected_build; $expected_build = $base_expected_build;
$expected_build['#items']['test.example1'] = $get_built_element($tree[0], []); $expected_build['#items']['test.example1'] = $get_built_element($tree[0]);
} }
else { else {
$expected_build = $base_expected_build_empty; $expected_build = $base_expected_build_empty;
...@@ -217,9 +232,9 @@ public function providerTestBuildCacheability() { ...@@ -217,9 +232,9 @@ public function providerTestBuildCacheability() {
$tree[0]->access = $access; $tree[0]->access = $access;
$expected_build = $base_expected_build; $expected_build = $base_expected_build;
if ($access === NULL || $access->isAllowed()) { if ($access === NULL || $access->isAllowed()) {
$expected_build['#items']['test.example1'] = $get_built_element($tree[0], []); $expected_build['#items']['test.example1'] = $get_built_element($tree[0]);
} }
$expected_build['#items']['test.example2'] = $get_built_element($tree[1], []); $expected_build['#items']['test.example2'] = $get_built_element($tree[1]);
$expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts()); $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts());
$data[] = [ $data[] = [
'description' => "Single-level tree; access=$i; link=$j.", 'description' => "Single-level tree; access=$i; link=$j.",
...@@ -245,13 +260,13 @@ public function providerTestBuildCacheability() { ...@@ -245,13 +260,13 @@ public function providerTestBuildCacheability() {
]; ];
$tree[0]->subtree[0]->subtree[0]->access = $access; $tree[0]->subtree[0]->subtree[0]->access = $access;
$expected_build = $base_expected_build; $expected_build = $base_expected_build;
$expected_build['#items']['test.roota'] = $get_built_element($tree[0], ['menu-item--expanded']); $expected_build['#items']['test.roota'] = $get_built_element($tree[0]);
$expected_build['#items']['test.roota']['below']['test.parentc'] = $get_built_element($tree[0]->subtree[0], ['menu-item--expanded']); $expected_build['#items']['test.roota']['below']['test.parentc'] = $get_built_element($tree[0]->subtree[0]);
if ($access === NULL || $access->isAllowed()) { if ($access === NULL || $access->isAllowed()) {
$expected_build['#items']['test.roota']['below']['test.parentc']['below']['test.example1'] = $get_built_element($tree[0]->subtree[0]->subtree[0], []); $expected_build['#items']['test.roota']['below']['test.parentc']['below']['test.example1'] = $get_built_element($tree[0]->subtree[0]->subtree[0]);
} }
$expected_build['#items']['test.rootb'] = $get_built_element($tree[1], ['menu-item--expanded']); $expected_build['#items']['test.rootb'] = $get_built_element($tree[1]);
$expected_build['#items']['test.rootb']['below']['test.example2'] = $get_built_element($tree[1]->subtree[0], []); $expected_build['#items']['test.rootb']['below']['test.example2'] = $get_built_element($tree[1]->subtree[0]);
$expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts()); $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts());
$data[] = [ $data[] = [
'description' => "Multi-level tree; access=$i; link=$j.", 'description' => "Multi-level tree; access=$i; link=$j.",
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
* - title: The menu link title. * - title: The menu link title.
* - url: The menu link url, instance of \Drupal\Core\Url * - url: The menu link url, instance of \Drupal\Core\Url
* - localized_options: Menu link localized options. * - localized_options: Menu link localized options.
* - is_expanded: TRUE if the link has visible children within the current
* menu tree.
* - is_collapsed: TRUE if the link has children within the current menu tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
* *
* @ingroup themeable * @ingroup themeable
*/ */
...@@ -31,14 +36,22 @@ ...@@ -31,14 +36,22 @@
{% else %} {% else %}
<ul class="toolbar-menu"> <ul class="toolbar-menu">
{% endif %} {% endif %}
{% for item in items %} {% for item in items %}
<li{{ item.attributes }}> {%
{{ link(item.title, item.url) }} set classes = [
{% if item.below %} 'menu-item',
{{ menus.menu_links(item.below, attributes, menu_level + 1) }} item.is_expanded ? 'menu-item--expanded',
{% endif %} item.is_collapsed ? 'menu-item--collapsed',
</li> item.in_active_trail ? 'menu-item--active-trail',
{% endfor %} ]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
* - below: The book item child items. * - below: The book item child items.
* - title: The book link title. * - title: The book link title.
* - url: The book link URL, instance of \Drupal\Core\Url. * - url: The book link URL, instance of \Drupal\Core\Url.
* - is_expanded: TRUE if the link has visible children within the current
* book tree.
* - is_collapsed: TRUE if the link has children within the current book tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
*/ */
#} #}
{% import _self as book_tree %} {% import _self as book_tree %}
...@@ -29,14 +34,22 @@ ...@@ -29,14 +34,22 @@
{% else %} {% else %}
<ul class="menu"> <ul class="menu">
{% endif %} {% endif %}
{% for item in items %} {% for item in items %}
<li{{ item.attributes }}> {%
{{ link(item.title, item.url) }} set classes = [
{% if item.below %} 'menu-item',
{{ book_tree.book_links(item.below, attributes, menu_level + 1) }} item.is_expanded ? 'menu-item--expanded',
{% endif %} item.is_collapsed ? 'menu-item--collapsed',
</li> item.in_active_trail ? 'menu-item--active-trail',
{% endfor %} ]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ book_tree.book_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
* - title: The menu link title. * - title: The menu link title.
* - url: The menu link url, instance of \Drupal\Core\Url * - url: The menu link url, instance of \Drupal\Core\Url
* - localized_options: Menu link localized options. * - localized_options: Menu link localized options.
* - is_expanded: TRUE if the link has visible children within the current
* menu tree.
* - is_collapsed: TRUE if the link has children within the current menu tree
* that are not currently visible.
* - in_active_trail: TRUE if the link is in the active trail.
*/ */
#} #}
{% import _self as menus %} {% import _self as menus %}
...@@ -29,14 +34,22 @@ ...@@ -29,14 +34,22 @@
{% else %} {% else %}
<ul class="menu"> <ul class="menu">
{% endif %} {% endif %}
{% for item in items %} {% for item in items %}
<li{{ item.attributes }}> {%
{{ link(item.title, item.url) }} set classes = [
{% if item.below %} 'menu-item',
{{ menus.menu_links(item.below, attributes, menu_level + 1) }} item.is_expanded ? 'menu-item--expanded',
{% endif %} item.is_collapsed ? 'menu-item--collapsed',
</li> item.in_active_trail ? 'menu-item--active-trail',
{% endfor %} ]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment