FormHelperTrait::addWarning() should use '#theme' => 'status_messages' rather than output raw markup

Created on 30 January 2023, over 1 year ago
Updated 8 February 2023, over 1 year ago

Problem/Motivation

At the time of writing, FormHelperTrait::addWarning() contains this which creates a warning message manually via raw markup and directly attaches the Classy message library.

  protected function addWarning($message, $tokens) {
    $warning = $this->t($message, $tokens);
    $this->form()['warning'] = [
      '#type' => 'markup',
      '#weight' => -10,
      '#markup' => Markup::create("
        <div class='messages messages--warning'>
          {$warning}
        </div>
      "),
    ];
    $this->form()['#attached']['library'][] = 'classy/messages';
    return $this;
  }

This is both unnecessary and prevents the expected Drupal rendering, alter hooks/preprocess, and other things correctly interacting with it. Additionally, the Classy theme is deprecated in Drupal 9.5 and removed in Drupal 10 β†’ .

Steps to reproduce

Try to apply any customizations (e.g. preprocess) to warning messages; they won't be applied to the warning message FormHelperTrait::addWarning() creates. Run this on Drupal 10 without the contrib Classy installed; the status message will no longer have the expected styling.

A minor example on Drupal 9.5 using Claro without the change:

Notice the icon is not placed where it should be and not the correct Claro icon.

With the change:

Notice that it now uses the correct Claro icon and positions it where it should be.

Proposed resolution

In #3008741-5: Fix readonly notice message styling β†’ for the Read only mode module, I changed a similar markup output to '#theme' => 'status_messages' which follows Drupal best practices and allows current and future modules and themes to behave as expected.

Remaining tasks

Do the above.

User interface changes

None?

API changes

None?

Data model changes

None?

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

User interface

Created by

πŸ‡¨πŸ‡¦Canada Ambient.Impact Toronto

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

Comments & Activities

Production build 0.69.0 2024