System Menu blocks do not have attributes

Created on 9 August 2019, almost 6 years ago
Updated 20 January 2023, over 2 years ago

When viewing system menu blocks placed with layout builder, the blocks do not have title_attributes, contextual links, attributes.id and other commonly required fields for the theme layer.

This causes blocks to be placed with broken id attributes like the following:

<nav role="navigation" aria-labelledby="-menu" data-layout-content-preview-placeholder-label="&quot;Main navigation&quot; block" class="block block-menu navigation menu--main">
      
  <h2 id="-menu">Main navigation</h2>
  

      <div class="content">
        <div class="menu-toggle-target menu-toggle-target-show" id="show-"></div>
    <div class="menu-toggle-target" id="hide-"></div>
    <a class="menu-toggle" href="#show-">Show β€” Main navigation</a>
    <a class="menu-toggle menu-toggle--hide" href="#hide-">Hide β€” Main navigation</a>
    

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'menu__main' -->
<!-- FILE NAME SUGGESTIONS:
   * menu--main.html.twig
   x menu.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/navigation/menu.html.twig' -->

              <ul class="clearfix menu">
                    <li class="menu-item">
        <a href="/" data-drupal-link-system-path="<front>">Home</a>
              </li>
        </ul>
  


<!-- END OUTPUT from 'core/themes/classy/templates/navigation/menu.html.twig' -->


  </div>
</nav>
πŸ› Bug report
Status

Needs review

Version

10.1 ✨

Component
Layout builderΒ  β†’

Last updated about 14 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Needs work over 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    For the specific test coverage still needed.

    Also I see that the tests were updated is that because this attribute is now required? Won't this break existing sites that don't have it?

  • First commit to issue fork.
  • Merge request !6965Add a default ID to system menu blocks β†’ (Open) created by danielveza
  • Status changed to Needs review over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Opened an MR with a slightly different approach.

    The existing patch will add an ID to every since block that is added via LB, which feels a bit overkill to solve this. Instead I've added a default ID that will be applied to system_menu_blocks if one does not exist. If people like this approach I'll add some tests

  • Pipeline finished with Success
    over 1 year ago
    Total: 613s
    #114338
  • Status changed to Needs work over 1 year ago
  • The Needs Review Queue Bot β†’ tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request β†’ . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU
  • Status changed to Needs review over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU
  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Seems a few other block--system-menu-block.html.twig in core. Is there a place we can add an assertion to?

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    This appears no longer to be an issue as of 11.3.x. Placing a menu block on the page successfully generates unique ID attributes; placing a second instance of the same menu block confirms that the ID attributes and corresponding aria-labelledby attributes are unique. I haven't tracked down the core commit responsible for this yet, but will set this to "Postponed" given the above.

    Example markup of first instance of menu block:

    <nav id="block-olivero-mainnavigation" class="contextual-region primary-nav block block-menu navigation menu--main" aria-labelledby="block-olivero-mainnavigation-menu" role="navigation">
          
      <h2 class="block__title" id="block-olivero-mainnavigation-menu">Main navigation</h2>
    

    Example markup of second instance of menu block:

    <nav id="block-olivero-mainnavigation-2" class="contextual-region primary-nav block block-menu navigation menu--main" aria-labelledby="block-olivero-mainnavigation-2-menu" role="navigation">
          
      <h2 class="block__title" id="block-olivero-mainnavigation-2-menu">Main navigation</h2>
    
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    My previous comment was wrong. This is still an issue in Drupal core, though it only manifests in the context of placing a menu block using Layout Builder.

  • Pipeline finished with Canceled
    5 days ago
    #542402
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Seems a few other block--system-menu-block.html.twig in core. Is there a place we can add an assertion to?

    Agreed that modifying the Twig templates is not the best approach here. Instead, we can supply the ID attribute to all templates through the SystemMenuBlock::build() method. The rendering logic for a unique ID can be modeled exactly on block/src/Hook/BlockHooks::preprocessBlock(), per below:

        // Create a valid HTML ID and make sure it is unique.
        if (!empty($variables['elements']['#id'])) {
          $variables['attributes']['id'] = Html::getUniqueId('block-' . $variables['elements']['#id']);
        }
    

    I've created an alternate merge request, at https://git.drupalcode.org/project/drupal/-/merge_requests/12664

  • Pipeline finished with Canceled
    5 days ago
    #542394
  • Pipeline finished with Success
    5 days ago
    #542403
Production build 0.71.5 2024