Problem/Motivation

We have a nice summary with taw HTML since #3008364: Show raw summary when striping tags returns empty

However if there is 150 characters of HTML first, it switches to raw HTML display instead of trying to extract text after stripping first.

Proposed resolution

Run strip HTML on the full body and cutoff afterwards.
Only show raw HTML if still nothing.

Comments

miro_dietiker created an issue. See original summary.

johnchque’s picture

Status: Active » Needs review
StatusFileSize
new2.25 KB
new2.96 KB

This should make the magic, adding tests. :)

miro_dietiker’s picture

  1. +++ b/src/Entity/Paragraph.php
    @@ -712,15 +712,15 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
             $summary = htmlspecialchars(trim($text));
    ...
    +        $summary = Unicode::truncate($summary, 150);
    

    If this creates an encoded entity like & after 148 characters then it would be cutoff and display wrongly.

    I think we should apply htmlspecialchars after truncation.

    But yeah you're right, the raw summary isn't truncated at all currently...

  2. +++ b/src/Entity/Paragraph.php
    @@ -712,15 +712,15 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
           $summary = trim(strip_tags($text));
    

    I think we should truncate right after this line for the first time.

The last submitted patch, 2: extract_text_long_html-3011151-2-test-only.patch, failed testing. View results

johnchque’s picture

Should be better now. :)

johnchque’s picture

StatusFileSize
new619 bytes

I forgot the interdiff.

The last submitted patch, 5: extract_text_long_html-3011151-5-test-only.patch, failed testing. View results

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/src/Entity/Paragraph.php
@@ -712,11 +712,10 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
         $summary = htmlspecialchars(trim($text));

We should truncate this too, otherwise it could be a huge BLOB of HTML Script or so.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new3.04 KB
new407 bytes

Test not changing, thus previous test only patch still applies. :)

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Entity/Paragraph.php
@@ -712,14 +712,16 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface {
 
       $text = $this->get($field_name)->value;
-      if (strlen($text) > 150) {
-        $text = Unicode::truncate($text, 150);
-      }
-
       $summary = trim(strip_tags($text));
+      if (strlen($summary) > 150) {
+        $summary = Unicode::truncate($summary, 150);
+      }
       if (empty($summary)) {
         // Tease raw HTML to have at least some summary.
         $summary = htmlspecialchars(trim($text));
+        if (strlen($summary) > 150) {
+          $summary = Unicode::truncate($summary, 150);
+        }
       }
     }
 

There is no need to do a separate strlen() check ourself for > 150, Unicode:: truncate() does that too as basically the very first thing.

So I think you can just integrate that into the existing lines: Unicode::truncate(trim(strip_tags($text))) and then the same for the html version.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new2.97 KB
new752 bytes

More magic. :) Thanks @Berdir!

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Thanks.

miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed

Awesome. Committed. :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.