System Menu blocks do not have attributes

Created on 9 August 2019, about 6 years ago
Updated 20 January 2023, almost 3 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 1 month 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
    3 months 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
    3 months ago
    #542394
  • Pipeline finished with Success
    3 months ago
    #542403
  • πŸ‡ΊπŸ‡ΈUnited States theMusician

    The latest patch appears to work. This is a screenshot of the code output when two unique menu blocks are inserted on the same page via layout builder.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Nice! Glad we found a non template approach.

    Moving to NW for the test coverage also issue summary may need some love to include proposed solution and maybe screenshots.

    Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    mark_fullmer β†’ changed the visibility of the branch 3073895-system-menu-blocks to hidden.

  • Pipeline finished with Canceled
    3 months ago
    #553387
  • Pipeline finished with Failed
    3 months ago
    #553393
  • Pipeline finished with Failed
    3 months ago
    #553391
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    mark_fullmer β†’ changed the visibility of the branch 3073895-blocks-unique-id to hidden.

  • Pipeline finished with Failed
    3 months ago
    #553394
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • Pipeline finished with Failed
    3 months ago
    #553434
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    +1 on the approach from the latest MR. Looks like the newly added test coverage is failing to find the newly added ID though. I expect it will be a quick fix.

  • Pipeline finished with Failed
    3 months ago
    #554103
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    mark_fullmer β†’ changed the visibility of the branch 3073895-layout-builder-menu-blocks to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    mark_fullmer β†’ changed the visibility of the branch 3073895-layout-builder-menu-blocks to active.

  • Pipeline finished with Failed
    about 2 months ago
    #585437
  • Pipeline finished with Success
    about 2 months ago
    #585466
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Updated to accommodate tests! Ready for further review.

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Nice. Given it a (hopefully) last review. No issues, reckon this can be RTBC.

  • πŸ‡«πŸ‡·France nod_ Lille

    I'm not sure the fix is appropriate.

    IDs are not a backend concern, so it seems strange to handle that in the backend when the frontend can deal with it, we try not to rely on ids for styling and JS so the id could be random and it'd still work.

    The MR https://git.drupalcode.org/project/drupal/-/merge_requests/6965 looks promising need to use |clean_unique_id to make sure things don't break when you're dealing with ajax stuff.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    we try not to rely on ids for styling and JS so the id could be random and it'd still work.

    Indeed, separate from styling and JS targets, HTML ID attributes, especially, as in this case, when associated with HTML heading tags, are used as in-page anchor links. Having a "permalink" through a consistent HTML ID, therefore, is preferable to a random one.

    The approach in https://git.drupalcode.org/project/drupal/-/merge_requests/6965 would seem to provide a consistent ID, so if the Drupal core practice is to register HTML IDs in Twig templates, then MR 6965 would seem to be the right approach.

    The disadvantage, for the sake of stating it explicitly, of updating (multiple) core Twig templates is that any sites that have defined their own block--system-menu-block templates will not receive this change. In my experience in Drupal development, the menu block template is frequently modified on a per site basis.

  • Status changed to Needs review 25 days ago
  • Pipeline finished with Failed
    25 days ago
    Total: 1595s
    #610352
Production build 0.71.5 2024