Error: Class "Drupal\twig_tweak\TwigExtension" not found in Drupal\Component\DependencyInjection\Container->createService()

Created on 14 September 2023, about 1 year ago
Updated 6 January 2024, 10 months ago

Problem/Motivation

Using Drupal 9, the upgrade from version 2.10 to 3.2 causes WSOD.

Steps to reproduce

Install fresh Drupal 9
Install and enable twig_tweak 2.10 using:
composer require 'drupal/twig_tweak:^2.10'
Enable twig_tweak module
Clear all caches
Login and navigate to status page at /admin/reports/status
Notice you can view the status report successfully
Upgrade the twig tweak module to ver 3.2.1 with composer require 'drupal/twig_tweak:^3.2'
Clear all caches
Navigate to status page at /admin/reports/status
Notice the WSOD and error:

The website encountered an unexpected error. Please try again later.
Error: Class "Drupal\twig_tweak\TwigExtension" not found in Drupal\Component\DependencyInjection\Container->createService() (line 259 of core/lib/Drupal/Component/DependencyInjection/Container.php).
Drupal\Component\DependencyInjection\Container->createService(Array, 'twig_tweak.twig_extension') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('twig_tweak.twig_extension', 1) (Line: 434)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 273)
Drupal\Component\DependencyInjection\Container->createService(Array, 'twig') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('twig') (Line: 207)
Drupal::service('twig') (Line: 47)
twig_render_template('core/modules/views/templates/views-view-field.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('views_view_field', Array) (Line: 433)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 1746)
Drupal\views\Plugin\views\field\FieldPluginBase->theme(Object) (Line: 777)
Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 788)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 374)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array) (Line: 716)

Proposed resolution

Fix the code? I tried stepping through but didn't find an obvious issue. I was suspecting a missing include statement but

I was able to reproduce this on two different sites which are in the process of being upgraded from Drupal 9 to Drupal 10.
If I uninstall version 2.10, clear caches, install version 3.2.1, enable version 3.2.1, it works fine.

This makes deployment more challenging. I have to disable the module locally, commit, deploy. Then add the new module locally, enable it, commit and deploy again.

🐛 Bug report
Status

Closed: cannot reproduce

Version

3.2

Component

Code

Created by

🇺🇸United States selwynpolit

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

Comments & Activities

  • Issue created by @selwynpolit
  • 🇷🇺Russia Chi

    Clear all caches

    It's supposed to rebuild the service container. In your case it did not happen by some reason.

    That might be caused by opcache. Try to restart your server/php-fpm after Twig Tweak update.

  • Assigned to Shreya_98
  • Issue was unassigned.
  • 🇺🇸United States tommyddp

    Was having the same issue, and after my twig tweak update ran sudo service php8.0-fpm restart seemingly successfully, but the error still persists.

  • Error: Class "Drupal\twig_tweak\TwigExtension" not found in
    Drupal 10 compatible version twig_tweak module 'TwigExtension' class replace by 'TwigTweakExtension' under /src folder.
    Is there any patches are available for fix this issue ?

  • Status changed to Closed: cannot reproduce 10 months ago
  • 🇷🇺Russia Chi

    Tried to reproduce it locally. Everything worked as expected. The issue may happen with any module. You need to figure out why cache clear it not rebuilding service container on your Drupal instance.
    Possible workaround is uninstalling Twig Tweak module just before running `composer update` command.

Production build 0.71.5 2024