Problem/Motivation

On Drupal 11.4.0 and newer, loading a page or rendering a field using the responsive_gallery formatter triggers a fatal error:

Fatal error: Type of Drupal\responsive_gallery\Plugin\Field\FieldFormatter\ResponsiveGalleryFormatter::$fileUrlGenerator must be Drupal\Core\File\FileUrlGeneratorInterface (as in class Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter) in /path/to/modules/contrib/responsive_gallery/src/Plugin/Field/FieldFormatter/ResponsiveGalleryFormatter.php on line 29

Cause

In Drupal core 11.4.x, the parent class Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter adopted PHP 8 constructor property promotion with explicit type declarations:

protected FileUrlGeneratorInterface $fileUrlGenerator

ResponsiveGalleryFormatter redeclares protected $fileUrlGenerator; without a type declaration, causing a fatal type mismatch in PHP 7.4+. 2. In addition, Drupal 11.4 introduced ?ImageDerivativeUtilities $imageDerivativeUtilities as the 11th parameter in ImageFormatter::__construct(). The constructor override in ResponsiveGalleryFormatter previously forwarded $route_match as the 11th argument into parent::__construct(), which leads to a TypeError.

Proposed resolution

  1. Remove the redundant, untyped protected $fileUrlGenerator; property declaration (it is already declared and assigned with the proper type in the parent ImageFormatter).
  2. Remove the custom constructor override.
  3. Update ResponsiveGalleryFormatter::create() to call parent::create() and assign module-specific services (renderer and current_route_match) directly onto the instance:
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
  $instance->renderer = $container->get('renderer');
  $instance->routeMatch = $container->get('current_route_match');
  return $instance;
}

This prevents brittleness against future upstream changes to ImageFormatter's constructor parameters.

Remaining tasks

Review and test the attached patch.

Credits

Gemini

Comments

chike created an issue.

webengr’s picture

This patch fixed it for me, my version:

% vendor/bin/drush pm:list --filter=responsive_gallery
-------------------- ----------------------------------------- --------- ---------
Package Name Status Version
-------------------- ----------------------------------------- --------- ---------
Responsive Gallery Responsive Gallery (responsive_gallery) Enabled 1.2.2
-------------------- ----------------------------------------- --------- ---------
% vendor/bin/drush status --field=drupal-version
11.4.9