Last active
August 17, 2018 12:26
-
-
Save ahebrank/6be72dfe374134650dfd4fced646c8b4 to your computer and use it in GitHub Desktop.
Drupal 8 text filter plugin to remove blank headings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\MY_MODULE\Plugin\Filter; | |
| use Drupal\filter\Plugin\FilterBase; | |
| use Drupal\filter\FilterProcessResult; | |
| /** | |
| * @Filter( | |
| * id = "filter_blank_headings", | |
| * title = @Translation("Remove blank heading elements"), | |
| * description = @Translation("ckeditor can create a lot of e.g., <code><h2>&nbsp;</h2></code> tags; this removes them"), | |
| * type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE, | |
| * ) | |
| */ | |
| class FilterBlankHeadings extends FilterBase { | |
| public function process($text, $langcode) { | |
| $text = trim($text); | |
| // ugh, unicode | |
| $text = str_replace(chr(194).chr(160), ' ', $text); | |
| $text = preg_replace('/<h[2-6].*>(\s| )*<\/h[2-6]>/', '', $text); | |
| return new FilterProcessResult($text); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment