Problem/Motivation

Enabling the Flag module produces a site-wide error message: TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 in Drupal\Core\Template\TwigExtension->__construct() (line 86 of /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Template/TwigExtension.php)

Steps to reproduce

Flag 8.x-4.0-beta2 + Drupal 9.3.0-dev + PHP 7.4.20
Then enable the Flag module
Once the module is enabled:

TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 in Drupal\Core\Template\TwigExtension->__construct() (line 86 of /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Template/TwigExtension.php)

#0 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php(262): Drupal\Core\Template\TwigExtension->__construct(Object(Drupal\Core\Render\Renderer), Object(Drupal\Core\Render\MetadataBubblingUrlGenerator), Object(Drupal\Core\Theme\ThemeManager), Object(Drupal\Core\Datetime\DateFormatter), Object(Drupal\flag\FlagCountManager))
#1 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'flag.twig.count')
#2 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get('flag.twig.count', 1)
#3 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)
#4 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'twig')
#5 /var/www/drupalvm/drupal/web/core/lib/Drupal.php(198): Drupal\Component\DependencyInjection\Container->get('twig')
#6 /var/www/drupalvm/drupal/web/core/themes/engines/twig/twig.engine(57): Drupal::service('twig')
#7 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('core/modules/im...', Array)
#8 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(436): Drupal\Core\Theme\ThemeManager->render('image_formatter', Array)
#9 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(449): Drupal\Core\Render\Renderer->doRender(Array)
#10 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, true)
#11 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(157): Drupal\Core\Render\Renderer->render(Array, true)
#12 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(578): Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}()
#13 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(158): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#14 /var/www/drupalvm/drupal/web/modules/contrib/token/token.tokens.inc(1656): Drupal\Core\Render\Renderer->renderPlain(Array)
#15 [internal function]: field_tokens('entity', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#16 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('field_tokens', Array)
#17 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Utility/Token.php(304): Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array)
#18 /var/www/drupalvm/drupal/web/modules/contrib/token/token.tokens.inc(958): Drupal\Core\Utility\Token->generate('entity', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#19 [internal function]: token_tokens('node', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#20 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('token_tokens', Array)
#21 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Utility/Token.php(304): Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array)
#22 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Utility/Token.php(196): Drupal\Core\Utility\Token->generate('node', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#23 /var/www/drupalvm/drupal/web/modules/contrib/metatag/src/MetatagToken.php(66): Drupal\Core\Utility\Token->replace('[node:field_ima...', Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#24 /var/www/drupalvm/drupal/web/modules/contrib/metatag/src/MetatagManager.php(577): Drupal\metatag\MetatagToken->replace('[node:field_ima...', Array, Array, NULL)
#25 /var/www/drupalvm/drupal/web/modules/contrib/metatag/src/MetatagManager.php(493): Drupal\metatag\MetatagManager->generateRawElements(Array, Object(Drupal\node\Entity\Node))
#26 /var/www/drupalvm/drupal/web/modules/contrib/metatag/metatag.module(528): Drupal\metatag\MetatagManager->generateElements(Array, Object(Drupal\node\Entity\Node))
#27 /var/www/drupalvm/drupal/web/modules/contrib/metatag/metatag.module(297): metatag_get_tags_from_route()
#28 /var/www/drupalvm/drupal/web/modules/contrib/metatag/metatag.module(243): _metatag_remove_duplicate_entity_tags(Array)
#29 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(539): metatag_entity_view_alter(Array, Object(Drupal\node\Entity\Node), Object(Drupal\Core\Entity\Entity\EntityViewDisplay))
#30 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(305): Drupal\Core\Extension\ModuleHandler->alter('node_view', Array, Object(Drupal\node\Entity\Node), Object(Drupal\Core\Entity\Entity\EntityViewDisplay))
#31 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Entity/EntityViewBuilder.php(239): Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array)
#32 [internal function]: Drupal\Core\Entity\EntityViewBuilder->build(Array)
#33 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(101): call_user_func_array(Array, Array)
#34 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(786): Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_ren...', 'exception', 'Drupal\\Core\\Ren...')
#35 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(377): Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array)
#36 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)
#37 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(241): Drupal\Core\Render\Renderer->render(Array, false)
#38 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(578): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#39 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(242): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#40 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(132): Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#41 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#42 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#43 /var/www/drupalvm/drupal/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#44 /var/www/drupalvm/drupal/vendor/symfony/http-kernel/HttpKernel.php(163): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view')
#45 /var/www/drupalvm/drupal/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#46 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#47 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#48 /var/www/drupalvm/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#49 /var/www/drupalvm/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#50 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#51 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#52 /var/www/drupalvm/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#53 /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#54 /var/www/drupalvm/drupal/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#55 {main}

