Duplicate HTML ID 'disclosure-menu' in some circumstances

Created on 21 May 2025, 2 months ago

Problem/Motivation

We are seeing that in some circumstances, when multiple disclosure menu blocks are on a page, the disclosure menus are not getting unique IDs.

In our case, we have 2 disclosure menus on all pages; 1 for our desktop menu and a second for our mobile menu. In our circumstance, the mobile menu is rendered on the page second and uses the menu toggle for the whole menu. We're seeing that sometimes our mobile menu won't open. In these circumstances, I noticed that our 2 disclosure menu blocks had the same ID and because the mobile menu was second, the toggle was trying to open the desktop menu. Aha!

But this is strange because we call <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Utility%21Html.php/function/Html%3A%3AgetUniqueId">Html::getUniqueId()</a> in <a href="https://git.drupalcode.org/project/disclosure_menu/-/blob/2.1.x/src/Plugin/Block/DisclosureMenuBlock.php?ref_type=heads#L402">DisclosureMenuBlock::build()</a> which states:

Prepares a string for use as a valid HTML ID and guarantees uniqueness.

My hypothesis is that has something to do with caching, my theory is that sometimes one of our blocks remains in render cache and the other does not. When the second block is built, Html::getUniqueId() doesn't recognize that the ID is already used because it was used in a cached block. 🀷 I think I must be wrong.

Maybe this is related to [1852090]

Steps to reproduce

See above.

Proposed resolution

Update Disclosure blocks to use the block's Plugin ID as part of it's ID. While this might seem unnecessary, it should drastically limit the chances of this issue happening.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States sonfd Portland, ME

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024