Open gagarine opened 5 years ago
So navbar class should be removed in the block template as the page.html.twig allows you to add multiple menus. Removing it in block--system-menu-block.html would break menu that are not rendered in page.main_navigation or page.second_navigation region.
Somethings like this would allow to have template suggestion depending of the region:
// Add a region variable to a block.
// http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
function THEMENAME_preprocess_block(&$variables) {
if (isset($variables["elements"]["#id"])) {
$block_id = $variables["elements"]["#id"];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
$variables["content"]["#attributes"]["region"] = $block->getRegion();
}
}
}
// add a template suggestion based on region name
// http://kristiankaa.dk/article/drupal8-region-specific-menu-theme-hook-suggestion
function THEMENAME_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
if (isset($variables["attributes"]["region"])) {
$suggestions[] = $variables["theme_hook_original"] . "__" . $variables["attributes"]["region"];
}
}
Then we can use specific block template for menu in those region. Somethings like that:
{% set heading_id = attributes.id ~ '-menu'|clean_id %}
{# Label. If not displayed, we still provide it for screen readers. #}
{% if not configuration.label_display %}
{% set title_attributes = title_attributes.addClass('sr-only') %}
{% endif %}
<h2{{ title_attributes.setAttribute('id', heading_id) }}>{{ configuration.label }}</h2>
{% block content %}
{{ content }}
{% endblock %}
Nav block added in navbar add again a uselss nav.
The problem is block--system-menu-block.html add the nav and page.html.twig also have a nav.