Proposed resolution

As Twig_SimpleFunction is deprecated, use TwigFunction instead.
Instead of extending TwigExtension, extend Twig\Extension\AbstractExtension.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gombi created an issue. See original summary.

gombi’s picture

Adding a patch to resolve the issue.

gombi’s picture

Status: Needs review » Closed (outdated)

The dev branch already fixes this issue.

mxmilkiib’s picture

Version: 8.x-4.0-beta2 » 8.x-4.x-dev
Category: Bug report » Support request
Status: Closed (outdated) » Active

Sorry if I'm being a noob, but I'm apparently getting this still with -dev;

root@vultr:/var/www/html# composer require 'drupal/flag:4.x-dev@dev'
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
./composer.json has been updated
Loading composer repositories with package information
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing drupal/flag (dev-4.x 2bfff5b): Cloning 2bfff5b3fe from cache
Package doctrine/reflection is abandoned, you should avoid using it. Use roave/better-reflection instead.
Package sensiolabs/security-checker is abandoned, you should avoid using it. Use https://github.com/fabpot/local-php-security-checker instead.
Package webmozart/path-util is abandoned, you should avoid using it. Use symfony/filesystem instead.
Writing lock file
Generating autoload files
Hardening vendor directory with .htaccess and web.config files.
53 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Cleaning installed packages.
root@vultr:/var/www/html# drush cr

PHP Fatal error:  Uncaught TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 and defined in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php:86
Stack trace:
#0 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(262): Drupal\Core\Template\TwigExtension->__construct()
#1 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService()
#2 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get()
#3 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters()
#4 /var/www/html/core/lib/Drupal/Comp in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php on line 86

Fatal error: Uncaught TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 and defined in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php:86
Stack trace:
#0 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(262): Drupal\Core\Template\TwigExtension->__construct()
#1 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService()
#2 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get()
#3 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters()
#4 /var/www/html/core/lib/Drupal/Comp in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php on line 86
 [warning] Drush command terminated abnormally.

Might I be missing something? Thanks.

mxmilkiib’s picture

Hmm, on trying to uninstall;

root@vultr:/var/www/html# composer remove drupal/flag
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Loading composer repositories with package information
Warning from https://repo.packagist.org: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2. See https://blog.packagist.com/deprecating-composer-1-support/
Updating dependencies (including require-dev)
Package operations: 0 installs, 0 updates, 1 removal
  - Removing drupal/flag (dev-4.x)
Deleting modules/contrib/flag - deleted
Package doctrine/reflection is abandoned, you should avoid using it. Use roave/better-reflection instead.
Package sensiolabs/security-checker is abandoned, you should avoid using it. Use https://github.com/fabpot/local-php-security-checker instead.
Package webmozart/path-util is abandoned, you should avoid using it. Use symfony/filesystem instead.
Writing lock file
Generating autoload files
Hardening vendor directory with .htaccess and web.config files.
53 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Cleaning installed packages.
root@vultr:/var/www/html# drush cr
PHP Fatal error:  Uncaught TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 and defined in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php:86
Stack trace:
#0 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(262): Drupal\Core\Template\TwigExtension->__construct()
#1 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService()
#2 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get()
#3 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters()
#4 /var/www/html/core/lib/Drupal/Comp in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php on line 86

Fatal error: Uncaught TypeError: Argument 5 passed to Drupal\Core\Template\TwigExtension::__construct() must implement interface Drupal\Core\File\FileUrlGeneratorInterface or be null, instance of Drupal\flag\FlagCountManager given, called in /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php on line 262 and defined in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php:86
Stack trace:
#0 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(262): Drupal\Core\Template\TwigExtension->__construct()
#1 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService()
#2 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get()
#3 /var/www/html/core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters()
#4 /var/www/html/core/lib/Drupal/Comp in /var/www/html/core/lib/Drupal/Core/Template/TwigExtension.php on line 86
 [warning] Drush command terminated abnormally.
Berdir’s picture

If the code is still there after a composer remove then you have an older version of the module installed manually somewhere. Remove that.

Note that you must not remove a module with composer without before uninstalling it properly through the UI or drush. In this case, what you want to do is search for flag.info.yml or a flag folder anywhere, remove that, and then add it back with composer. You do not need the dev version, there is a current release with this fix.

mxmilkiib’s picture

Status: Active » Fixed

Thanks! I had forgot the right way to remove a module, so flipped a coin n got it wrong ;)

FernandaPorto’s picture

This patch worked for me! Thanks!

Pamela Sales’s picture

Thanks! This patch is working for me as of December 2021.

Berdir’s picture

You don't need this patch if you use the most recent flag module version.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.