From 226dc8a6def7e40c23949d07e9e44c26176c1ea0 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 19 Mar 2018 23:00:28 +0100 Subject: [PATCH 1/3] Creates add_attribute twig function Replicates the Drupal twig function create_attribute for patternlab https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates#create_attribute --- dist/functions/create_attribute.function.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dist/functions/create_attribute.function.php diff --git a/dist/functions/create_attribute.function.php b/dist/functions/create_attribute.function.php new file mode 100644 index 0000000..72e0874 --- /dev/null +++ b/dist/functions/create_attribute.function.php @@ -0,0 +1,12 @@ + array('html')) +); \ No newline at end of file From ff39b8fe14ce78afd08154fa2ad8dfb407b5cb42 Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Tue, 20 Mar 2018 13:07:59 +0100 Subject: [PATCH 2/3] Loops through each attribute --- dist/functions/create_attribute.function.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dist/functions/create_attribute.function.php b/dist/functions/create_attribute.function.php index 72e0874..a13b07f 100644 --- a/dist/functions/create_attribute.function.php +++ b/dist/functions/create_attribute.function.php @@ -3,9 +3,8 @@ $function = new Twig_SimpleFunction( 'create_attribute', function ($attributes = []) { - if (isset($attributes) && isset($attributes['class'])) { - $classes = join(' ', $attributes['class']); - return ' class="' . $classes .'"'; + foreach ($attributes as $key => $value) { + print ' ' . $key . '="' . join(' ', $value) . '"'; } }, array('is_safe' => array('html')) From 287b04b20e091cd17b58257e77a4908026c14b2b Mon Sep 17 00:00:00 2001 From: Kent Richards Date: Sun, 29 Dec 2019 21:21:43 -0800 Subject: [PATCH 3/3] Use \Drupal\Core\Template\Attribute for create_attribute Uses `\Drupal\Core\Template\Attribute` as the basis for `create_attribute()`. See: https://github.com/pattern-lab/plugin-drupal-twig-components/pull/10#issuecomment-403172065 --- dist/functions/create_attribute.function.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dist/functions/create_attribute.function.php b/dist/functions/create_attribute.function.php index a13b07f..addc45a 100644 --- a/dist/functions/create_attribute.function.php +++ b/dist/functions/create_attribute.function.php @@ -1,11 +1,11 @@ $value) { - print ' ' . $key . '="' . join(' ', $value) . '"'; - } - }, - array('is_safe' => array('html')) -); \ No newline at end of file +use \Drupal\Core\Template\Attribute; + +$function = new Twig_SimpleFunction('create_attribute', function ($attributes = []) { + return new Attribute($attributes); +});