Problem/Motivation

While checking the Glossary 2 module with phpstan, I noticed a warning about the setError method.

FormStateInterface::setError() is defined as:

  /**
   * ...snip..
   * @param string $message
   *   (optional) The error message to present to the user.
   * ...snip..
   */
  public function setError(array &$element, $message = '');

However, most (82) implementations in core actually take a TranslatableMarkup for the $message parameter.

The 23 exceptions are:

  • web/core/lib/Drupal/Core/Form/FormStateInterface.php specifies the default as "", being self-consistent
  • web/core/lib/Drupal/Core/Form/FormState.php specifies the default as ""
  • web/core/lib/Drupal/Core/Form/FormStateDecoratorBase.php specifies the default as ""
  • web/core/tests/Drupal/Tests/Core/Form/FormStateTest.php passes 'Fail'
  • web/core/modules/settings_tray/tests/modules/settings_tray_test/src/Plugin/Block/ValidationErrorBlock.php passes 'Sorry system error. Please save again.'
  • web/core/modules/responsive_image/src/ResponsiveImageStyleForm.php passes 'Provide a value for the sizes attribute.' and 'Select at least one image style.', which looks like they should actually be translated
  • 16 cases of pass variables, like web/core/modules/views/src/Plugin/views/display/PathPluginBase.php passing an $error, which is actually always a TranslatableMarkup.

Steps to reproduce

Compare the outputs of

  • grep -irE "setError\([^\)]" web/core | cut -c1-180 | grep -E '(this->t|this->formatPlural| t|new TranslatableMarkup)\('
  • grep -irE "setError\([^\)]" web/core | cut -c1-180 | grep -vE '(this->t|this->formatPlural| t|new TranslatableMarkup)\('

Proposed resolution

  • Modify setError to match what code actually does: update the PHPdoc to @param Drupal\Core\StringTranslation $error

Remaining tasks

Decide on how to change the non-TranslatableMarkup calls. Most of them appear to be in tests or missing translations (ResponsiveImageStyle).

User interface changes

None.

API changes

If it is just a phpdoc, none. If we type the return value, it may qualify as an API change, but one that matches core implementations, and those implementations I looked at in contrib.

Data model changes

None.

Release notes snippet

None.

CommentFileSizeAuthor
#2 3373098-2.patch555 bytesabhijith s

Comments

fgm created an issue. See original summary.

abhijith s’s picture

StatusFileSize
new555 bytes

Adding patch for the issue. Please review.

andreastkdf’s picture

Status: Active » Reviewed & tested by the community
Issue tags: +ContributionWeekend, +ContributionWeekend2024

Looks good to me, thanks!

fgm’s picture

Status: Reviewed & tested by the community » Needs work

The doccomment change is OK, but as explained in the IS, there are 23 places in core where this change needs to be applied to, for default strings and test values. The PR needs to change all of that too.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.