Problem/Motivation

`Drupal.Message.remove()` & `Drupal.Message.clear()` cannot remove messages from backend since all backend messages missing the `[data-drupal-message-id]` attribute.

Steps to reproduce

  1. Trigger or create a status message displayed either in the frontend or admin theme (assuming status-messages.html.twig is still using the template from core) .
  2. Note that there is no "data-drupal-message-id" attributes on the status message element.
  3. Open the javascript console and run: new Drupal.Message().add('Test message');. The newly created status message has the data-drupal-message-id attribute and so can be removed using Drupal.Message().remove(id)

Side note: Status messages created in Javascript seem to be inserted inside of status messages from the backend.

Proposed resolution

Determine whether the it's the intention of the Javascript Message API to interact with status messages from the backend as it works at the moment or if it's just supposed to interact with status messages it creates.

Update the status-messages.html.twig file to have a "data-drupal-message-id" attribute added, we may also need a better unique ID if some variation of "title_ids[type]" variable isn't unique enough.

It's also worth noting that if that ID is added to the main wrapper in the status-messages template file, if there are multiple messages, they would all share the same ID added to the wrapper.

Issue fork drupal-3202043

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

droplet created an issue. See original summary.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

eric115’s picture

Component: javascript » markup
Issue summary: View changes

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Category: Bug report » Feature request
Issue tags: -JavaScript +JavaScript, +Bug Smash Initiative

This came up as a daily triage target for the bug smash initiative.

Discussed with @catch in #bugsmash and we agree that this should be a feature request, so it currently works as designed that you cannot interact with the messages from PHP (as @catch pointed out, there is currently also no easy way to do that in PHP either), but it would be nice if you could do it.

kostyashupenko’s picture

Assigned: Unassigned » kostyashupenko
Issue tags: -JavaScript +JavaScript

Gonna fix it

kostyashupenko’s picture

Assigned: kostyashupenko » Unassigned
Status: Active » Needs work

What i'm thinking is that maybe we'd better put this unique ID html attribute into the {{ attributes }} globally for messages? Instead of overriding each template. And also - only in Claro theme {{ title_ids }} exist (and it's not pretty unique actually).

+ tests required

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.