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

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Eduardo Morales Alberti’s picture

Status: Needs work » Needs review
duckydan’s picture

I had to change lines 37-43 of svg_image/src/Plugin/Field/FieldFormatter/SvgImageUrlFormatter.php

to

/**
 * {@inheritdoc}
 */
public function __construct($pluginId, $pluginDefinition, FieldDefinitionInterface $fieldDefinition, array $settings, $label, $viewMode, array $thirdPartySettings, EntityStorageInterface $ImageStyleStorage, AccountInterface $currentUser, FileUrlGeneratorInterface $fileUrlGenerator) {
  parent::__construct($pluginId, $pluginDefinition, $fieldDefinition, $settings, $label, $viewMode, $thirdPartySettings, $currentUser, $ImageStyleStorage);
  $this->fileUrlGenerator = $fileUrlGenerator;
}

Then it works fine.

carsoncho’s picture

I'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.

silverham’s picture

This error is also preventing form from on saving my Drupal views. Please commit fix.

Views error:

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 /app/build/web/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('image_url', Array, Object, Array, '', '_custom', Array, Object, Object) (Line: 41)
Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageUrlFormatter->__construct('image_url', Array, Object, Array, '', '_custom', Array, Object, Object, Object) (Line: 59)
Drupal\svg_image\Plugin\Field\FieldFormatter\SvgImageUrlFormatter::create(Object, Array, 'image_url', Array) (Line: 64)
Drupal\Core\Field\FormatterPluginManager->createInstance('image_url', Array) (Line: 126)
Drupal\Core\Field\FormatterPluginManager->getInstance(Array) (Line: 1002)
Drupal\views\Plugin\views\field\EntityField->getFormatterInstance() (Line: 1020)
Drupal\views\Plugin\views\field\EntityField->calculateDependencies() (Line: 71)
Drupal\views\Plugin\views\display\DisplayPluginBase->getPluginDependencies(Object) (Line: 89)
Drupal\views\Plugin\views\display\DisplayPluginBase->calculatePluginDependencies(Object, 17)
array_walk(Array, Array) (Line: 959)
Drupal\views\Plugin\views\display\DisplayPluginBase->calculateDependencies() (Line: 71)
Drupal\Core\Config\Entity\ConfigEntityBase->getPluginDependencies(Object) (Line: 89)
Drupal\Core\Config\Entity\ConfigEntityBase->calculatePluginDependencies(Object) (Line: 282)
Drupal\views\Entity\View->calculateDependencies() (Line: 319)
Drupal\Core\Config\Entity\ConfigEntityBase->preSave(Object) (Line: 292)
Drupal\views\Entity\View->preSave(Object) (Line: 562)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 517)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 253)
Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 607)
Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 991)
Drupal\views_ui\ViewUI->save() (Line: 337)
Drupal\views_ui\ViewEditForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('view_edit_form', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 230)
Drupal\views_ui\Controller\ViewsUIController->edit(Object, NULL)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 270)
Drupal\shield\ShieldMiddleware->bypass(Object, 1, 1) (Line: 137)
Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
silverham’s picture

Actually 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.

silverham’s picture

Rikkarv’s picture

On 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?

tdnshah’s picture

Status: Needs review » Needs work

The patches are not resolving the issue when upgraded to D10, In D10 i am getting the below error

ArgumentCountError: 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 and exactly 10 expected in Drupal\image\Plugin\Field\FieldFormatter\ImageUrlFormatter->__construct() (line 74 of core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php).

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.

tdnshah’s picture

Status: Needs work » Needs review
silverham’s picture

Attaching a new patch that does not override the __construct() method, nor does it define the specific parameter order in its own create() 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 parent create() method.

silverham’s picture

Title: SvgImageUrlFormatter parent construct wrong order » SvgImageUrlFormatter construct method issues between Drupal versions - wrong order/missing parameter
Version: 3.0.1 » 3.x-dev
Issue summary: View changes
silverham’s picture

Title: SvgImageUrlFormatter construct method issues between Drupal versions - wrong order/missing parameter » SvgImageUrlFormatter construct method is different between Drupal core versions - wrong order/missing parameter error
duckydan’s picture

The patch in #12 fixed it for me. Thank you!

duckydan’s picture

Status: Needs review » Reviewed & tested by the community
imyaro’s picture

Assigned: Unassigned » imyaro

  • imyaro authored 72d9fb11 on 8.x-1.x
    Issue #3344624 by silverham, duckydan, imyaro: SvgImageUrlFormatter...
imyaro’s picture

Status: Reviewed & tested by the community » Fixed

Fixed. Thanks, @silverham!

I've also did it for all the Image Formatters.

I will add it into the next release.

silverham’s picture

Thank you @imyaro 😊🥳

  • imyaro authored 72d9fb11 on 3.x
    Issue #3344624 by silverham, duckydan, imyaro: SvgImageUrlFormatter...

Status: Fixed » Closed (fixed)

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