Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292064

New twig variable deprecation check should use a service and not the direct class

$
0
0

Problem/Motivation

When the TwigExtension service is overridden, #3270148: Provide a mechanism for deprecation of variables used in twig templates uses the wrong class and throws an exception:

The website encountered an unexpected error. Try again later.

Twig\Error\RuntimeError: The "Drupal\Core\Template\TwigExtension" extension is not enabled in "__string_template__d7d7f1a462505651fc222178924da388" at line 1. in Twig\ExtensionSet->getExtension() (line 73 of /var/www/html/vendor/twig/twig/src/ExtensionSet.php).
Twig\Environment->getExtension('\Drupal\Core\Template\TwigExtension') (Line: 40)

Steps to reproduce

Override the class in a custom module service provider class. Then visit any page that uses twig.

/**
 * Modifies the date_formatter service.
 */
class MyModuleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    if ($container->hasDefinition('twig.extension')) {
      $container->getDefinition('twig.extension')
        ->setClass('Drupal\my_module\Template\TwigExtension');
    }
  }

}

The overridden class can be simply defined.

<?php

namespace Drupal\my_module\Template;

use Drupal\Core\Template\TwigExtension as CoreTwigExtension;

/**
 * class TwigExtension.
 */
class TwigExtension extends CoreTwigExtension {
}

Viewing all articles
Browse latest Browse all 292064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>