Score:0

--2 is appended to the block ID

jp flag

I have a strange problem. After upgrading a website from D8 to D9, some of my block IDs are changed. Some of them now have --2 behind the ID. This is messing up my styling and scripts. And I'm wondering how this can happen and if there is a simple solution for it. I found this issue without any solution other than changing my CSS and JS files.

I know Drupal sometimes changes ID after and Ajax call, this new ID is already three after the first page load. I can offcourse remove the --2 from the id using twig strip or something but this feels like a big hack, so I want to know why this is happening.

Does anyone have an idea where to look for where and why, how this is happening?

Jaypan avatar
de flag
If I recall correctly (I looked into it maybe 10 years ago), it's for the #ajax functionality. As the element is being rebuilt, it's not the original element, it's a new element, so semantically using the same ID would be incorrect. If you need to keep targeting the same item, you can use the wrapper element class plus the element class to target the element.
cn flag
Sounds like something is calling `Html::getUniqueId()` twice for those blocks. It happens in `template_preprocess_block` by default, so look for custom code which might try to set the ID again, or perhaps anything that would be causing the blocks to be rendered twice
Joost avatar
jp flag
Thanks Clive, I had a look at this and I have two contrib modules doing this webform and facets. But removing the hook from both modules doesn't eliminate the problem. My custom modules dont have this hook. So it might by a core module but how to figure out which one? I found out it is only doing this with views blocks. So for some reason these blocks are rendered twice and only showing once? What would my next step in solving this problem be?
Kevin avatar
in flag
The short term fix is to not style by ids, this is apt to happen, depending.
Joost avatar
jp flag
Yes I already fixed the styling by using the class but this problem still bothers me. Rendering several blocks twice for no reason doesn’t feel right. So I want to dig into this to really fix it. But how…?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.