SvgImageUrlFormatter construct method is different between Drupal core versions - wrong order/missing parameter error

Created on 27 February 2023, over 1 year ago
Updated 28 October 2023, about 1 year ago

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.

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • @eduardo-morales-alberti opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇺🇸United States duckydan

    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.

  • 🇺🇸United States carsoncho Kansas City, MO

    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.

  • 🇦🇺Australia silverham

    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)
  • 🇦🇺Australia silverham

    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.

  • 🇦🇺Australia silverham

    Correct patch.

  • 🇵🇹Portugal Rikkarv

    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?

  • Status changed to Needs work about 1 year ago
  • 🇮🇳India tdnshah

    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).
    
    
  • Status changed to Needs review about 1 year ago
  • 🇦🇺Australia silverham

    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 🐛 SvgImageUrlFormatter parent construct missing arguments 10 needed 9 passed. Active 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.

  • 🇺🇸United States duckydan

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

  • Status changed to RTBC about 1 year ago
  • Assigned to imyaro
    • imyaro authored 72d9fb11 on 8.x-1.x
      Issue #3344624 by silverham, duckydan, imyaro: SvgImageUrlFormatter...
  • Status changed to Fixed about 1 year ago
  • Fixed. Thanks, @silverham!

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

    I will add it into the next release.

  • 🇦🇺Australia silverham

    Thank you @imyaro 😊🥳

    • imyaro authored 72d9fb11 on 3.x
      Issue #3344624 by silverham, duckydan, imyaro: SvgImageUrlFormatter...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024