The error message states that no arguments are being passed to the Twig filter extension. Drupal Twig filter extensions must be defined in [MODULE].services.yml
, and in that service definition, the services to be injected as dependencies are defined. These are the arguments that BemTwigExtension::__construct
is expecting, but not finding.
You could do something like the followingin emulsify_twig.services.yml
:
services:
emulsify_twig.bem_twig_extension:
class: Drupal\emulsify_twig\TwigExtension\BemTwigExtension
tags:
- { name: twig.extension }
arguments:
- '@renderer'
- '@url_generator'
...
Entering all the dependencies using their service definition names.
However, in the original post, as __construct
is passing the arguments to the parent, the parent service definition can be referenced to determine which arguments it expects. In the original ploblem TwigExtension is being extended, and the TwigExtension
API page lists one service as using it: twig.extension. Viewing the source on twig.extension
API page shows the services expected by the parent class (the arguments
):
class: Drupal\Core\Template\TwigExtension
arguments:
- '@renderer'
- '@url_generator'
- '@theme.manager'
- '@date.formatter'
- '@file_url_generator'
tags:
- { name: twig.extension, priority: 100 }
Then, instead of redefining all these arguments in the extended service definition, the parent can be set, using its arguments and tags as a base. So, back to emulsify_twig.services.yml
the service would be defined as follows:
services:
emulsify_twig.bem_twig_extension:
class: Drupal\emulsify_twig\TwigExtension\BemTwigExtension
parent: twig.extension
arguments:
- '@additional_class.1'
...
Where @additional_class.1
is the name of any additional services that the extended class will use, that the parent does not. Note that if the extended class does not require any additional services, the __construct()
method does not need to be implemented in the extended class at all, as the parent implementation will be used when not defined in the child class. However, the service will still need to be defined in emulsify_twig.services.yml
with the parent set as twig.extension
.