File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,8 @@ public function getFunctions() {
20
20
Bolt \TwigFunctions::link (),
21
21
Bolt \TwigFunctions::getSpacingScaleSequence (),
22
22
Bolt \TwigFunctions::github_url (),
23
- Bolt \TwigFunctions::inlineFile ()
23
+ Bolt \TwigFunctions::inlineFile (),
24
+ Bolt \TwigFunctions::merge_attributes ()
24
25
];
25
26
}
26
27
Original file line number Diff line number Diff line change @@ -265,6 +265,23 @@ public static function create_attribute() {
265
265
});
266
266
}
267
267
268
+ // Custom function for merging Drupal Attribute objects
269
+ // Gives $source preference, unless a key is set in both arrays and $source value is empty or null
270
+ public static function merge_attributes () {
271
+ return new Twig_SimpleFunction ('merge_attributes ' , function ($ target , $ source ) {
272
+ // For each key in $source...
273
+ foreach ($ source as $ key => $ value ) {
274
+ // If $key is not in $target, or if $key is in $target and $value in $source is empty, add/overwrite $key in $target
275
+ // NOTE: empty() and is_null() do not work in the second half of this statement. Why is that?
276
+ if (empty ($ target [$ key ]) || (!empty ($ target [$ key ]) && $ value != "" )) {
277
+ $ target [$ key ] = $ value ;
278
+ }
279
+ }
280
+
281
+ return $ target ;
282
+ });
283
+ }
284
+
268
285
public static function github_url () {
269
286
return new Twig_SimpleFunction ('github_url ' , function (\Twig_Environment $ env , $ twigPath ) {
270
287
$ filePath = TwigTools \Utils::resolveTwigPath ($ env , $ twigPath );
You can’t perform that action at this time.
0 commit comments