From d872b7bd0e318da169d5401dd7fcedaa44b77634 Mon Sep 17 00:00:00 2001 From: Chi Date: Thu, 27 Jul 2017 22:12:07 +0500 Subject: [PATCH] Added check_markup filter. --- src/TwigExtension.php | 24 +++++++++++++++++++ tests/src/Functional/TwigTweakTest.php | 4 ++++ .../install/filter.format.twig_tweak_test.yml | 16 +++++++++++++ .../templates/twig-tweak-test.html.twig | 1 + 4 files changed, 45 insertions(+) create mode 100644 tests/twig_tweak_test/config/install/filter.format.twig_tweak_test.yml diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 711a2ac..614b10d 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -48,6 +48,7 @@ class TwigExtension extends \Twig_Extension { new \Twig_SimpleFilter('preg_replace', [$this, 'pregReplaceFilter']), new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']), new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']), + new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']), ]; // PHP filter should be enabled in settings.php file. if (Settings::get('twig_tweak_enable_php_filter')) { @@ -407,6 +408,29 @@ class TwigExtension extends \Twig_Extension { return \Drupal::transliteration()->transliterate($string, $langcode, $unknown_character, $max_length); } + /** + * Runs all the enabled filters on a piece of text. + * + * @param string $text + * The text to be filtered. + * @param string|null $format_id + * (optional) The machine name of the filter format to be used to filter the + * text. Defaults to the fallback format. See filter_fallback_format(). + * @param string $langcode + * (optional) The language code of the text to be filtered. + * @param array $filter_types_to_skip + * (optional) An array of filter types to skip, or an empty array (default) + * to skip no filter types. + * + * @return \Drupal\Component\Render\MarkupInterface + * The filtered text. + * + * @see check_markup() + */ + public function checkMarkup($text, $format_id = NULL, $langcode = '', array $filter_types_to_skip = []) { + return check_markup($text, $format_id, $langcode, $filter_types_to_skip); + } + /** * Evaluates a string of PHP code. * diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index d776103..5cfa762 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -152,6 +152,10 @@ class TwigTweakTest extends BrowserTestBase { // Test transliteration. $xpath = '//div[@class = "tt-transliterate" and contains(text(), "Privet!")]'; $this->assertByXpath($xpath); + + // Test text format. + $xpath = '//div[@class = "tt-check-markup"]'; + $this->assertEquals('bold strong', trim($this->xpath($xpath)[0]->getHtml())); } /** diff --git a/tests/twig_tweak_test/config/install/filter.format.twig_tweak_test.yml b/tests/twig_tweak_test/config/install/filter.format.twig_tweak_test.yml new file mode 100644 index 0000000..6421ab5 --- /dev/null +++ b/tests/twig_tweak_test/config/install/filter.format.twig_tweak_test.yml @@ -0,0 +1,16 @@ +langcode: en +status: true +dependencies: {} +name: Twig tweak test +format: twig_tweak_test +weight: 0 +filters: + filter_html: + id: filter_html + provider: filter + status: true + weight: 0 + settings: + allowed_html: '' + filter_html_help: true + filter_html_nofollow: false diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index add9655..ed94972 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -38,4 +38,5 @@
{{ 'foo' | preg_replace('(foo)', '$1-bar') }}
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
{{ 'Привет!' | transliterate('ru') }}
+
{{ 'bold strong' | check_markup('twig_tweak_test') }}