JavaScript Messages API cannot remove message from backend

Created on 6 March 2021, almost 4 years ago
Updated 16 August 2023, over 1 year ago

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.

Feature request
Status

Needs work

Version

11.0 🔥

Component
Markup 

Last updated about 2 months ago

No maintainer
Created by

🇭🇰Hong Kong droplet

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇱Netherlands Lendude Amsterdam

    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.

  • Assigned to kostyashupenko
  • 🇷🇺Russia kostyashupenko Omsk

    Gonna fix it

  • Open on Drupal.org →
    Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Not currently mergeable.
  • @kostyashupenko opened merge request.
  • last update over 1 year ago
    Build Successful
  • Issue was unassigned.
  • Status changed to Needs work over 1 year ago
  • 🇷🇺Russia kostyashupenko Omsk

    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

Production build 0.71.5 2024