Problem/Motivation

Using tokens in the text currently leads to cut-off and thereby broken tokens, when trimming happens inside the token, for example:
[custom:this-is-my-token]
or
[site:logo:theme:very-long-string]
or
This is a node teaser text which uses the [site:url] token

will be cut off, if hit by the trim length and the token won't be replaced. Displayed result for example:
This is a node teaser text which uses the [site:u...

Steps to reproduce

See above

Proposed resolution

Tokens have to be replaced before trimming the text to return the expected, correctly trimmed result

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork smart_trim-3301743

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

anybody’s picture

This is closely related to #2041053: Add option to strip Media module code (D7) eventhough the media entities are not real tokens.

anybody’s picture

Issue tags: +Needs tests

Made a first shot, for further improvements this could be heavily inspired by https://www.drupal.org/project/token_filter

We should also add tests with at least site tokens to ensure it works as expected.

anybody’s picture

Title: Tokens should be replaced before trimming » Support tokens in more text and replace in content before trimming
Status: Active » Needs review

As these changes affect several places in the file, I decided to combine this and do a cleanup of the token logics in smart_trim.

Please review :) If everything is fine at manual testing, we'll write some tests.

anybody’s picture

Grevil made their first commit to this issue’s fork.

grevil’s picture

Status: Needs review » Needs work

Does not work currently with trim_type "chars"

Example:

Formatter Settings (on body from bundle article):

 'type' => 'smart_trim',
        'settings' => [
          'trim_length' => 10,
          'trim_type' => 'chars',
          'summary_handler' => 'trim',
          'trim_options' => [
            'replace_tokens' => TRUE,
          ],
        ],

Test string = "Test [node:content-type]"

Expected Result: "Test Artic"
Actual Result: "Test"

See failing tests for more information Edit: "Testing is generally disabled for this module"

anybody’s picture

Thanks @Grevil, I created a separate issue for testing: #3308865: Write tests and enable automated testing

Let's see if we can fix that. Anyway I don't think it's due to this error and was wrong before already. Good tests :)

anybody’s picture

@Grevil: I found it and created an issue: #3308868: Unclear trimming behavior (whole words or not?) - missing setting

So please change the expected test result and add a comment with the issue link above, that this may change in future versions.
Expected result is now "Test" as it's the whole word BEFORE the char count.

grevil’s picture

Status: Needs work » Needs review

Done! All tests are green locally.

anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Grevil! :)

anybody’s picture

Any active maintainer to have a look?

markie’s picture

I am liking this code and how it works, but I am having a hard time manually testing this. Can you write up some text instructions please?

grevil’s picture

@markie sure thing!

  • Enable the token, token_filter and smart_trim modules.
  • Go to "/admin/config/content/formats" and enable the "Replaces global and entity tokens with their values" filter on your preferred filter format (e.g. full_html).
  • Go to "/admin/structure/types/manage/article/display" and set the body formatter to "Smart trimmed" and set the trimmed length to "20" on the default view.
  • Now create a new article, set your filter you modified earlier on the body and type in "test test test [node:comment-count]" and save.
  • On viewing the entity, it will display "test test test [node".
  • Now apply this issue's patch and clear the caches.
  • After that, go back to "/admin/structure/types/manage/article/display" and modify the body formatter settings once again. A new "Replace tokens before trimming" checkbox should appear.
  • Check that checkbox and save
  • Go back to your just created article and the token should be rendered ("test test test 0" in my case, since there are no comments on the article)!

  • markie committed bd240b5 on 2.0.x authored by Anybody
    Issue #3301743 by Anybody, Grevil: Support tokens in more text and...
markie’s picture

Status: Reviewed & tested by the community » Fixed

Awesome.. Makes so much more sense now. Committed and will be in the next release.

anybody’s picture

Hi @markie, thanks! We'd still like to offer our help as Co-Maintainers, if you'd like to.

Status: Fixed » Closed (fixed)

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