Open
Conversation
Replicates the Drupal twig function create_attribute for patternlab https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates#create_attribute
Author
|
Needs improving to work with any attribute though |
Author
|
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;
});
} |
Author
|
@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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
linktwig function to makecreate_attributework in Pattern Lab.Tested locally with:
which results in generated html: