summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-08-12 03:14:21 (GMT)
committerAlex Pott2014-08-12 03:14:21 (GMT)
commit3f3fc8db8ff8c498e45c8ab4f874be0ec84be7ec (patch)
tree588c4f7214aeb1d6f2dfb37f4fb2e270be3f2a91
parentb366d7d7982eeaaa3f7cf5bf23b66384f623a1a7 (diff)
Issue #2283301 by Dave Reid, davidhernandez, Cottser, joelpittet: Added Twig filters for drupal_html_class() and drupal_clean_id_identifier().
-rw-r--r--core/lib/Drupal/Core/Template/TwigExtension.php4
-rw-r--r--core/modules/system/src/Tests/Theme/TwigFilterTest.php8
-rw-r--r--core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig3
3 files changed, 12 insertions, 3 deletions
diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php
index 05c7390..cf3500e 100644
--- a/core/lib/Drupal/Core/Template/TwigExtension.php
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -82,6 +82,10 @@ class TwigExtension extends \Twig_Extension {
// Array filters.
new \Twig_SimpleFilter('without', 'twig_without'),
+
+ // CSS class and ID filters.
+ new \Twig_SimpleFilter('clean_class', 'drupal_html_class'),
+ new \Twig_SimpleFilter('clean_id', 'drupal_clean_id_identifier'),
);
}
diff --git a/core/modules/system/src/Tests/Theme/TwigFilterTest.php b/core/modules/system/src/Tests/Theme/TwigFilterTest.php
index c22be7f..5665a4c 100644
--- a/core/modules/system/src/Tests/Theme/TwigFilterTest.php
+++ b/core/modules/system/src/Tests/Theme/TwigFilterTest.php
@@ -104,13 +104,17 @@ class TwigFilterTest extends WebTestBase {
'message' => 'Without string attribute in the front.',
),
array(
- 'expected' => '<div><span checked>Without either nor class attributes.</span></div>',
- 'message' => 'Attributes printed without id nor class attributes.',
+ 'expected' => '<div><span checked>Without id and class attributes.</span></div>',
+ 'message' => 'Attributes printed without id and class attributes.',
),
array(
'expected' => '<div><span id="quotes" checked class="red green blue">All attributes again.</span></div>',
'message' => 'All attributes printed again.',
),
+ array(
+ 'expected' => '<div id="quotes-here"><span class="gray-like-a-bunny bem__ized--top-feature" id="quotes-here">ID and class. Having the same ID twice is not valid markup but we want to make sure the filter doesn\'t use drupal_html_id().</span></div>',
+ 'message' => 'Class and ID filtered.',
+ ),
);
foreach ($elements as $element) {
diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig
index 28e5c15..56fc3f1 100644
--- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig
+++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.filter.html.twig
@@ -18,5 +18,6 @@
<div><span class="{{ attributes.class }}">Class attributes only:</span></div>
<div><span {{ attributes.checked }}{{ attributes|without('checked') }}>Without boolean attribute.</span></div>
<div><span data-id="{{ attributes.id }}"{{ attributes|without('id') }}>Without string attribute.</span></div>
-<div><span{{ attributes|without('id', 'class') }}>Without either nor class attributes.</span></div>
+<div><span{{ attributes|without('id', 'class') }}>Without id and class attributes.</span></div>
<div><span{{ attributes }}>All attributes again.</span></div>
+<div id="{{ 'quotes Here!'|clean_id }}"><span class="{{ 'Gray like a bunny!'|clean_class }} {{ 'BEM__ized--Top Feature'|clean_class }}" id="{{ 'quotes Here!'|clean_id }}">ID and class. Having the same ID twice is not valid markup but we want to make sure the filter doesn't use drupal_html_id().</span></div>