In trying to get the metatags module to use a sensibly trimmed and cleaned version of a field, I came to the conclusion it would be really useful to have tokens exposed for textarea fields.

Preferably this token should be dynamic so that an argument of trim length can be passed in, this broadens the usefulness of the tokens.

Issue fork smart_trim-2782455

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

duncan.moo created an issue. See original summary.

duncan.moo’s picture

Assigned: duncan.moo » Unassigned
Status: Active » Needs review
StatusFileSize
new3.1 KB

A few notes on my patch:

  • I am no token expert, please review the code.
  • I am only exposing tokens for fields which are text_with_summary, should there be tokens for other textarea fields?
  • The trim length is limited to character length, but respects word boundaries.
  • The token pattern is [node:field-name-smart-trim:?] where ? = int = max length
    • I did not use [node:field-name:smart-trim] due to a collision with [node:field-name:?]
    • I have only done it for nodes
  • This patch does not add a token dependency to smart_trim, but if token module is enabled the code in smart_trim.tokens.inc gets used

It is far from perfect, please review, make suggestions, fix...

duncan.moo’s picture

StatusFileSize
new3.11 KB

Correction on one of the parameters going into the smart_trim_field_formatter_view() function:
'trim_options' => array('text' => TRUE),

duncan.moo’s picture

StatusFileSize
new3.35 KB

So another revision of this patch, although it was working warning messages let me know that I was calling smart_trim_field_formatter_view() with $instance set as the token string. I am using field_info_instance() to get the correct array.

g33kg1rl’s picture

Super patch!

I cleared the cache and the tokens were available, but they don't show up perfectly in the token list. (the node:body-smart-trim didn't show up but versions such as cer:node:body-smart-trim showed up)

I also received this error message after saving content:

Notice: Undefined index: en in smart_trim_tokens() (line 75 of all/modules/smart_trim/smart_trim.tokens.inc).
Warning: Invalid argument supplied for foreach() in smart_trim_field_formatter_view() (line 40 of all/modules/smart_trim/smart_trim.module).

maxplus’s picture

@duncan.moo

Thanks for this handy patch, I'm using the smart-trim token for setting a node title using Auto Entity Label, works OK until now

ruloweb’s picture

Actually this can be done without the patch, if you enable the Token View Mode and apply the Smart Trim format to your fields, Token module will use it. It can be done with any Field Formatted that you want.

Please give it a try, I have solved it that way.

If you can solve it in that way, I think we should mark this issue as Works as designed.

g33kg1rl’s picture

Personally I like this patch better. Sometimes I need to specify a very specific word or character count and I don't want it to be universally applied over the whole website.

I needed this for trimmed meta tag descriptions and meta page titles.

ruloweb’s picture

@g33kg1rl actually I did the same, I wanted to trimmed and strop HTML in the metatag descriptions, if you select the Smart Trim format only in the Token View Mode, it will apply for tokens, not the entire site.

duncan.moo’s picture

Thanks for the suggestion @ruloweb however it is not a node display that I am after here but as for g33kg1rl I needed a way to get a node summary that I could use for meta-tags.

I am just not clear on your suggestion, can you explain more clearly? I want to have a token that is specifically for the description meta-tag, it will not be used at all anywhere else, and I do not want to override the body token to be the same as the meta-tag token.

ruloweb’s picture

StatusFileSize
new127.18 KB

@duncan.moo if you create (or enable if it is already there) a View Mode called Token for your entity, then you can change display settings on any field for that View Mode only, then metatag will notice that, and apply those settings.

Screenshot attached, I did that for a paragraph type, and it works like a charm.

g33kg1rl’s picture

@ruloweb I do appreciate you taking the time to explain this alternative way, but it doesn't actually meet the needs that the patch does.

I do not want to change the format on the content type. I don't want the other format to appear anywhere else than the tokens that I put in the field provided by the Metatag module. This patch accomplishes that exact use case.

duncan.moo’s picture

Yes agreed thanks for the explanation @ruloweb however if we are already using tokens for display of the field elsewhere and I change the token display mode it will affect the other use of tokens. So although it is a work-around it is not an ideal solution.

g33kg1rl’s picture

Is this rtbc?

duncan.moo’s picture

StatusFileSize
new3.3 KB

I found another issue with the patch supplied in #4, I was using langcode supplied by the function call rather than that on the node, this was causing an undefined index error.

Attached patch is a reroll of the full patch in #4 above.

duncan.moo’s picture

StatusFileSize
new3.3 KB

Uploaded the wrong patch, trying again.

mgerbault’s picture

Available in Drupal 8 ?

chris matthews’s picture

This patch simply removes the extra blank line at EOF.

bessonweb’s picture

When I try to apply the patch with git, it say "error: corrupt patch at line 96".

Do you have a solution?

Thanks.

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

voleger’s picture

Added token alter to fix [node:summary] token. Thanks for #2139587-1: Meta Description Token [node:summary] from Meta Tag Module Breaks using Smart Trim code snipped and little bit improvements, the [node:summary] token will respects trim_type words configuration.

voleger’s picture

voleger’s picture

Added helper function for word count token support. Now token ignores HTML markup during words calculations.

jim22’s picture

Thank you, duncan.moo. This patch is awesome.

