-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creates add_attribute twig function #10
base: master
Are you sure you want to change the base?
Conversation
Replicates the Drupal twig function create_attribute for patternlab https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates#create_attribute
Needs improving to work with any attribute though |
Function now loops through all the attributes |
Hi @waako, I used the following to rely on Drupal's tools: <?php
/**
* @file
* Add "create_attribute" function for Pattern Lab.
*/
use \Drupal\Core\Template\Attribute;
$function = new Twig_SimpleFunction('create_attribute', function ($attributes = []) {
return new Attribute($attributes);
}); |
We do something virtually identical in our design system in order to make sure all of Drupal’s attributes functionality makes it in there (including the setAttribute, addClass, and removeClass methods, etc) https://github.com/bolt-design-system/bolt/blob/master/packages/core-php/src/TwigFunctions.php#L240 use \Drupal\Core\Template\Attribute;
//...
// Backport the native create_attribute function from Drupal to natively work in Pattern Lab
public static function create_attribute() {
return new Twig_SimpleFunction('create_attribute', function($attributes) {
return is_array($attributes) ? new Attribute($attributes) : $attributes;
});
} |
@piouPiouM @sghoweri Thanks, that seems to make sense, did not realise that could use a Drupal class inside patternlab function. |
Any way I can include this in my project already? The fix mentioned by sghoweri? |
Replicates the Drupal twig function create_attribute for patternlab
https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates#create_attribute
#8 got me thinking about this, so used existing
link
twig function to makecreate_attribute
work in Pattern Lab.Tested locally with:
which results in generated html: