comparison core/lib/Drupal/Core/Template/TwigNodeTrans.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\Core\Template; 3 namespace Drupal\Core\Template;
4
5 use Twig\Node\CheckToStringNode;
4 6
5 /** 7 /**
6 * A class that defines the Twig 'trans' tag for Drupal. 8 * A class that defines the Twig 'trans' tag for Drupal.
7 * 9 *
8 * This Twig extension was originally based on Twig i18n extension. It has been 10 * This Twig extension was originally based on Twig i18n extension. It has been
16 18
17 /** 19 /**
18 * {@inheritdoc} 20 * {@inheritdoc}
19 */ 21 */
20 public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) { 22 public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) {
21 parent::__construct([ 23 $nodes['body'] = $body;
22 'count' => $count, 24 if ($count !== NULL) {
23 'body' => $body, 25 $nodes['count'] = $count;
24 'plural' => $plural, 26 }
25 'options' => $options, 27 if ($plural !== NULL) {
26 ], [], $lineno, $tag); 28 $nodes['plural'] = $plural;
29 }
30 if ($options !== NULL) {
31 $nodes['options'] = $options;
32 }
33 parent::__construct($nodes, [], $lineno, $tag);
27 } 34 }
28 35
29 /** 36 /**
30 * {@inheritdoc} 37 * {@inheritdoc}
31 */ 38 */
32 public function compile(\Twig_Compiler $compiler) { 39 public function compile(\Twig_Compiler $compiler) {
33 $compiler->addDebugInfo($this); 40 $compiler->addDebugInfo($this);
34 41
35 $options = $this->getNode('options');
36
37 list($singular, $tokens) = $this->compileString($this->getNode('body')); 42 list($singular, $tokens) = $this->compileString($this->getNode('body'));
38 $plural = NULL; 43 $plural = NULL;
39 44
40 if (NULL !== $this->getNode('plural')) { 45 if ($this->hasNode('plural')) {
41 list($plural, $pluralTokens) = $this->compileString($this->getNode('plural')); 46 list($plural, $pluralTokens) = $this->compileString($this->getNode('plural'));
42 $tokens = array_merge($tokens, $pluralTokens); 47 $tokens = array_merge($tokens, $pluralTokens);
43 } 48 }
44 49
45 // Start writing with the function to be called. 50 // Start writing with the function to be called.
65 $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); 70 $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', ');
66 } 71 }
67 $compiler->raw(')'); 72 $compiler->raw(')');
68 73
69 // Write any options passed. 74 // Write any options passed.
70 if (!empty($options)) { 75 if ($this->hasNode('options')) {
71 $compiler->raw(', ')->subcompile($options); 76 $compiler->raw(', ')->subcompile($this->getNode('options'));
72 } 77 }
73 78
74 // Write function closure. 79 // Write function closure.
75 $compiler->raw(')'); 80 $compiler->raw(')');
76 81
111 $n = $node->getNode('expr'); 116 $n = $node->getNode('expr');
112 while ($n instanceof \Twig_Node_Expression_Filter) { 117 while ($n instanceof \Twig_Node_Expression_Filter) {
113 $n = $n->getNode('node'); 118 $n = $n->getNode('node');
114 } 119 }
115 120
121 if ($n instanceof CheckToStringNode) {
122 $n = $n->getNode('expr');
123 }
116 $args = $n; 124 $args = $n;
117 125
118 // Support TwigExtension->renderVar() function in chain. 126 // Support TwigExtension->renderVar() function in chain.
119 if ($args instanceof \Twig_Node_Expression_Function) { 127 if ($args instanceof \Twig_Node_Expression_Function) {
120 $args = $n->getNode('arguments')->getNode(0); 128 $args = $n->getNode('arguments')->getNode(0);
131 case 'placeholder': 139 case 'placeholder':
132 $argPrefix = '%'; 140 $argPrefix = '%';
133 break; 141 break;
134 } 142 }
135 $args = $args->getNode('node'); 143 $args = $args->getNode('node');
144 }
145 if ($args instanceof CheckToStringNode) {
146 $args = $args->getNode('expr');
136 } 147 }
137 if ($args instanceof \Twig_Node_Expression_GetAttr) { 148 if ($args instanceof \Twig_Node_Expression_GetAttr) {
138 $argName = []; 149 $argName = [];
139 // Reuse the incoming expression. 150 // Reuse the incoming expression.
140 $expr = $args; 151 $expr = $args;