Problem/Motivation

\Drupal\Core\Field\WidgetBase::getFilteredDescription() can cause deprecations on PHP 8.1 because $this->fieldDefinition->getDescription() can return a NULL... \Drupal::token()->replace() calls string functions that will issue a deprecation in PHP 8.1.

Steps to reproduce

Proposed resolution

Given the docs are:

  /**
   * Returns a human readable description.
   *
   * Descriptions are usually used on user interfaces where the data is edited
   * or displayed.
   *
   * @return string|null
   *   The description, or NULL if no description is available.
   */
  public function getDescription();

we need to handle NULL at this point.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#2 3239744-2.patch684 bytesalexpott

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new684 bytes

This is the simplest fix.

alexpott’s picture

We could do

  /**
   * Returns the filtered field description.
   *
   * @return \Drupal\Core\Field\FieldFilteredMarkup|string
   *   The filtered field description, with tokens replaced.
   */
  protected function getFilteredDescription() {
    $description = $this->fieldDefinition->getDescription();
    if (is_null($description)) {
      return '';
    }
    return FieldFilteredMarkup::create(\Drupal::token()->replace($description));
  }

but I'm not sure it is worth it.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

We can also do:

  return FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription() ?? ''));

Just like the solution from comment #2, they are different solution with the same result.

Looks good to me.

  • catch committed 50d6142 on 9.3.x
    Issue #3239744 by alexpott, daffie: \Drupal\Core\Field\WidgetBase::...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Yeah the cast to string seems fine here.

Committed 50d6142 and pushed to 9.3.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.