Skip to content

Commit 94b8263

Browse files
committed
Add Attribute object handling to PatternDataNodeVisitor
1 parent d2c9f50 commit 94b8263

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/aleksip/DataTransformPlugin/Twig/PatternDataEmbedNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public function __construct(\Twig_Node_Embed $originalNode, $data)
1818
$originalNode->getNodeTag()
1919
);
2020

21-
$this->data = $data;
21+
$this->setData($data);
2222
}
2323
}

src/aleksip/DataTransformPlugin/Twig/PatternDataIncludeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function __construct(\Twig_Node_Include $originalNode, $data)
1717
$originalNode->getNodeTag()
1818
);
1919

20-
$this->data = $data;
20+
$this->setData($data);
2121
}
2222
}

src/aleksip/DataTransformPlugin/Twig/PatternDataNodeTrait.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,57 @@
22

33
namespace aleksip\DataTransformPlugin\Twig;
44

5+
use Drupal\Core\Template\Attribute;
6+
57
trait PatternDataNodeTrait
68
{
79
protected $data;
810

11+
public function setData($data)
12+
{
13+
if (is_int($data) || is_float($data)) {
14+
if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
15+
setlocale(LC_NUMERIC, 'C');
16+
}
17+
18+
$this->data .= $data;
19+
20+
if (false !== $locale) {
21+
setlocale(LC_NUMERIC, $locale);
22+
}
23+
} elseif (null === $data) {
24+
$this->data .= 'null';
25+
} elseif (is_bool($data)) {
26+
$this->data .= ($data ? 'true' : 'false');
27+
} elseif (is_array($data)) {
28+
$this->data .= 'array(';
29+
$first = true;
30+
foreach ($data as $key => $v) {
31+
if (!$first) {
32+
$this->data .= ', ';
33+
}
34+
$first = false;
35+
$this->setData($key);
36+
$this->data .= ' => ';
37+
$this->setData($v);
38+
}
39+
$this->data .= ')';
40+
} elseif ($data instanceof Attribute) {
41+
$this->data .= 'new \Drupal\Core\Template\Attribute(';
42+
$this->setData($data->toArray());
43+
$this->data .= ')';
44+
} else {
45+
$this->data .= sprintf('"%s"', addcslashes($data, "\0\t\"\$\\"));
46+
}
47+
}
48+
949
protected function addTemplateArguments(\Twig_Compiler $compiler)
1050
{
1151
if (null === $this->getNode('variables')) {
1252
if (false === $this->getAttribute('only')) {
1353
$compiler
1454
->raw('array_merge($context, ')
15-
->repr($this->data)
55+
->raw($this->data)
1656
->raw(')')
1757
;
1858
}
@@ -22,7 +62,7 @@ protected function addTemplateArguments(\Twig_Compiler $compiler)
2262
} elseif (false === $this->getAttribute('only')) {
2363
$compiler
2464
->raw('array_merge($context, ')
25-
->repr($this->data)
65+
->raw($this->data)
2666
->raw(', ')
2767
->subcompile($this->getNode('variables'))
2868
->raw(')')

0 commit comments

Comments
 (0)