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.
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.
Comment | File | Size | Author |
---|
Issue fork smart_trim-2782455
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
Comment #2
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedA few notes on my patch:
It is far from perfect, please review, make suggestions, fix...
Comment #3
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedCorrection on one of the parameters going into the smart_trim_field_formatter_view() function:
'trim_options' => array('text' => TRUE),
Comment #4
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedSo 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.
Comment #5
g33kg1rl CreditAttribution: g33kg1rl commentedSuper 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).
Comment #6
maxplus CreditAttribution: maxplus commented@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
Comment #7
ruloweb CreditAttribution: ruloweb at Media.Monks commentedActually 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.
Comment #8
g33kg1rl CreditAttribution: g33kg1rl commentedPersonally 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.
Comment #9
ruloweb CreditAttribution: ruloweb at Media.Monks commented@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.
Comment #10
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedThanks 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.
Comment #11
ruloweb CreditAttribution: ruloweb at Media.Monks commented@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.
Comment #12
g33kg1rl CreditAttribution: g33kg1rl commented@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.
Comment #13
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedYes 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.
Comment #14
g33kg1rl CreditAttribution: g33kg1rl commentedIs this rtbc?
Comment #15
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedI 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.
Comment #16
duncan.moo CreditAttribution: duncan.moo as a volunteer commentedUploaded the wrong patch, trying again.
Comment #17
mgerbaultAvailable in Drupal 8 ?
Comment #18
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commentedThis patch simply removes the extra blank line at EOF.
Comment #19
Bessonweb CreditAttribution: Bessonweb commentedWhen I try to apply the patch with git, it say "error: corrupt patch at line 96".
Do you have a solution?
Thanks.
Comment #22
volegerAdded 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.
Comment #23
volegerComment #24
volegerAdded helper function for word count token support. Now token ignores HTML markup during words calculations.
Comment #25
jim22 CreditAttribution: jim22 commentedThank 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]
Comment #26
AnybodyAny active maintainer here for D7 to review this?