JS messages are themed differently, not using status-messages.html.twig

Created on 10 April 2024, 3 months ago
Updated 29 June 2024, 1 day ago

Problem/Motivation

When theming status messages, this is generally done by overriding status-messages.html.twig. However, messages added via AJAX or JS ( JavaScript Messages API β†’ ) use Drupal.theme.message in core/misc/message.js or a similar function in the theme that overrides it.

Steps to reproduce

  1. Override the twig file in a theme
  2. Add a message via JS
  3. It doesn't match the expected HTML from the twig file

Here is Drupal.theme.message in core/misc/message.js.

Even the attributes and classes added aren't consistent with what core has in status-messages.html.twig

This requires developers wanting to unify their results to create JS that builds HTML similar to what is in the Twig file.

There are also inconsistencies between the results of the two methods in Core. One example is that in JS there is:

    messageWrapper.setAttribute(
      'role',
      type === 'error' || type === 'warning' ? 'alert' : 'status',
    );

which sets the role to "alert" for errors and warnings. But in the Twig file it only sets that role for errors. Also, the JS sets data-drupal-message-id, but the Twig template doesn't.

Proposed resolution

Is there a way to unify these, so that the Messages API uses the twig file and there aren't disparate results?

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
JavascriptΒ  β†’

Last updated about 7 hours ago

Created by

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

Comments & Activities

Production build 0.69.0 2024