mirror of
https://gitlab.unicamp.br/infimecc_drupal11_modules/structural_pages.git
synced 2026-03-10 10:27:41 -03:00
Add dynamic menu block based on site structure hierarchy
Implements SiteStructureMenuBlock that renders a navigation menu from the ancestor entity (term, user, or group) through all content_page hierarchies. Features configurable depth, active trail highlighting, and proper cache invalidation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
63
templates/site-structure-menu.html.twig
Normal file
63
templates/site-structure-menu.html.twig
Normal file
@@ -0,0 +1,63 @@
|
||||
{#
|
||||
/**
|
||||
* @file
|
||||
* Template for the site structure menu block.
|
||||
*
|
||||
* Available variables:
|
||||
* - ancestor: The ancestor entity (term, user, or group).
|
||||
* - tree: Array of menu tree items, each containing:
|
||||
* - id: Node ID.
|
||||
* - title: Node title.
|
||||
* - url: Node URL.
|
||||
* - depth: Current depth level.
|
||||
* - children: Array of child items.
|
||||
* - entity: The node entity.
|
||||
* - active_trail: Array of node IDs in the active trail.
|
||||
* - show_ancestor_title: Whether to show the ancestor title as header.
|
||||
*/
|
||||
#}
|
||||
{{ attach_library('site_structure/menu') }}
|
||||
{% if tree is not empty %}
|
||||
<nav class="site-structure-menu" aria-label="{{ 'Site structure navigation'|t }}">
|
||||
{% if show_ancestor_title and ancestor %}
|
||||
<h2 class="site-structure-menu__title">
|
||||
{% if ancestor.toUrl is defined %}
|
||||
<a href="{{ ancestor.toUrl }}">{{ ancestor.label }}</a>
|
||||
{% else %}
|
||||
{{ ancestor.label }}
|
||||
{% endif %}
|
||||
</h2>
|
||||
{% endif %}
|
||||
|
||||
<ul class="site-structure-menu__list site-structure-menu__list--level-1">
|
||||
{% for item in tree %}
|
||||
{{ _self.menu_item(item, active_trail, 1) }}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% macro menu_item(item, active_trail, depth) %}
|
||||
{% set is_active = item.id in active_trail %}
|
||||
{% set has_children = item.children is not empty %}
|
||||
{% set classes = [
|
||||
'site-structure-menu__item',
|
||||
'site-structure-menu__item--level-' ~ depth,
|
||||
is_active ? 'site-structure-menu__item--active-trail',
|
||||
has_children ? 'site-structure-menu__item--has-children',
|
||||
] %}
|
||||
|
||||
<li{{ create_attribute().addClass(classes) }}>
|
||||
<a href="{{ item.url }}" class="site-structure-menu__link{% if is_active %} site-structure-menu__link--active-trail{% endif %}">
|
||||
{{- item.title -}}
|
||||
</a>
|
||||
|
||||
{% if has_children %}
|
||||
<ul class="site-structure-menu__list site-structure-menu__list--level-{{ depth + 1 }}">
|
||||
{% for child in item.children %}
|
||||
{{ _self.menu_item(child, active_trail, depth + 1) }}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endmacro %}
|
||||
Reference in New Issue
Block a user