ConfigFormBase::formatMultipleViolationsMessage() return type change will cause error

Created on 16 October 2024, 2 months ago

Problem/Motivation

๐Ÿ› Fix transltion string in ConfigFormBase.php Active changed the return type of ConfigFormBase::formatMultipleViolationsMessage() from TranslatableMarkup to MarkupInterface|\Stringable

  • When using the new return type in an override with a stable version of core <=11.0.5, the child class is returning a wider type than it's parent.
  • When using the old return type in an override with 11.x-dev, parent::formatMultipleViolationsMessage() returns a wider type than the child class.
  • The return type could be simplified to just \Stringable, which MarkupInterface implements.

For a module to maintain compatibility with older versions of core and not break if it calls the parent method which now returns Markup, it will need to change the value returned from its parent:

  protected function formatMultipleViolationsMessage(string $form_element_name, array $violations): TranslatableMarkup {
    $parent = parent::formatMultipleViolationsMessage($form_element_name, $violations);
    if ($parent instanceof TranslatableMarkup) {
      return $parent;
    }
    else {
      // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
      return $this->t((string) $parent);
    }
  }

Once the module drops support for older versions of core, it can update its typehint and remove the extra code.

\Drupal\update\UpdateSettingsForm also needs its return type updated.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component

forms system

Created by

๐Ÿ‡จ๐Ÿ‡ฆCanada gapple

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

Merge Requests

Comments & Activities

  • Issue created by @gapple
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rajeevkumar

    To update the formatMultipleViolationsMessage() method to maintain compatibility with both older and newer versions of Drupal core, you can adjust the return type to \Stringable and handle the MarkupInterface type from the parent. Hereโ€™s the updated code:

    protected function formatMultipleViolationsMessage(string $form_element_name, array $violations): Stringable {
        $parent = parent::formatMultipleViolationsMessage($form_element_name, $violations);
        if ($parent instanceof TranslatableMarkup) {
            return $parent;
        } else {
            // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
            return $this->t((string) $parent);
        }
    }

    This is a broader type that includes MarkupInterface, making it compatible with the newer core return type.

    If your code references \Drupal\update\UpdateSettingsForm, make sure its formatMultipleViolationsMessage() method is updated with the same logic to maintain consistency.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 101s
    #337596
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rajeevkumar

    The code ensures compatibility with environments where \Stringable might not be explicitly implemented or reliably checked. For example, if there are inconsistencies in extending \Stringable (e.g., in older or custom implementations), this safeguard ensures the return type is met.
    By explicitly handling \Stringable compatibility, the fallback to TranslatableMarkup guarantees compliance with the methodโ€™s return signature even in edge cases.
    While I acknowledge that the base class is expected to comply with the return type, the explicit check is intentional to ensure compatibility, clarity, and robustness in a dynamic and customizable system like Drupal.

Production build 0.71.5 2024