Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Nav blocks in navbar display nav #1

Open
gagarine opened this issue Apr 19, 2019 · 1 comment
Open

Nav blocks in navbar display nav #1

gagarine opened this issue Apr 19, 2019 · 1 comment

Comments

@gagarine
Copy link
Owner

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.

@gagarine
Copy link
Owner Author

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 %}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant