Problem/Motivation
Drupal core has different construction parameters depending on whther you on Drupal core =< 9.3, => 9.4 or even Drupal 10.
Steps to reproduce
In Drupal 10, SvgImageUrlFormatter parent construct missing arguments 10 needed 9 passed.
Too few arguments to function Drupal\image\Plugin\Field\FieldFormatter\ImageUrlFormatter::__construct(), 9 passed in /var/www/html/docroot/modules/contrib/svg_image/src/Plugin/Field/FieldFormatter/SvgImageUrlFormatter.php on line 41
In Drupal 9.4, SvgImageUrlFormatter parent construct does not match with ImageFormatter because of the argument current_user.
The website encountered an unexpected error. Please try again later.
TypeError: Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter::__construct(): Argument #8 ($current_user) must be of type Drupal\Core\Session\AccountInterface, Drupal\image\ImageStyleStorage given, called in /var/www/html/docroot/modules/contrib/svg_image/src/Plugin/Field/FieldFormatter/SvgImageUrlFormatter.php on line 41 in Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->__construct() (line 78 of core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php).
Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->__construct() (Line: 41)
Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageUrlFormatter->__construct() (Line: 59)
Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageUrlFormatter::create() (Line: 64)
Drupal\Core\Field\FormatterPluginManager->createInstance() (Line: 126)
Drupal\Core\Field\FormatterPluginManager->getInstance() (Line: 207)
Drupal\Core\Entity\Entity\EntityViewDisplay->getRenderer() (Line: 238)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple() (Line: 340)
Drupal\Core\Entity\EntityViewBuilder->buildComponents() (Line: 282)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple() (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build()
call_user_func_array() (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 772)
Drupal\Core\Render\Renderer->doCallback() (Line: 363)
Drupal\Core\Render\Renderer->doRender() (Line: 201)
Drupal\Core\Render\Renderer->render() (Line: 479)
[...]
Proposed resolution
Do not override __construct() method and reuse the parent create() method, then assign property after object creation so it will be compatible with future releases.
Issue fork svg_image-3344624
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:
- 3344624-svgimageurlformatter-parent-construct changes, plain diff MR !17
Comments
Comment #3
Eduardo Morales AlbertiComment #4
duckydan CreditAttribution: duckydan as a volunteer commentedI had to change lines 37-43 of svg_image/src/Plugin/Field/FieldFormatter/SvgImageUrlFormatter.php
to
Then it works fine.
Comment #5
carsoncho CreditAttribution: carsoncho at Forum One commentedI'm running into the exact same issue using Drupal core 9.4.12 and svg_image 3.0.1. Here's a patch based off the MR.
Comment #6
silverham CreditAttribution: silverham at EY Digital commentedThis error is also preventing form from on saving my Drupal views. Please commit fix.
Views error:
Comment #7
silverham CreditAttribution: silverham at EY Digital commentedActually this issue only appears in Drupal 9.4.x. In Drupal 9.5.x/10.x/11.x, the order is correct. Attaching patch to detect version.
Comment #8
silverham CreditAttribution: silverham at EY Digital commentedCorrect patch.
Comment #9
Rikkarv CreditAttribution: Rikkarv commentedOn https://www.drupal.org/project/svg_image/releases/3.0.1 it says that "Works with Drupal: ^9.3 || ^10".
Shouldn't this be replaced with ^9.5 instead of ^9.3?
Comment #10
tdnshah CreditAttribution: tdnshah as a volunteer and at Acquia commentedThe patches are not resolving the issue when upgraded to D10, In D10 i am getting the below error
The issue i was facing was not exactly this one so have created an seprate issue and have added a patch for it over https://www.drupal.org/project/svg_image/issues/3388971..
Sorry if this created a little junk, I feel this both are related though, movings this back to need review as I havent test the patches with D9 as mentioned above.
Comment #11
tdnshah CreditAttribution: tdnshah as a volunteer and at Acquia commentedComment #12
silverham CreditAttribution: silverham at EY Digital commentedAttaching a new patch that does not override the
__construct()
method, nor does it define the specific parameter order in its owncreate()
method.Rather, it assigns the required extra class property after the class creation by overriding
create()
method, calling the parent create method, then assigning the paramater, so then it is compatible with all versions of Drupal core including Drupal 10. (Thanks to comment #3388971-3: SvgImageUrlFormatter parent construct missing arguments 10 needed 9 passed. for the pointer).I am closing the duplicate issue in #3388971: SvgImageUrlFormatter parent construct missing arguments 10 needed 9 passed. as this patch fixes both issues. Additionally, the reason why the initial patch in the linked ticket does not work is because it defines the parameter order in it's
create()
method, rather reusing the parentcreate()
method.Comment #13
silverham CreditAttribution: silverham at EY Digital commentedComment #14
silverham CreditAttribution: silverham at EY Digital commentedComment #15
duckydan CreditAttribution: duckydan as a volunteer commentedThe patch in #12 fixed it for me. Thank you!
Comment #16
duckydan CreditAttribution: duckydan as a volunteer commentedComment #17
imyaro CreditAttribution: imyaro at Attico International commentedComment #19
imyaro CreditAttribution: imyaro at Attico International commentedFixed. Thanks, @silverham!
I've also did it for all the Image Formatters.
I will add it into the next release.
Comment #20
silverham CreditAttribution: silverham at EY Digital commentedThank you @imyaro 😊🥳