1

I made a custom module that extends TwigExtension, where I define all sort of custom twig filters. It worked well until the update to 8.3, where TwigExtension changed slightly:

/**
* Constructs \Drupal\Core\Template\TwigExtension.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
*   The renderer.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
*   The URL generator.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
*   The theme manager.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
*   The date formatter.
*/
public function __construct(RendererInterface $renderer, 
  UrlGeneratorInterface $url_generator, ThemeManagerInterface 
  $theme_manager, DateFormatterInterface $date_formatter) {
  $this->renderer = $renderer;
  $this->urlGenerator = $url_generator;
  $this->themeManager = $theme_manager;
  $this->dateFormatter = $date_formatter;
}

My service yaml looked like this:

services:
  custom_twig.twig.extension:
    arguments: ['@renderer']
class: Drupal\custom_twig\TwigExtension\LabTwigExtension
tags:
  - { name: twig.extension }

And I changed the services to have the new required ones.

services:
  custom_twig.twig.extension:
    arguments: ['@renderer', '@url_generator', '@theme.manager','@date.formatter']
class: Drupal\custom_twig\TwigExtension\LabTwigExtension
tags:
  - { name: twig.extension }

Although I set the correct arguments, Drupal throws an error:

Recoverable fatal error: Argument 2 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\Routing\UrlGeneratorInterface, none given, called in /A

I am fairly certain that url_generator implement UrlGeneratorInterface. Is there anything else I got wrong?

  • Surprise!!! welcome to the new world of semantic versioning. – No Sssweat Apr 11 '17 at 9:36
  • Without seeing the code of the constructor for your class, we cannot point you to the error. If I understand you correctly, you just show the constructor of the base class. – kiamlaluno Apr 11 '17 at 14:45
  • As side note, your class constructor should call the constructor of the base class. – kiamlaluno Apr 11 '17 at 14:48
0

If you are using Twig Extender , try updating it to version 8.x-2.0-rc1. It worked for me

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.