Some notes for anyone else running into issues implementing.

1) I used patch #16, as #18 had an error.

2) I ran into an error on line 75 of smart_trim.tokens.inc. I switched [$langcode] to ['und']. This seems to have resolved it and now outputs the token.

3) I can't see the smart_trim tokens in the fast_token_browser, but using the following works for node body field.
[node:body-smart-trim:145]

anybody’s picture

Any active maintainer here for D7 to review this?

heddn’s picture

Version: 7.x-1.x-dev » 2.1.x-dev

I'm sure the implementation between D7 and D10 is a little different. But a smart trim token for summary fields is actually a missing feature-set in D10 too. And since the life-support for D7 is so short at this point, I'm going to just move versions and work on a version of these functionality for the 2.1.x version.

heddn changed the visibility of the branch 2.1.x to hidden.

heddn’s picture

Initial implementation in smart trim 2.1.x provided.

akalata’s picture

Status: Needs review » Reviewed & tested by the community

Works as expected, though I'm not sure about the assumption of a `token` view mode -- but I think that's more of a foundational approach with "how do we handle tokens that might need to respond to settings?".

heddn’s picture

The 'token' view mode logic is exactly what token.module assumes. It seems like that assumption was a safe one to continue using here too.

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

ultimike’s picture

Status: Reviewed & tested by the community » Needs work

Thanks everyone for pushing this issue forward, and also for your patience for one of this module's slacker maintainers finds the time to check it out (present company included).

First off - this needs a test. @markie and I have been really trying to require tests for all feature additions and bug fixes, so this would be no exception. I think a functional test would be pretty straight-forward...

I manually tested this using Gitpod with a few of our DrupalEasy alums and all appears to work as expected. The only thing I would make note of (possibly with an update to the README and/or docs) is the fact that the Token view mode must be enabled and configured with a Smart Trim formatter (for the field(s) in question) or the Smart Trim tokens aren't really involved.

Regardless, we tested Smart Trim tokens with Pathauto and Metatag with everything working as expected.

I went ahead and committed @heddn and @akalata's suggestions, as well as a PhpStan ignore for renderPlain() in smart_trim.tokens.inc (see #3463405: Handle renderPlain() deprecation).

-mike

heddn’s picture

Status: Needs work » Needs review

Test coverage added.

markie’s picture

Status: Needs review » Needs work

Needs a rebase please.

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

heddn changed the visibility of the branch 2782455-smart_trim_summary_token to hidden.

heddn’s picture

Status: Needs work » Needs review

This should now be green, except for Drupal 11 support. Which is still not functional on HEAD.

ankitv18 changed the visibility of the branch 2.1.x to active.

ankitv18 changed the visibility of the branch 2782455-smart_trim_summary_token to active.

ankitv18 changed the visibility of the branch 2.1.x to hidden.

ankitv18 changed the visibility of the branch 2782455-smart-trim-tokens-7.x to hidden.

heddn’s picture

In #39 thought I had it figured out why tests are failing on pre 10.2. They pass successfully on local 10.2, just not on the testbot. Since 10.1 is no longer supported and this is a new feature, I feel quite happy to simply skip execution on old drupal versions (that's what we do now). It results in the same work later since in both cases we'd have pre 10.2 logic that would need to be removed.

ultimike’s picture

Status: Needs review » Needs work

I changing this to "needs work", but for any serious reason - only because I have a question about the test logic that I couldn't figure out on my own...

thanks,
-mike

heddn’s picture

Status: Needs work » Needs review

Comment posted.

markie’s picture

Version: 2.1.x-dev » 2.x-dev
markie’s picture

Status: Needs review » Reviewed & tested by the community

Tested in Drupalpod with metatag and it seems to be working well. Was able to update the metatag description with a token.

  • markie committed 31bfff67 on 2.x authored by heddn
    Issue #2782455 by heddn, duncan.moo, voleger, ankitv18, ultimike, Chris...
markie’s picture

Status: Reviewed & tested by the community » Fixed

Merged and prepping for the next release.

Status: Fixed » Closed (fixed)

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

jacobupal’s picture

I've just tried this out and while [node:body-smart-trim] alone will work, when I use it as a meta tag description [node:body-smart-trim:160] i.e. including a max-length value, it does not. The page simply loads without the appropriate meta tag present. Presumably because "[node:body-smart-trim:160]" could not be found.

So two questions:

  1. Am I using the token correctly?
  2. If I can't enter a character limit, what is the default character limit being used?
ultimike’s picture

@jacobupal,

No, tokens that include a max-length value are not supported.

When using the new Smart Trim token, the max value (and the rest of the settings) will come from the Token view mode of the entity (if specified) or fallback to the default view mode settings.

-mike

heddn’s picture

Exactly what Mike said in #53. That is how core tokens work too. This is just following that precedent.

anybody’s picture

Just found this issue but couldn't find documentation on this secret feature anywhere. Is it possible that this isn't documented at all?
Maybe someone how knows how to use this could add some sentences to the READM.md or the documentation pages? (Maybe as follow-up?)

ultimike’s picture

@anybody - it took us awhile, but here's a new doc page for it!

https://www.drupal.org/docs/contributed-modules/smart-trim/smart-trim-to...