3 namespace Drupal\filter_test\Plugin\Filter;
5 use Drupal\filter\FilterProcessResult;
6 use Drupal\filter\Plugin\FilterBase;
7 use Drupal\Component\Utility\Xss;
10 * Provides a test filter to restrict HTML tags and attributes.
13 * id = "filter_test_restrict_tags_and_attributes",
14 * title = @Translation("Tag and attribute restricting filter"),
15 * description = @Translation("Used for testing \Drupal\filter\Entity\FilterFormatInterface::getHtmlRestrictions()."),
16 * type = Drupal\filter\Plugin\FilterInterface::TYPE_HTML_RESTRICTOR
19 class FilterTestRestrictTagsAndAttributes extends FilterBase {
24 public function process($text, $langcode) {
25 $allowed_tags = array_filter($this->settings['restrictions']['allowed'], function ($value) {
26 return is_array($value) || (bool) $value !== FALSE;
28 return new FilterProcessResult(Xss::filter($text, array_keys($allowed_tags)));
34 public function getHTMLRestrictions() {
35 $restrictions = $this->settings['restrictions'];
37 // The configuration system stores FALSE as '0' and TRUE as '1'. Fix that.
38 if (isset($restrictions['allowed'])) {
39 foreach ($restrictions['allowed'] as $tag => $attrs_or_bool) {
40 if (!is_array($attrs_or_bool)) {
41 $restrictions['allowed'][$tag] = (bool) $attrs_or_bool;
44 foreach ($attrs_or_bool as $attr => $attrvals_or_bool) {
45 if (!is_array($attrvals_or_bool)) {
46 $restrictions['allowed'][$tag][$attr] = (bool) $attrvals_or_bool;
49 foreach ($attrvals_or_bool as $attrval => $bool) {
50 $restrictions['allowed'][$tag][$attr][$attrval] = (bool) $bool;
57 if (isset($restrictions['forbidden_tags'])) {
58 foreach ($restrictions['forbidden_tags'] as $tag => $bool) {
59 $restrictions['forbidden_tags'][$tag] = (bool) $bool;