Twould be terrific if this module provided a Twig filter

Created on 25 May 2023, about 1 year ago
Updated 2 December 2023, 7 months ago

For the times when you need to trim text that isn't a field value.

Feature request
Status

Fixed

Version

2.1

Component

Code

Created by

🇨🇦Canada dalin Guelph, 🇨🇦, 🌍

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

Merge Requests

Comments & Activities

  • Issue created by @dalin
  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    19 pass
  • @lostcarpark opened merge request.
  • 🇮🇪Ireland lostcarpark

    Thought this would be an interesting one to tinker with as it shouldn't have any conflicts with other pending changes.

    I have defined two Twig templates, smart_trim_chars and smart_trim_words.

    Both take two parameters, the number of characters/words to trim at, and the suffix to append if trimming took place.

    They can be used as follows:

    {{ item.content | smart_trim_chars(50, '...') }}

    {{ item.content | smart_trim_words(10, '') }}

    Note that the only work on a single value field. If the field may contain multiple values, chain with the render filter.

    {{ item.content | render | smart_trim_chars(50, '...') }}

    The twig filter seems to cover the basics. If there are any other cases I'm missing, let me know.

    I think it needs some tests.

  • Assigned to lostcarpark
  • Status changed to Needs work about 1 year ago
  • Status changed to Postponed about 1 year ago
  • 🇮🇪Ireland lostcarpark

    I've looked at ways of writing a test for this, and it looks like the best option is to add a twig file to the test theme in Add config option for more link wrapper Fixed . I'm postponing until that change is merged.

  • Status changed to Needs work about 1 year ago
  • 🇮🇪Ireland lostcarpark

    I don't think it's possible to change the target of a MR, so pushed 2.1.x-dev into fork, then created new branch off it, and cherry-picked the previous Twig filter change into that.

  • 🇮🇪Ireland lostcarpark

    New test added to test Twig filter by creating a new content type, and a node of that content type, than using a node template to format that field with a Node template file that displays the body field with the smart trim filter using a number of different sets of parameters.

  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • Status changed to RTBC 10 months ago
  • 🇺🇦Ukraine abramm Lutsk

    Thank you @lostcarpark, this is exactly what I was looking for.
    Both chars and words filters works as charm with patch from MR applied on top of 2.1.0.

    Setting RTBC.

  • First commit to issue fork.
  • Status changed to Needs work 9 months ago
  • 🇺🇸United States ultimike Florida, USA

    Nice job here! I've added a bit to the README about the new Twig filters (and also in order to trigger our GitLab testing pipeline).

    Unfortunately, the Drupal 9 test is failing. So you don't have to dig around the pipeline log:

    There was 1 error:
    1) Drupal\Tests\smart_trim\Functional\SmartTrimTemplateTest::testTwigFilter
    Exception: TypeError: Argument 1 passed to Drupal\smart_trim\SmartTrimTwigExtension::smartTrimChars() must be an instance of Drupal\smart_trim\mixed, instance of Drupal\Core\Render\Markup given, called in /builds/project/smart_trim/web/sites/simpletest/85255959/files/php/twig/65070270b6c80_node--filtered.html.twig_oM0lrod6OBLYHWFm2RodUzANQ/J9Lx1Ct3fYJu3yOsMu0dSpkeSLtn6OKh0HiNp4mDlSo.php on line 50
    Drupal\smart_trim\SmartTrimTwigExtension->smartTrimChars()() (Line: 54)

    @lostcarpark - I am a bit curious about

    new TwigFilter('smart_trim', [$this, 'smartTrimChars'], ['is_safe' => ['html']]),

    What's the purpose of this filter? Kind of like a default?

    Someone needs to confirm that I didn't add anything silly to the README.

    Also - once this is merged and a new release is tagged with this change, we need to open an issue to add a bit about this to the module documentation .

    thanks,
    -mike

  • 🇮🇪Ireland lostcarpark

    Doh! I forgot the mixed type is new in PHP 8. Should be fairly easy to fix that.

    I'll have to have a look to see what the TwigFilter line is doing, as it was a while ago.

  • 🇮🇪Ireland lostcarpark

    That seems to be passing the test now. mixed doesn't work in PHP7, but string|null does.

    The purpose of the line above is to set up a "smart_trim" filter, which does the same as "smart_trim_chars". They are basically aliases. Perhaps it would be better to just have the other two so that it's totally clear they are for characters and words. If you think that would be better, we can just delete that line.

    Your addition to the README looks good to me.

  • Status changed to Needs review 9 months ago
  • First commit to issue fork.
  • 🇮🇪Ireland lostcarpark

    Discussed with @ultimike. Tests were failing because of HTML in the input. Decided to add a "strip_html" parameter (defaulting to true, because most people will want to trim to length without considering HTML.

    Updated tests to cover both true and false values.

    All tests passing locally.

    When tests run in CI, it seems to try to run against 2.0 branch, and fails build step.

    When explicitly kick off CI test, tests pass, but PHPStan fails, reporting unsafe use of new static.

    I think we need to tell the MR to test against the 2.1 branch. Not sure how to fix the PHPStan issue.

  • 🇺🇦Ukraine abramm Lutsk

    Hi @lostcarpark,

    The 'unsafe new static' is a known issue reported by PHPStan for many Drupal project; in fact, core CI configuration even ignores it. That's because this is in fact not an issue but rather a best practice in Drupal world. Updating the code the way PHPStan suggest would actually make it worse for Drupal use case.

    Read here on why the issue is happening and how to handle it:
    https://www.drupal.org/docs/develop/development-tools/phpstan/handling-u...

    TL;DR: Just put a phpstan.neon file like this: https://git.drupalcode.org/project/single_content_sync/-/blob/1.4.x/phps...

  • 🇮🇪Ireland lostcarpark

    That sounds like the right solution, but I don't think it should be part of this issue. I'll test on the main branch, and if the same issue occurs I'll open a separate issue.

  • 🇮🇪Ireland lostcarpark

    I have confirmed that the PHPStan error is in SmartTrimFormatter.php.

    This change makes no changes to this file.

    The change suggested in #21 sounds the right way to fix it, but I feel adding to this change would confuse things.

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

    Ah bummer. It is failing the Drupal 9 tests with lots of errors like:

    1) Drupal\Tests\smart_trim\Functional\SmartTrimFunctionalTest::testInstallation
    ParseError: syntax error, unexpected '|', expecting variable (T_VARIABLE)
    /builds/project/smart_trim/src/SmartTrimTwigExtension.php:56

    -mike

  • 🇮🇪Ireland lostcarpark

    Found the problem.

    I had used string|null, but the null type was only added in PHP 8.1. Changing to ?string fixes it (except ? is not allowed in docblock, so left the null there).

    All tests passing except for known PHPStan issue.

  • Status changed to Needs review 7 months ago
  • Status changed to Fixed 7 months ago
  • 🇺🇸United States ultimike Florida, USA

    Looks good - thanks for the quick fix!

    Merged.

    -mike

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

Production build 0.69.0 2024