1212
1313/**
1414 * @package Twig Food
15- * @author Michal Landsman <michal. landsman@burda .cz>
15+ * @author Michal Landsman <landsman@studioart .cz>
1616 */
1717class Food extends \Drupal \Core \Template \TwigExtension
1818{
@@ -22,20 +22,36 @@ class Food extends \Drupal\Core\Template\TwigExtension
2222 */
2323 protected $ entityTypeManager ;
2424
25- /**
25+
26+ /**á
2627 * @var string
2728 */
2829 protected $ themeName ;
2930
3031
31- public function __construct (RendererInterface $ renderer , UrlGeneratorInterface $ url_generator , ThemeManagerInterface $ theme_manager , DateFormatterInterface $ date_formatter , EntityTypeManagerInterface $ entity_type_manager )
32- {
32+ /**
33+ * @param RendererInterface $renderer
34+ * @param UrlGeneratorInterface $url_generator
35+ * @param ThemeManagerInterface $theme_manager
36+ * @param DateFormatterInterface $date_formatter
37+ * @param EntityTypeManagerInterface $entity_type_manager
38+ */
39+ public function __construct
40+ (
41+ RendererInterface $ renderer ,
42+ UrlGeneratorInterface $ url_generator ,
43+ ThemeManagerInterface $ theme_manager ,
44+ DateFormatterInterface $ date_formatter ,
45+ EntityTypeManagerInterface $ entity_type_manager
46+ ){
3347 $ this ->entityTypeManager = $ entity_type_manager ;
3448 $ this ->themeName = $ theme_manager ->getActiveTheme ()->getName ();
3549
50+
3651 parent ::__construct ($ renderer , $ url_generator , $ theme_manager , $ date_formatter , $ date_formatter );
3752 }
3853
54+
3955 /**
4056 * Gets a unique identifier for this Twig extension.
4157 */
@@ -44,6 +60,7 @@ public function getName()
4460 return 'twig_food.twig_extension ' ;
4561 }
4662
63+
4764 /**
4865 * Generate a list of all twig functions
4966 * @return array
@@ -62,6 +79,7 @@ public function getFunctions()
6279 ];
6380 }
6481
82+
6583 /**
6684 * Generates a list of all Twig filters that this extension defines.
6785 */
@@ -73,22 +91,41 @@ public function getFilters()
7391 ];
7492 }
7593
94+
95+ /**
96+ * Return full path used in macro, supports dynamic paths like: '@marianne/images/image.svg' for child themes
97+ * @param $string
98+ * @return string
99+ */
100+ public function themeFullPath ($ string )
101+ {
102+ $ s = explode ('/ ' , $ string );
103+ $ f = strpos ($ s [0 ], '@ ' ) !== false ? str_replace ('@ ' , '' , $ s [0 ]) : '' ;
104+ $ s [0 ] = drupal_get_path ("theme " , !empty ($ f ) ? $ f : $ this ->themeName );
105+ $ p = implode ('/ ' , $ s );
106+
107+
108+ return $ p ;
109+ }
110+
111+
76112 /**
77113 * Return SVG source code as string to Twig - usage: {{ svg('bgCarousel.svg')|raw }}
78114 * @param $path
79115 * @return string
80116 */
81117 public function renderSVG ($ path )
82118 {
83- $ theme = drupal_get_path ("theme " , $ this ->themeName );
84- $ fullPath = "{$ theme }/ {$ path }" ;
119+ $ fullPath = $ this ->themeFullPath ($ path );
85120 $ handle = fopen ($ fullPath , "r " );
86121 $ contents = fread ($ handle , filesize ($ fullPath ));
87122 fclose ($ handle );
88123
124+
89125 return $ contents ;
90126 }
91127
128+
92129 /**
93130 * Make render of var, removes html comments from string, do strip_tags, remove new lines => naked string
94131 * Example: A string which has value <!-- Start DEBUG --> ABCD <!-- End DEBUG -->
@@ -102,9 +139,11 @@ public function renderNakedField($string)
102139 $ withoutComments = preg_replace ('/<!--(.|\s)*?-->/ ' , '' , $ rendered );
103140 $ naked = strip_tags (str_replace (["\n" , "\r" ], '' , html_entity_decode ($ withoutComments , ENT_QUOTES , 'UTF-8 ' )));
104141
142+
105143 return $ naked ;
106144 }
107145
146+
108147 /**
109148 * Check string length and return him summary or in original
110149 * @param $string
@@ -124,9 +163,11 @@ public function renderWithMaxLength($string, $max = 0, $dots = true)
124163 $ string = (isset ($ items [0 ]) ? $ items [0 ] : "" ) . ($ dots ? "... " : "" );
125164 }
126165
166+
127167 return $ string ;
128168 }
129169
170+
130171 /**
131172 * Return array of selected block
132173 * @param $id string
@@ -135,9 +176,12 @@ public function renderWithMaxLength($string, $max = 0, $dots = true)
135176 public function loadBlock ($ id )
136177 {
137178 $ block = $ this ->entityTypeManager ->getStorage ('block ' )->load ($ id );
179+
180+
138181 return $ block ? $ this ->entityTypeManager ->getViewBuilder ('block ' )->view ($ block ) : '' ;
139182 }
140183
184+
141185 /**
142186 * Render region by id
143187 * @param $id
@@ -156,9 +200,11 @@ public function loadRegion($id)
156200 $ result [] = $ this ->loadBlock ($ id );
157201 }
158202
203+
159204 return $ result ;
160205 }
161206
207+
162208 /**
163209 * Prev gallery
164210 * @param $id
@@ -170,6 +216,7 @@ public function loadGalleryPrev($id, $thumbnail = 'thumbnail')
170216 return $ this ->getMediaData ($ id , '< ' , 'DESC ' , $ thumbnail );
171217 }
172218
219+
173220 /**
174221 * Next gallery
175222 * @param $id
@@ -181,6 +228,7 @@ public function loadGalleryNext($id, $thumbnail = 'thumbnail')
181228 return $ this ->getMediaData ($ id , '> ' , 'ASC ' , $ thumbnail );
182229 }
183230
231+
184232 /**
185233 * Load gallery images
186234 * @param $id
@@ -194,10 +242,10 @@ public function loadGalleryThumbs($id, $thumbnail = 'thumbnail')
194242 ->load ($ id );
195243
196244 $ images = $ gallery ->get ('field_media_images ' );
245+ $ result = [];
197246
198247 if ($ images )
199248 {
200- $ result = [];
201249 foreach ($ images as $ image )
202250 {
203251 $ mid = $ image ->entity ->id ();
@@ -211,13 +259,13 @@ public function loadGalleryThumbs($id, $thumbnail = 'thumbnail')
211259 'thumb ' => ImageStyle::load ($ thumbnail )->buildUrl ($ imageUrl ),
212260 ];
213261 }
214-
215- return $ result ;
216262 }
217263
218- return [];
264+
265+ return $ result ;
219266 }
220267
268+
221269 /**
222270 * Load main node object anywhere
223271 * @param bool|true $returnId
@@ -231,9 +279,11 @@ public function getMainNode($returnId = true)
231279 return $ returnId ? $ node ->id () : $ node ;
232280 }
233281
282+
234283 return null ;
235284 }
236285
286+
237287 /**
238288 * Load one gallery
239289 * @param $currentId
@@ -287,9 +337,11 @@ public function getMediaData($currentId, $dateComparator, $sortOrder, $thumbnail
287337 ];
288338 }
289339
340+
290341 return null ;
291342 }
292343
344+
293345 /**
294346 * @param $viewName
295347 * @param $displayId
@@ -307,7 +359,9 @@ public function viewEmbed($viewName, $displayId)
307359
308360 }
309361
362+
310363 return "Missing viewName or displayId parameter " ;
311364 }
312365
366+
313367}
0 commit comments