Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When passing to truncate_utf8() text that spans multiple lines, the function cuts the text on the first newline character if the $wordsafe flag is set to TRUE, no matter what $max_length is passed to it.
To test:
$text = <<<EOD
This is a text that spans multiple lines.
Line 2 goes here.
EOD;
dsm(truncate_utf8($text, 51, TRUE));
dsm(truncate_utf8($text, 51, FALSE));
Output:
- This is a text that spans multiple lines.
- This is a text that spans multiple lines.
Line 2 go
Expected behavior:
- This is a text that spans multiple lines.
Line 2 - This is a text that spans multiple lines.
Line 2 go
The attached patch fixes this by adding the s (PCRE_DOTALL) modifier to the regular expression used.
Comment | File | Size | Author |
---|---|---|---|
#20 | unicode-truncate-1712106-19.patch | 1.67 KB | m4olivei |
#9 | unicode-truncate-1712106-9-FAIL.patch | 904 bytes | dawehner |
#9 | unicode-truncate-1712106-9.patch | 1.77 KB | dawehner |
#8 | drupal8-cut_on_the_first_line_wordsafe_true-1712106-8.patch | 913 bytes | InternetDevels |
Comments
Comment #1
Feng-Shui CreditAttribution: Feng-Shui commentedAttached patched works for me, thanks.
Comment #2
jamix CreditAttribution: jamix commentedRe-rolling against 8.x-dev.
Comment #3
JacobSanfordRe-roll to reflect core changes.
Comment #4
gaellafond CreditAttribution: gaellafond commentedI just found this issue. Any idea when it will be fixed in the Core?
Comment #5
yce CreditAttribution: yce commentedThanks, attached patch works for me too. (7.x)
Comment #6
BoobaaAttached is an updated patch against 7.26 (with offset changes only).
Comment #8
InternetDevels CreditAttribution: InternetDevels commentedInterdiff is empty, because function truncate_utf8 moved to Unicode::truncate.
Comment #9
dawehnerThe code looked fine, though there was clearly no test so I went ahead and used the issue summary to create one
Comment #10
dawehnerups
Comment #12
xmacinfoHas this been fixed for Drupal 7?
Comment #17
m4oliveiJust came across this. Patch still works against 8.4.x.
Comment #20
m4oliveiAlright, I guess not. Here's a re-roll.
Comment #21
m4oliveiComment #23
alexpottCommitted and pushed a9685a928d to 8.6.x and fd2d6c259d to 8.5.x. Thanks!
I re-wrote the test data to be inline with coding standards. I ran the test locally and proved it failed as expected.