diff --git a/core/lib/Drupal/Core/Template/Loader/StringLoader.php b/core/lib/Drupal/Core/Template/Loader/StringLoader.php index 400bab578b..d0590a226a 100644 --- a/core/lib/Drupal/Core/Template/Loader/StringLoader.php +++ b/core/lib/Drupal/Core/Template/Loader/StringLoader.php @@ -20,7 +20,7 @@ * @see \Drupal\Core\Render\Element\InlineTemplate * @see twig_render_template() */ -class StringLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface { +class StringLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface { /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index 1caf3b4e4b..0900946027 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -17,13 +17,21 @@ class TwigNodeTrans extends \Twig_Node { /** * {@inheritdoc} */ - public function __construct(\Twig_Node $body, \Twig_Node $plural, \Twig_Node $count, \Twig_Node $options, $lineno, $tag = NULL) { - parent::__construct(array( - 'count' => $count, - 'body' => $body, - 'plural' => $plural, - 'options' => $options, - ), array(), $lineno, $tag); + public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node $options = NULL, $lineno, $tag = NULL) { + $nodes = []; + if (NULL !== $body) { + $nodes['body'] = $body; + } + if (NULL !== $count) { + $nodes['count'] = $count; + } + if (NULL !== $plural) { + $nodes['plural'] = $plural; + } + if (NULL !== $options) { + $nodes['options'] = $options; + } + parent::__construct($nodes, [], $lineno, $tag); } /** @@ -32,29 +40,27 @@ public function __construct(\Twig_Node $body, \Twig_Node $plural, \Twig_Node $co 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); } // Start writing with the function to be called. - $compiler->write('echo ' . (empty($plural) ? 't' : '\Drupal::translation()->formatPlural') . '('); + $compiler->write('echo ' . ($this->hasNode('plural') ? '\Drupal::translation()->formatPlural' : 't') . '('); // Move the count to the beginning of the parameters list. - if (!empty($plural)) { - $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), '); + if ($this->hasNode('plural')) { + $compiler->raw('abs(')->subcompile($this->hasNode('count') ? $this->getNode('count') : NULL)->raw('), '); } // Write the singular text parameter. $compiler->subcompile($singular); // Write the plural text parameter, if necessary. - if (!empty($plural)) { + if ($this->hasNode('plural')) { $compiler->raw(', ')->subcompile($plural); } @@ -67,8 +73,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. diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php index 579f2d4331..2e7c086ea3 100644 --- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php +++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php @@ -21,10 +21,10 @@ class TwigTransTokenParser extends \Twig_TokenParser { public function parse(\Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); - $body = []; - $options = []; - $count = []; - $plural = []; + $plural = NULL; + $count = NULL; + $options = NULL; + $body = NULL; if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) { $body = $this->parser->getExpressionParser()->parseExpression(); @@ -47,18 +47,6 @@ public function parse(\Twig_Token $token) { $this->checkTransString($body, $lineno); - if ($body === []) { - $body = new \Twig_Node($body); - } - if ($plural === []) { - $plural = new \Twig_Node($plural); - } - if ($count === []) { - $count = new \Twig_Node($count); - } - if ($options === []) { - $options = new \Twig_Node($options); - } $node = new TwigNodeTrans($body, $plural, $count, $options, $lineno, $this->getTag()); return $node; @@ -67,14 +55,14 @@ public function parse(\Twig_Token $token) { /** * Detect a 'plural' switch or the end of a 'trans' tag. */ - public function decideForFork($token) { + public function decideForFork(\Twig_Token $token) { return $token->test(array('plural', 'endtrans')); } /** * Detect the end of a 'trans' tag. */ - public function decideForEnd($token) { + public function decideForEnd(\Twig_Token $token) { return $token->test('endtrans'); }