Drupal.messages displays new messages inside any existing messages on the site instead of separating them.

Created on 26 May 2020, over 4 years ago
Updated 6 June 2023, over 1 year ago

I am using the Drupal.messages JS API in the Entity Form Monitor module β†’ . I have an issue that when I add an 'error' message type, when there is an existing status message on the page, the messages get bundled together instead of styled separately, so my error message kind of looks like a success message.

              // Display a warning message on the form.
              const messages = new Drupal.Message();
              messages.add(
                  Drupal.t('The form has become outdated. Copy any unsaved work in the form below and then <a href="!url">reload this page</a>', { '!url': window.location }),
                  { type: 'error' }
              );

Actual Result:

β†’

<div data-drupal-messages="" class="messages-list">
  <div role="contentinfo" aria-labelledby="message-status-title" class="messages-list__item messages messages--status">
    <div class="messages__header">
      <h2 id="message-status-title" class="messages__title">
        Status message
      </h2>
    </div>
    <div class="messages__content">
      CSS and JavaScript cache cleared.
    </div>
    <div class="messages messages--error" role="alert" aria-labelledby="error-632206106470448-title" data-drupal-message-id="error-632206106470448" data-drupal-message-type="error">
      <div class="messages__header">
        <h2 id="error-632206106470448-title" class="messages__title">
          Error message
        </h2>
      </div>
      <div class="messages__content">
        The form has become outdated. Copy any unsaved work in the form below and then <a href="http://90.drupal.test/node/1/edit?destination=/admin/content">reload this page</a>
      </div>
    </div>
  </div>
</div>

The problem is that the error message is added inside of the <div role="contentinfo" aria-labelledby="message-status-title" class="messages-list__item messages messages--status"> div. If I close that div before the error message, then it works correctly.

Expected Result:

β†’

<div data-drupal-messages="" class="messages-list">
  <div role="contentinfo" aria-labelledby="message-status-title" class="messages-list__item messages messages--status">
    <div class="messages__header">
      <h2 id="message-status-title" class="messages__title">
        Status message
      </h2>
    </div>
    <div class="messages__content">
      CSS and JavaScript cache cleared.
    </div>
  </div> <!-- This is the closing div needed -->
  <div class="messages messages--error" role="alert" aria-labelledby="error-632206106470448-title" data-drupal-message-id="error-632206106470448" data-drupal-message-type="error">
    <div class="messages__header">
      <h2 id="error-632206106470448-title" class="messages__title">
        Error message
      </h2>
    </div>
    <div class="messages__content">
      The form has become outdated. Copy any unsaved work in the form below and then <a href="http://90.drupal.test/node/1/edit?destination=/admin/content">reload this page</a>
    </div>
  </div>
</div>
πŸ› Bug report
Status

Closed: duplicate

Version

9.5

Component
JavascriptΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States dave reid Nebraska USA

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

Production build 0.71.5 2024