4

This question is about Drupal 8.

Placing the following line inside a story node's body (I guess it also works for other content types) adds a manual teaser break:

<!--break-->

In Drupal 7, to make manual teaser breaks work with the "Filtered HTML" text format it was necessary to add <!--> to the text format's set of allowed HTML tags (for instance, see this "Drupal Answers" post).

In Drupal 8, however, this solution no longer works: Manual teaser breaks are silently ignored even if <!--> is present in the allowed HTML tags of "Filtered HTML". I have tried to add a couple of plausible seeming combinations to the "Filtered HTML" text format, such as <!--break--> or <!---->, and a couple of implausible combinations (<!--...--> and <!--.*-->), but nothing works.

I double-checked and manual teaser breaks work fine when I switch a story's text format to "Full HTML" text format.

How can I get manual teaser breaks to work again with "Filtered HTML" in Drupal 8? If this is not possible I will have to go through all nodes of my site after the D7 > D8 migration and switch them to "Full HTML".

5

This comes down to being a problem of DomDocument and its childNodes property that does not include DomComments. Specifically, Drupal\filter\Plugin\Filter\FilterHtml::getHtmlRestrictions() will not consider any comment-like tags that are added in the 'allowed HTML tags' list. This is due to these lines in that function's code:

$html = str_replace('>', ' />', $this->settings['allowed_html']);
// Protect any trailing * characters in attribute names, since DomDocument
// strips them as invalid.
$star_protector = '__zqh6vxfbk3cg__';
$html = str_replace('*', $star_protector, $html);
$body_child_nodes = Html::load($html)->getElementsByTagName('body')->item(0)->childNodes;

This loads the text of the 'allowed HTML tags' text as an HTML DomDocument and retrieves the list of the children. Only 'proper' HTML tags will be listed as part of the childNodes property and therefore, the Drupal 7 workaround will not work for Drupal 8.

In order to solve this, one could override the FilterHtml plugin in order to allow <!--break--> to remain in the text.

One attempt following this approach exists here:

https://www.drupal.org/project/drupal8breaks

which was in turn inspired by

https://medium.com/@djphenaproxima/how-to-bend-drupal-8-plugins-to-your-nefarious-will-94da0c31f095

Good luck!

P.S.: Also available on GitHub https://github.com/stefanospetrakis/drupal8breaks

  • 2
    Too bad, no hope of getting it to work with config only. But at least I now know what's going on. I think I will write a bug report over at drupal.org. Thanks a lot for writing a whole module, I will probably give it a try. – herzbube Dec 2 '18 at 0:59
  • You are welcome, do link the report back here in a comment, I would like to follow that further too. Cheers! – Stefanos Petrakis Dec 2 '18 at 7:11
  • OK, here you go: Issue 3017548. – herzbube Dec 2 '18 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.