- Issue created by @herved
- 🇧🇪Belgium herved
So far I see the code at core/modules/contextual/js/contextual.js:167 is attempting to get data-contextual-id elements that are not guaranteed to be there if these were relocated.
window.setTimeout(() => { initContextual( $context .find(`[data-contextual-id="${contextualID.id}"]:empty`) .eq(0), html, ); });
But this line hasn't changed in #3203920 so probably there is something more at play there.
- Merge request !12677Avoid JS error when contextual link target is moved or missing. → (Open) created by herved
- 🇮🇳India himanshu raj Noida
I was able to reproduce the issue by following the same steps as mentioned above:
1. Added the below JS snippet manually in the contextual.js file to simulate block movement:
Drupal.behaviors.contextual_bug = { attach(context) { once('contextual_bug', '#block-olivero-views-block-who-s-online-who-s-online-block .block__content', context).forEach((element) => { // Move the who's online block content at the bottom, before body closing tag, replicating mmenu. const clone = element.cloneNode(true); document.body.appendChild(clone); element.remove(); }); } }
2. Placed the Who's online block in the Content region.
3. Reloaded the page.
At this point, I saw the following error in the browser console:Uncaught TypeError: Cannot set properties of undefined (setting 'textContent')
Then I applied the patch from #10 → and run composer install.
After applying the patch:The error disappeared from the console.
###Patch / MR tested
Applied patch from #10###Results:
JS console error disappeared after applying the patch.
### Environment
Drupal 11.2.x, PHP 8.3, MySQL 8.0, DDEV 1.25 (Docker + WSL2). - 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
There are multiple places we call initContextual, we've updated one of those calls to check the element exists, but not the other.
But we've also updated the logic inside initContextual - I don't think we need both.
I'm inclined to make it check it from the outside rather than insideLet's do that - thanks!