Skip to content

Instantly share code, notes, and snippets.

@ahebrank
Last active August 17, 2018 12:26
Show Gist options
  • Select an option

  • Save ahebrank/6be72dfe374134650dfd4fced646c8b4 to your computer and use it in GitHub Desktop.

Select an option

Save ahebrank/6be72dfe374134650dfd4fced646c8b4 to your computer and use it in GitHub Desktop.
Drupal 8 text filter plugin to remove blank headings
<?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>&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;</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|&nbsp;)*<\/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