2
2
3
3
namespace aleksip \DataTransformPlugin \Twig ;
4
4
5
+ use Drupal \Core \Template \Attribute ;
6
+
5
7
trait PatternDataNodeTrait
6
8
{
7
9
protected $ data ;
8
10
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
+
9
49
protected function addTemplateArguments (\Twig_Compiler $ compiler )
10
50
{
11
51
if (null === $ this ->getNode ('variables ' )) {
12
52
if (false === $ this ->getAttribute ('only ' )) {
13
53
$ compiler
14
54
->raw ('array_merge($context, ' )
15
- ->repr ($ this ->data )
55
+ ->raw ($ this ->data )
16
56
->raw (') ' )
17
57
;
18
58
}
@@ -22,7 +62,7 @@ protected function addTemplateArguments(\Twig_Compiler $compiler)
22
62
} elseif (false === $ this ->getAttribute ('only ' )) {
23
63
$ compiler
24
64
->raw ('array_merge($context, ' )
25
- ->repr ($ this ->data )
65
+ ->raw ($ this ->data )
26
66
->raw (', ' )
27
67
->subcompile ($ this ->getNode ('variables ' ))
28
68
->raw (') ' )
0 commit comments