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
- Remove the redundant, untyped
protected $fileUrlGenerator;property declaration (it is already declared and assigned with the proper type in the parentImageFormatter). - Remove the custom constructor override.
- Update
ResponsiveGalleryFormatter::create()to callparent::create()and assign module-specific services (rendererandcurrent_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
| Comment | File | Size | Author |
|---|---|---|---|
| responsive_gallery_d11_4_compatibility.patch | 3.19 KB | chike |
Comments