Question

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".

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à drupal.stackexchange
scroll top