Browse Source

Added check_markup filter.

8.x-1.x
Chi 8 years ago
parent
commit
d872b7bd0e
  1. 24
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 16
      tests/twig_tweak_test/config/install/filter.format.twig_tweak_test.yml
  4. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

24
src/TwigExtension.php

@ -48,6 +48,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFilter('preg_replace', [$this, 'pregReplaceFilter']), new \Twig_SimpleFilter('preg_replace', [$this, 'pregReplaceFilter']),
new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']), new \Twig_SimpleFilter('image_style', [$this, 'imageStyle']),
new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']), new \Twig_SimpleFilter('transliterate', [$this, 'transliterate']),
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']),
]; ];
// PHP filter should be enabled in settings.php file. // PHP filter should be enabled in settings.php file.
if (Settings::get('twig_tweak_enable_php_filter')) { 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); 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. * Evaluates a string of PHP code.
* *

4
tests/src/Functional/TwigTweakTest.php

@ -152,6 +152,10 @@ class TwigTweakTest extends BrowserTestBase {
// Test transliteration. // Test transliteration.
$xpath = '//div[@class = "tt-transliterate" and contains(text(), "Privet!")]'; $xpath = '//div[@class = "tt-transliterate" and contains(text(), "Privet!")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test text format.
$xpath = '//div[@class = "tt-check-markup"]';
$this->assertEquals('<b>bold</b> strong', trim($this->xpath($xpath)[0]->getHtml()));
} }
/** /**

16
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: '<b>'
filter_html_help: true
filter_html_nofollow: false

1
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -38,4 +38,5 @@
<div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div> <div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div> <div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>
<div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</div> <div class="tt-transliterate">{{ 'Привет!' | transliterate('ru') }}</div>
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>' | check_markup('twig_tweak_test') }}</div>
</div> </div>

Loading…
Cancel
Save