Service 'twig_extensions.twig.array' for consumer 'twig' does not implement Twig_ExtensionInterface

Created on 27 February 2019, almost 6 years ago
Updated 16 September 2024, 3 months ago

I just enabled this module in lighting distribution. After enabling, I can't clear cache using drush and unable to run update.php. Even I unable to install any other module. Please help me to figure this out.

The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'twig_extensions.twig.array' for consumer 'twig' does not implement Twig_ExtensionInterface. in Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->processServiceCollectorPass() (line 164 of /var/www/html/beusdevlight/docroot/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php).
Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->processServiceCollectorPass(Array, 'twig', Object) (Line: 97)
Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process(Object) (Line: 141)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 788)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1346)
Drupal\Core\DrupalKernel->compileContainer() (Line: 920)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 42)
Drupal\Core\Update\UpdateKernel->initializeContainer() (Line: 476)
Drupal\Core\DrupalKernel->boot() (Line: 65)
Drupal\Core\Update\UpdateKernel->handle(Object) (Line: 28)

Thanks

🐛 Bug report
Status

Closed: outdated

Version

2.0

Component

Code

Created by

🇮🇳India iyyappan.govind Chennai, India

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States loopy1492

    I was having a ton of issues with this on a site and I think I finally tracked down the problem: it was using twig/twig: ^3.

    Here is code for a twig FUNCTION that works with twig/twig 2

    my_module/src/TwigExtension/MyModuleInputTwigExtension.php:

    namespace Drupal\my_module\TwigExtension;
    
    /**
     * Custom Twig extension to format my field.
     */
    class MyModuleInputTwigExtension extends \Twig_Extension {
    
      /**
       * {@inheritdoc}
       */
      public function getFunctions() {
        return [
          new \Twig_SimpleFunction('format_hours_msg', [$this, 'formatMsg'], ['is_safe' => ['html']]),
        ];
      }
    
      /**
       * Formats a string for proper output.
       *
       * @param string $string
       *   The input string.
       *
       * @return string
       *   The formatted string.
       */
      public function formatMsg($string) {
        // string formatting code  for $string
        return $string;
      }
    }
    

    my_module.services.yml:

    services:
      my_module.function:
        class: Drupal\my_module\TwigExtension\MyModuleInputTwigExtension
        tags:
          - { name: twig.extension }

    But when I tried to use a similar method with /Twig_SimpleFilter or even /Twig_SimpleFunction and whatnot on the other site, it threw:

    Uncaught Error: Class "Twig_Extension" not found in /var/www/docroot/modules/custom/my_module/src/TwigExtension/MyModuleInputTwigExtension

    When I removed the /TwigExtension/ from the routing file, it threw:

    Service 'my_module.twig_extension' for consumer 'twig' does not implement Twig\Extension\ExtensionInterface.

    Here is code for a twig FILTER that works with twig/twig 3:

    my_module/src/TwigExtension/MyModuleInputTwigExtension.php:

    
    namespace Drupal\my_module\TwigExtension;
    
    use Twig\Extension\AbstractExtension;
    use Twig\TwigFilter;
    use Twig\TwigFunction;
    
    
    class MyModuleTwigExtension extends AbstractExtension {
      
      public function getFilters() {
        return [
          new TwigFilter('to_https', [$this, 'toHttpsFilter']),
        ];
      }
    
      public function toHttpsFilter($url) {
        // Check if the URL starts with 'http:' or 'https:' and replace it with 'https:'
        $url = preg_replace('/^(http:|https:)?/', 'https:', $url);
    
        return $url;
      }
    }
    

    I've also see the return line for getFilters() look like this:

    'to_https' => new TwigFilter('to_https', [$this, 'toHttpsFilter']),
    

    my_module.services.yml:

    services:
      my_module.twig_extension:
        class: Drupal\my_module\TwigExtension\MyModuleTwigExtension
        tags:
          - { name: twig.extension }
    
  • Status changed to Closed: outdated 3 months ago
  • 🇮🇳India ajmaltash

    Solution #20 provided by @loopy1492 has answer of the question. Basically issues could be more however the first issue is that you have to take correct class name of the Twig file. We can understand better with below code explanation:
    Below code snippet from our custom Twig file "MyCustomUrlFromUri.php" to define a twig filter

    class MyCustomUrlFromUri extends AbstractExtension {
    
      /**
       * {@inheritdoc}
       */
      public function getFilters() {
        return [
          new TwigFilter('url_from_uri', [$this, 'urlFromUri']),
        ];
      }

    And in our custom service file "my_custom.services.yml", you can see...

    my_custom.urlfromuri_twig_extension:
        class: Drupal\my_custom\Twig\MyCustomUrlFromUri
        tags:
          - { name: twig.extension } 

    So far you have noticed that this "MyCustomUrlFromUri" same at three places, so you too keep it same what ever you are taking the name.

    I hope it could help you.

  • 🇨🇦Canada joseph.olstad

    marking as "active" as per last comment.

Production build 0.71.5 2024