Class "Twig_Extension" not found

Created on 17 July 2023, over 1 year ago
Updated 23 August 2024, 3 months ago

Problem/Motivation

Got this error on instalation
Error: Class "Twig_Extension" not found in include() (line 14 of /var/www/docroot/modules/contrib/menu_tree/src/TwigExtension/MenuTreeTwigExtension.php) #0 /var/www/vendor/composer/ClassLoader.php(578): include()

Steps to reproduce

Drupal version 10.0.10, php 8.1.14
Just copy and install module (manually or via composer).

Proposed resolution

I did fix it locally

<?php

namespace Drupal\menu_tree\TwigExtension;

use Drupal\menu_tree\MenuTreeItems;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
 * Class TwigExtension.
 *
 * @package Drupal\menu_tree
 */
class MenuTreeTwigExtension extends AbstractExtension {

  /**
   * Items definition.
   *
   * @var \Drupal\menu_tree\MenuTreeItems
   */
  protected $items;

  /**
   * MenuItemsTwigExtension constructor.
   *
   * @param \Drupal\menu_tree\MenuTreeItems $item
   *   The MenuItems service.
   */
  public function __construct(MenuTreeItems $item) {
    $this->items = $item;
  }

  /**
   * {@inheritdoc}
   */
  public function getFunctions(): array {
    return [
      new TwigFunction('menu_tree',
        function ($menu_id = NULL, $exclude = NULL) {
          return $this->items->getMenuTree($menu_id, $exclude);
        },
        ['is_safe' => ['html']]
      ),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName(): string {
    return 'menu_tree';
  }

}

Also, I found some issues related to code standarts (dependicies, descriptions, etc) and issue of hook order.
Thanks.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇦Ukraine t1mm1 Kiev

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

Comments & Activities

  • Issue created by @t1mm1
  • Status changed to Needs review over 1 year ago
  • 🇺🇦Ukraine t1mm1 Kiev

    The patch

  • 🇮🇳India Akram Khan Cuttack, Odisha

    I reviewed the patch and it's resolved the issue. Before patch when i try to extend the module it gives Error and after applying patch the module got enabled successfully. Additionally, I have attached before and after state screenshots for reference.

  • Assigned to Jay Jangid
  • Issue was unassigned.
  • 🇮🇳India Jay Jangid

    I have reviewed and implemented the Patch provided by @t1mm1.
    The Patch has been implemented successfully.
    Result:- "Class 'Twig_Extension' not found", error resolved & module installed.
    I am sharing the before & after state in my screenshot for your reference.
    So we can move it to RTBC +.
    Thank You.

  • Assigned to peter törnstrand
  • Status changed to RTBC 12 months ago
  • 🇸🇪Sweden peter törnstrand

    Thanks, will get this commited asap.

  • 🇲🇽Mexico alff331

    Hola, tengo este error muy similar al anterior alguien me puede ayudar. Estoy ejecutando local.

    Estoy migrando de 8.9.6 a 10.3.1

    Error: Class "Twig_Extension" not found in include() (line 15 of C:\xampp\htdocs\modules\contrib\devel\src\Twig\Extension\Debug.php).
    include() (Line: 576)
    Composer\Autoload\{closure}('C:\xampp\htdocs/modules/contrib/devel/src\Twig\Extension\Debug.php') (Line: 427)
    Composer\Autoload\ClassLoader->loadClass('Drupal\devel\Twig\Extension\Debug')
    is_a('Drupal\devel\Twig\Extension\Debug', 'Twig\Extension\ExtensionInterface', 1) (Line: 183)
    Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->processServiceCollectorPass(Array, 'twig', Object) (Line: 118)
    Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process(Object) (Line: 80)
    Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 767)
    Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1447)
    Drupal\Core\DrupalKernel->compileContainer() (Line: 971)
    Drupal\Core\DrupalKernel->initializeContainer() (Line: 42)
    Drupal\Core\Update\UpdateKernel->initializeContainer() (Line: 515)
    Drupal\Core\DrupalKernel->boot() (Line: 65)
    Drupal\Core\Update\UpdateKernel->handle(Object) (Line: 27)

    • 95fa0673 committed on 1.0.x
      Issue #3375046 by t1mm1, dineshkumarbollu: Class "Twig_Extension" not...
  • Status changed to Fixed 3 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024