Problem/Motivation

After updating Drupal core to 11.4, every page that renders an image field
using the "Image Field Tokens" formatter fails with a WSOD / HTTP 500:

PHP Fatal error: Type of
Drupal\imagefield_tokens\Plugin\Field\FieldFormatter\ImageFieldTokensFormatter::$currentUser
must be Drupal\Core\Session\AccountInterface (as in class
Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter) in
web/modules/contrib/imagefield_tokens/src/Plugin/Field/FieldFormatter/ImageFieldTokensFormatter.php
on line 34

`ImageFieldTokensFormatter` re-declares two properties that are inherited
from core's `ImageFormatter`:

/**
 * The current user.
 *
 * @var \Drupal\Core\Session\AccountInterface
 */
protected $currentUser;

/**
 * The image style entity storage.
 *
 * @var \Drupal\image\ImageStyleStorageInterface
 */
protected $imageStyleStorage;

Drupal 11.4 added native type declarations to these properties in core's
`ImageFormatter`. Since PHP property types are invariant, the untyped
re-declarations in the child class now cause a fatal error as soon as the
class is loaded. The fatal is currently reported for `$currentUser`; once
that is fixed, `$imageStyleStorage` will trigger the same error.

Steps to reproduce

  1. Install Drupal core 11.4 with imagefield_tokens 3.2.0
  2. Configure an image field on any content type to use the
    "Image Field Tokens" formatter on Manage display.
  3. Visit a page that renders this field.
  4. HTTP 500 with the fatal error above.

Proposed resolution

Remove both redundant property declarations from
`ImageFieldTokensFormatter`. They are inherited from the parent class and
are already assigned via `parent::__construct()`, so no other changes are
required.

Because the properties are removed rather than typed, the fix remains
backwards compatible with Drupal 11.3 and 10.x.

Remaining tasks

  • Review MR.
  • Commit and tag a new release.
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

gemisky created an issue. See original summary.

gemisky changed the visibility of the branch 8.x-2.x to hidden.

jstoller’s picture

Status: Active » Reviewed & tested by the community

Works as advertised, and much appreciated.

deciphered made their first commit to this issue’s fork.

  • deciphered committed 33c0c488 on 3.0.x
    fix(#3607919): full Drupal 11.4 compatibility and code modernization
    
deciphered’s picture

Status: Reviewed & tested by the community » Fixed

Thanks @gemisky for the report and initial fix, and @jstoller for testing.

Committed and expanded on the fix with a broader code modernisation pass for full Drupal 11.4 compatibility:

  • Removed redundant $currentUser and $imageStyleStorage property declarations from ImageFieldTokensFormatter
  • Applied the same fix to ColorboxFormatter, ImageFieldTokensCropWidget, and ImageFieldTokensWigdet
  • Modernised the codebase with native type declarations and constructor property promotion
  • Added PHPStan configuration
  • Updated functional tests

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.