From 3f3fc8db8ff8c498e45c8ab4f874be0ec84be7ec Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 11 Aug 2014 22:14:21 -0500 Subject: [PATCH] Issue #2283301 by Dave Reid, davidhernandez, Cottser, joelpittet: Added Twig filters for drupal_html_class() and drupal_clean_id_identifier(). --- core/lib/Drupal/Core/Template/TwigExtension.php | 4 ++++ core/modules/system/src/Tests/Theme/TwigFilterTest.php | 8 ++++++-- .../templates/twig_theme_test.filter.html.twig | 3 ++- 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 05c739035cbc..cf3500e39b6e 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 c22be7f0109c..5665a4c029b5 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' => '
Without either nor class attributes.
', - 'message' => 'Attributes printed without id nor class attributes.', + 'expected' => '
Without id and class attributes.
', + 'message' => 'Attributes printed without id and class attributes.', ), array( 'expected' => '
All attributes again.
', 'message' => 'All attributes printed again.', ), + array( + 'expected' => '
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().
', + '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 28e5c151f6f6..56fc3f11b86c 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 @@
Class attributes only:
Without boolean attribute.
Without string attribute.
-
Without either nor class attributes.
+
Without id and class attributes.
All attributes again.
+