Having the same type of entity on one page with comments enabled breaks the HTML ID uniqueness of the wrapper

Created on 9 April 2021, almost 4 years ago
Updated 24 July 2023, over 1 year ago

Problem/Motivation

When you display the same type of entity on the same page multiple times with comments enabled (via Views), the wrapper ID stop being unique. Since this is passed through Html::getUniqueId this is not a problem on the first page load, but becomes an issue when one entity has a change in its comments and needs to be rerendered. Again this work fine for the normal AJAX reload, but when reloading the page only the changed entity will be rerendered but outside the context of the full page, so Html::getUniqueId will not do anything and the ID will not be unique, and worse the data-wrapper-html-id will point to the wrong entity, so any updates done will be shown on the wrong entity.

Steps to reproduce

  • Build a View that displays your entities that have a comment field using ajax_comments, with the full entities being rendered (including the comment form)
  • Add 2 entities of the same bundle so they are shown in the View
  • Go to the page that shows the View
  • Add comments to the SECOND entity
  • Reload the page
  • Add a comment to the SECOND entity again, the AJAX feedback will be shown on the first entity

Proposed resolution

Make the wrapper ID more specific and don't depend on Html::getUniqueId by adding the ID of the commented entity to the ID.

Remaining tasks

Patch and review

🐛 Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

🇳🇱Netherlands Lendude Amsterdam

Live updates comments and jobs are added and updated live.
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.

  • 🇩🇪Germany a.dmitriiev

    The fix helped me, thanks. And it actually makes sense to have the id of the parent entity in the id attribute.

  • First commit to issue fork.
  • It sounds like you're encountering an issue with duplicate wrapper IDs when displaying the same type of entity multiple times on a page with comments enabled via Views in Drupal. This problem arises because the wrapper ID, generated by Html::getUniqueId, stops being unique when entities are rerendered individually.

    To address this, you might secret starbucks menus consider implementing a custom solution to ensure unique IDs for each entity wrapper, even after rerendering. This could involve modifying the Views template or adding custom JavaScript to handle the ID uniqueness.

  • You've accurately described a tricky issue with Drupal's rendering and comment system when using Views and AJAX. The problem stems from the combination of non-unique wrapper IDs generated by Html::getUniqueId when entities of the same type are displayed multiple times on a page, and how AJAX updates interact with this. Click Here

  • You've accurately described a tricky issue with Drupal's rendering and comment system when using Views and AJAX. The problem stems from the combination of non-unique wrapper IDs generated by Html::getUniqueId when entities of the same type are displayed multiple times on a page, and how AJAX updates interact with this. Click Here

Production build 0.71.5 2024