After upgrading from 8.2.7 to 8.3.0 my site throws this error:

The website encountered an unexpected error. Please try again later.
ArgumentCountError: Too few arguments to function Drupal\Core\Template\TwigExtension::__construct(), 1 passed and exactly 4 expected in Drupal\Core\Template\TwigExtension->__construct() (line 68 of core/lib/Drupal/Core/Template/TwigExtension.php).
Drupal\Core\Template\TwigExtension->__construct(Object)
ReflectionClass->newInstanceArgs(Array) (Line: 928)
Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object, 'multi_line.twig_extension') (Line: 468)
Symfony\Component\DependencyInjection\ContainerBuilder->get('multi_line.twig_extension', 1) (Line: 985)
Symfony\Component\DependencyInjection\ContainerBuilder->resolveServices(Object) (Line: 982)
Symfony\Component\DependencyInjection\ContainerBuilder->resolveServices(Array) (Line: 1142)
Symfony\Component\DependencyInjection\ContainerBuilder->callMethod(Object, Array) (Line: 946)
Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object, 'twig') (Line: 468)
Symfony\Component\DependencyInjection\ContainerBuilder->get('twig') (Line: 158)
Drupal::service('twig') (Line: 56)
twig_render_template('core/themes/classy/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 139)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 140)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 66)
Drupal\Core\Render\BareHtmlPageRenderer->renderBarePage(Array, Object, 'maintenance_page', Array) (Line: 76)
Drupal\Core\ProxyClass\Render\BareHtmlPageRenderer->renderBarePage(Array, Object, 'maintenance_page', Array) (Line: 196)
Drupal\system\Controller\DbUpdateController->handle('info', Object)
call_user_func_array(Array, Array) (Line: 110)
Drupal\Core\Update\UpdateKernel->handleRaw(Object) (Line: 73)
Drupal\Core\Update\UpdateKernel->handle(Object) (Line: 28)

Any idea what could be causing this?

Comments

gaetanpralong’s picture

Exact same thing here.
Have you found anything ?

gaetanpralong’s picture

I had a custom module that provided the template files with a custom Twig extension, and was mentioned in the ArgumentCountError exception message at the equivalent line :
Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object, 'multi_line.twig_extension') (Line: 468)

I added an emtpy constructor method to the Twig extension class mentioned in the error message like so :
public function __construct() {}

... it did the trick.
No sure why this helped but it did.

Does your class providing the 'multi_line.twig_extension' has a constructor ?

jiisuominen’s picture

We have custom TwigExtension that started to give this error.

(edit, that custom constructor didn't help.)

I have a feeling that something in the declaration of the extension has changed or in the requirements how you're supposed to configure this in the module.services.yml.

Here's my module.services.yml, if I clear this then everything works, apart from the custom twig extension that is.

crape.twig.extension:
    class: Drupal\crape\CrapeTwigExtension
    tags:
      - { name: twig.extension }
    arguments: ["@renderer"]

This did work prior to 8.3 update.

/ Janne

jiisuominen’s picture

I added another argument to service declaration and got a bit further.

  crape.twig.extension:
    class: Drupal\crape\CrapeTwigExtension
    tags:
      - { name: twig.extension }
    arguments: ["@renderer","@url_generator"]

This config gets me to following error:

ArgumentCountError: Too few arguments to function Drupal\Core\Template\TwigExtension::__construct(), 2 passed in /var/www/drupalvm/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 275 and exactly 4 expected in Drupal\Core\Template\TwigExtension->__construct() (line 68 of core/lib/Drupal/Core/Template/TwigExtension.php).
Drupal\Core\Template\TwigExtension->__construct(Object, Object) (Line: 275)
Drupal\Component\DependencyInjection\Container->createService(Array, 'crape.twig.extension') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('crape.twig.extension', 1) (Line: 497)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 332)
Drupal\Component\DependencyInjection\Container->createService(Array, 'twig') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('twig') (Line: 158)
Drupal::service('twig') (Line: 51)

/ Janne

HanPa’s picture

Found the correct arguments by putting a breakpoint on the first line of TwigExtension->__construct() and looking at the _serviceId attributes.
Note the dots on theme.manager and date.formatter:

  crape.twig.extension:
    class: Drupal\crape\CrapeTwigExtension
    tags:
      - { name: twig.extension }
    arguments: ["@renderer", "@url_generator", "@theme.manager", "@date.formatter"]

So far it seems to work.

JeffC518’s picture

Great find. I was having the same inexplicable issue when trying to create a custom filter. The debug stacktrace showed that a custom filter in the Webform module was actually creating the problem. 

thulstrup’s picture

Yes, it was a Twig extension in a custom module that was causing the error.
Thanks for helping out!