Problem/Motivation

\Drupal\Core\Template\AttributeString::__toString() will cause a deprecation in PHP 8.1 when $this->value is NULL.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#3 3239762-3.patch520 bytesdaffie
#2 3239762-2-will-fail.patch557 bytesalexpott

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new557 bytes
daffie’s picture

StatusFileSize
new520 bytes

Lets see if a string type casting will fix the problem.

alexpott’s picture

@daffie #3 fixes it for sure. That's what is in #3220021: [meta] Ensure compatibility of Drupal 9 with PHP 8.1 (as it evolves) - I did #2 to see where and why we have the issue.

daffie’s picture

The testbot returns 3,388 failures for the run with the patch from comment #2. The patch from comment #3 should therefor fix a lot of errors for PHP 8.1.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

@daffie yes we know that already. The patch in #3 is exactly what the patch on #3220021: [meta] Ensure compatibility of Drupal 9 with PHP 8.1 (as it evolves) does to fix this on PHP 8.1.

What I'm trying to explore here is what is the best fix. The first step to that it to understand why $this->value is not a string sometimes. Here's why...

Tests are failing in #2 not because of NULLs but because of integers... for example:

$variables['attributes']['data-striping'] = 1;

in template_preprocess_table() is one cause. Another is checkbox values that are often integers.

I've considered overriding the \Drupal\Core\Template\AttributeValueBase constructor with something like:

  /**
   * {@inheritdoc}
   */
  public function __construct($name, $value) {
    if (!is_string($value)) {
      @trigger_error('\Drupal\Core\Template\AttributeString should only process strings.', E_USER_DEPRECATED);
      $value = (string) $value;
    }
    parent::__construct($name, $value);
  }

This would eventually allow us to add the string typehint to $value but this would have a behaviour change side-effect of changing the type when the raw value is returned by \Drupal\Core\Template\AttributeValueBase::value() so I think we should go for the solution in #3 and cross this bridge when we think more about primitive typehints.

  • catch committed 85b3e9c on 9.3.x
    Issue #3239762 by alexpott, daffie: \Drupal\Core\Template\...
catch’s picture

Status: Reviewed & tested by the community » Fixed

#3 seems like a good choice.

Committed 85b3e9c and pushed to 9.3.x. Thanks!

Status: Fixed » Closed (fixed)

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