Commit bf7029a6 authored by catch's avatar catch
Browse files

Issue #2600154 by alexpott, Cottser, joelpittet, mfernea, slasher13, vaplas,...

Issue #2600154 by alexpott, Cottser, joelpittet, mfernea, slasher13, vaplas, aleevas, Jo Fitzgerald, skyredwang: Update our Twig code to be ready for Twig 2.x
parent 4ba4c09f
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@

namespace Drupal\Core\Template\Loader;

use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\Source;

/**
 * Loads string templates, also known as inline templates.
 *
@@ -20,7 +24,7 @@
 * @see \Drupal\Core\Render\Element\InlineTemplate
 * @see twig_render_template()
 */
class StringLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface {
class StringLoader implements \Twig_LoaderInterface, ExistsLoaderInterface, SourceContextLoaderInterface {

  /**
   * {@inheritdoc}
@@ -55,4 +59,12 @@ public function isFresh($name, $time) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getSourceContext($name) {
    $name = (string) $name;
    return new Source($name, $name);
  }

}
+14 −11
Original line number Diff line number Diff line
@@ -18,12 +18,17 @@ class TwigNodeTrans extends \Twig_Node {
   * {@inheritdoc}
   */
  public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) {
    parent::__construct([
      'count' => $count,
      'body' => $body,
      'plural' => $plural,
      'options' => $options,
    ], [], $lineno, $tag);
    $nodes['body'] = $body;
    if ($count !== NULL) {
      $nodes['count'] = $count;
    }
    if ($plural !== NULL) {
      $nodes['plural'] = $plural;
    }
    if ($options !== NULL) {
      $nodes['options'] = $options;
    }
    parent::__construct($nodes, [], $lineno, $tag);
  }

  /**
@@ -32,12 +37,10 @@ public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_N
  public function compile(\Twig_Compiler $compiler) {
    $compiler->addDebugInfo($this);

    $options = $this->getNode('options');

    list($singular, $tokens) = $this->compileString($this->getNode('body'));
    $plural = NULL;

    if (NULL !== $this->getNode('plural')) {
    if ($this->hasNode('plural')) {
      list($plural, $pluralTokens) = $this->compileString($this->getNode('plural'));
      $tokens = array_merge($tokens, $pluralTokens);
    }
@@ -67,8 +70,8 @@ public function compile(\Twig_Compiler $compiler) {
    $compiler->raw(')');

    // Write any options passed.
    if (!empty($options)) {
      $compiler->raw(', ')->subcompile($options);
    if ($this->hasNode('options')) {
      $compiler->raw(', ')->subcompile($this->getNode('options'));
    }

    // Write function closure.
+2 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ class TestExtension extends \Twig_Extension {
   */
  public function getFunctions() {
    return [
      'testfunc' => new \Twig_Function_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction']),
      new \Twig_SimpleFunction('testfunc', [$this, 'testFunction']),
    ];
  }

@@ -39,7 +39,7 @@ public function getFunctions() {
   */
  public function getFilters() {
    return [
      'testfilter' => new \Twig_Filter_Function(['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter']),
      new \Twig_SimpleFilter('testfilter', [$this, 'testFilter']),
    ];
  }

+0 −1
Original line number Diff line number Diff line
services:
  twig_extension_test.twig.test_extension:
    arguments: ['@renderer']
    class: Drupal\twig_extension_test\TwigExtension\TestExtension
    tags:
      - { name: twig.extension }
+3 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Tests\system\Functional\Theme;

use Drupal\Tests\BrowserTestBase;
use Drupal\twig_extension_test\TwigExtension\TestExtension;

/**
 * Tests Twig extensions.
@@ -28,8 +29,8 @@ protected function setUp() {
   */
  public function testTwigExtensionLoaded() {
    $twigService = \Drupal::service('twig');
    $ext = $twigService->getExtension('twig_extension_test.test_extension');
    $this->assertEqual(get_class($ext), 'Drupal\twig_extension_test\TwigExtension\TestExtension', 'TestExtension loaded successfully.');
    $ext = $twigService->getExtension(TestExtension::class);
    $this->assertInstanceOf(TestExtension::class, $ext);
  }

  /**
Loading