Read more link always displayed in some settings

Created on 9 June 2023, about 1 year ago
Updated 3 August 2023, 11 months ago

I noticed that in my installation, the "Read more"-Link was always displayed, regardless of the setting "link_trim_only"

I did some debugging and found out, that in my case, $original_output always contained a linebreak after the html. The function "truncateHtml->truncateChars" always removes that last linebreak (which is correct, since it is after the last closing tag). But then of course, $original_output is always different from $output, so the "more"-link is always shown.

I fixed this by adding trim to the comparison

if (
          strpos(strrev($output), strrev('<!--break-->')) !== 0 &&
          (($more_settings['link_trim_only'] ?? FALSE) !== TRUE) ||
          (trim($original_output) !=trim($output))
        )

I am using CKEditor and SimpleHTML for the field, so maybe that is the reason that trailing newline is there. I would suggest to modify the comparsion between $original_output and $output to check if there was a real shortening.

🐛 Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bioschaf
  • Assigned to shailja179
  • Issue was unassigned.
  • 🇮🇳India shailja179 India

    @bioschaf,
    I am using these settings given in screenshot. But it is working as expected.
    Can you give the proper steps to reproduce the issue?

  • I am using the same settings, I selected
    [x] Display More link?
    and
    [x] Display More link only when content is trimmed?

    I don't think it is a problem with the settings of this module an how they work.

    As I wrote the "SmartTrimFormatter.php" compares two variables ($output and $original_output) to determine if the "More"-Link should be added or not. In my case (whatever the reason may be) $original_output contains a newline character at the end, which is stripped by this module's function "truncateChars" in TruncateHTML.php, even if the content itself is not shortened.

  • First commit to issue fork.
  • 🇺🇸United States ultimike Florida, USA

    @bioschaf,

    Thanks for your report and efforts on this issue.

    I have modified one of the test nodes in our existing SmartTrimFunctionalTest.php with a trailing "\r\n " - without any fix testMoreLinkOnlyWhenContentIsTrimmed() failed (good news!)

    I then removed the trim($html) bits from the TruncateHTML service methods and moved them to the formatter. This ensures that both the $output and $original_output are trimmed equally. With this fix, all tests pass.

    MR created and assigned to @markie for review.

    -mike

  • Assigned to markie
  • Status changed to Needs review about 1 year ago
  • @ultimike opened merge request.
  • First commit to issue fork.
  • 🇺🇸United States ultimike Florida, USA

    D9 test currently failing.

    1) Drupal\Tests\smart_trim\Unit\TruncateHTMLTest::testRemoveHtmlComments
    ReflectionException: Trying to invoke protected method Drupal\smart_trim\TruncateHTML::removeHtmlComments() from scope ReflectionMethod
    /builds/project/smart_trim/tests/src/Unit/TruncateHTMLTest.php:190
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/TextUI/Command.php:144
    /builds/project/smart_trim/vendor/phpunit/phpunit/src/TextUI/Command.php:97
    phpvfscomposer:///builds/project/smart_trim/vendor/phpunit/phpunit/phpunit:106

    See https://git.drupalcode.org/project/smart_trim/-/jobs/58193 for full info.

    -mike

  • Assigned to ultimike
  • Status changed to Needs work 11 months ago
  • 🇺🇸United States ultimike Florida, USA
  • 🇺🇸United States ultimike Florida, USA

    Turns out this was an easy fix - I had written the test for PHP 8.1+, which is why it failed the D9 test. MR updated.

    -mike

  • Assigned to markie
  • Status changed to Needs review 11 months ago
  • 🇺🇸United States ultimike Florida, USA

    All tests passing.

  • 🇺🇸United States ultimike Florida, USA

    Added Drupal Dev Days tag.

  • Status changed to Fixed 11 months ago
  • 🇺🇸United States markie Albuquerque, NM

    Merged. Thanks for all the good work

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

Production build 0.69.0 2024