I just call token_replce without further args for basic token substitution. I'm not very familiar with core, so I can't go any further with user,node or any other tokens.
Found useful for easily displaying "create user" links in form field help texts just by using [site:url]admin/user/create, for instance.
I'm not sure if this is the right place to patch, comments are appreciated.
Works for me.

EDIT: If you're interested in token support in default values, see #1070878: Allow tokens to be used in the default value of a field.

CommentFileSizeAuthor
#54 issue-1308564-token-support-in-field-descr-D7-54.patch6.34 KBamontero
#52 issue-1308564-token-support-in-field-descr-D7.patch5.7 KBamontero
#47 tokens_field_help_text-1308564-47.patch5.54 KBDeciphered
#47 tokens_field_help_text-1308564-47-FAIL.patch4.04 KBDeciphered
#47 interdiff.txt1.21 KBDeciphered
#44 1308564-44.patch4.92 KBamontero
#44 1308564-44-FAIL.patch4.04 KBamontero
#36 1308564-36.patch6.03 KBamontero
#36 1308564-36-FAIL.patch3.94 KBamontero
#31 1308564-31.patch5.36 KBkbasarab
#31 1308564-31-FAIL.patch3.39 KBkbasarab
#30 1308564-30.patch4.83 KBkbasarab
#30 interdiff.txt2.87 KBkbasarab
#27 drupal-core--basic-token-support-in-fields-help-texts--1308564-27.d8.patch2.35 KBamontero
#26 drupal-core--basic-token-support-in-fields-help-texts-d8_1.patch1.51 KBguile2912
#26 drupal-core--basic-token-support-in-fields-help-texts-d7.patch1.48 KBguile2912
#14 drupal-core--basic-token-support-in-fields-help-texts-d7.patch1.48 KBamontero
#14 drupal-core--basic-token-support-in-fields-help-texts-d8.patch1.51 KBamontero
#17 drupal-core--basic-token-support-in-fields-help-texts-d8.patch1.51 KBamontero
#17 drupal-core--basic-token-support-in-fields-help-texts-d7.patch.txt1.48 KBamontero
#15 drupal-core--basic-token-support-in-fields-help-texts-d8.patch1.51 KBamontero
#15 drupal-core--basic-token-support-in-fields-help-texts-D7.patch1.48 KBamontero
#7 token-support-field-settings-1308564-1.patch2.22 KBgrasmash
#7 d8-token-support-field-settings-1308564-1.patch2.28 KBgrasmash
#5 token-support-field-settings-1308564-1.patch3.02 KBgrasmash
#1 token-support-field-settings-1308564-1.patch2.22 KBgrasmash
basic-token-support-in-fields-help-texts.patch1.14 KBamontero
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

grasmash’s picture

I've added a few more lines this patch so that it now adds token support for all of the following:
- field default value
- field help text
- field description

I was considered rolling this functionality into either a token.module patch or a contrib module, but it ends up being much more efficient to simply place this change in core.

Issues requesting this functionality:
http://drupal.org/node/1070878
http://drupal.org/node/1248306

Dave Reid’s picture

Status: Needs review » Needs work
+++ b/modules/field/field.moduleundefined
@@ -501,6 +501,13 @@ function field_get_default_value($entity_type, $entity, $field, $instance, $lang
+
+  foreach ($items as $key => $value) {
+    if (token_scan($value['value'])) {
+      $items[$key]['value'] = token_replace($value['value']);
+    }
+  }

This only works if the default value is a string. Also it's unnecessary to call both token_scan(). token_replace() will do nothing if it doesn't find any tokens.

12 days to next Drupal core point release.

Dave Reid’s picture

Version: 7.x-dev » 8.x-dev

We also need to apply this to D8 first, then consider backporting if possible to D7.

Dave Reid’s picture

Issue tags: +token
grasmash’s picture

Version: 8.x-dev » 7.x-dev
Status: Needs work » Needs review
Issue tags: -token
FileSize
3.02 KB

Thanks Dave,

I replaced token_scan with is_string. Think that'll do the trick?

Dave Reid’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +token

This still needs to be patched for Drupal 8 first, which is where any changes for core have to go first.

grasmash’s picture

my bad, that last patch recorded two commits. I also hadn't noticed your drupal 8 change.

I'm attaching two patches here, one for d7 and one for d8.

grasmash’s picture

Issue tags: -token

switching to 8, sorry about this back and forth.

Dave-- please let me know what I can do to help move this patch into D7.

Thanks!

grasmash’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +token

Lol, not trying to detag this either.

Status: Needs review » Needs work
Issue tags: -token

The last submitted patch, d8-token-support-field-settings-1308564-1.patch, failed testing.

grasmash’s picture

Status: Needs work » Needs review
joppert’s picture

grasmash’s picture

Issue tags: +token
amontero’s picture

Re-rolling initial patches for just fields' help texts both for D7 & D8.
@madmatter23: Maybe we should split help text, description and default value token support into separate issues for making changes more manageable and easier for core maintainers, so we all can move the issues forward as time permits. I'm sure you are aware of #1070878: Allow tokens to be used in the default value of a field.
I will try to find some time to do a little research for related issues and let's hope all of them get into core soon.

amontero’s picture

I thought that naming patch as -d7 would test it against D7 core. I was wrong, it should have been -D7 (uppercase) and with that the patch will be ignored as the docs say.
I did a little research and found the issues:
#1070878: Allow tokens to be used in the default value of a field
#700230: default value with token replacements (Drupal 6)
So I'm splitting them to simplify and hopefully move them forward and making easier to core maintainers.
Re-rolling patches appropiately. D7 patch included ready to backport when issue gets to RTBC.

amontero’s picture

Issue summary: View changes

Related issue

Status: Needs review » Needs work

The last submitted patch, drupal-core--basic-token-support-in-fields-help-texts-D7.patch, failed testing.

amontero’s picture

Resubmitting patches. Testbot is not ignoring -D7 patch (?), renaming.

amontero’s picture

grasmash’s picture

Amontero, thanks for putting time into this. I think that splitting up the patches is a fine idea. Let me know if you need any help pushing this through.

amontero’s picture

Bumping. Anyone else tested this?
Thanks madmatter23, your test would be helpful.

guile2912’s picture

Patch d7 #17 works fine for me on 7.12

amontero’s picture

Thanks, guile2912.
D7 patch will be backported when D8 one goes RTBC. Do you have a 8.x install to test it? TIA.

amontero’s picture

Title: Basic token support in fields' help texts (patch provided) » Basic token support in fields' help texts
Assigned: Unassigned » amontero
Issue tags: +Quick fix
clashar’s picture

amontero, thank you for the patch.

it works fine on D8!

BTMash’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs backport to D7

I like the idea (and due to what the patch entails, am tagging as 'needs backport to D7'). And the code looks good to me as well. However, I haven't manually tested it yet, and I do think it could use a test to make sure it works as expected and doesn't have an effect on something else we don't know about as a result of the patch.

Also, the D7 patch should be in this issue.

guile2912’s picture

Tested the D7 for few month now, and havent seen any side effects
Re queuing both patches for testing, hoping the D7 on will make it !

-Edit, ok, so we have D8patch that passed, so it wont be tested again, and a D7 one that cannot be tested because "Patches can only be tested against the current version of the issue."

So here is the D7 backport issue : #1695984: Basic token support in fields' help texts - D7

amontero’s picture

Status: Needs work » Needs review
FileSize
2.35 KB

Rerolling 8.x patch adding image type field as pointed by pooja.sarvaiye in #1695984-5: Basic token support in fields' help texts - D7.

pooja.sarvaiye’s picture

Status: Needs review » Reviewed & tested by the community

The patch works fine for D8. Tokens get replaced in help text of all the field types including image.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

This issue still needs an automated test for the feature's functionality. Thanks!

kbasarab’s picture

FileSize
2.87 KB
4.83 KB

First attempt at adding tests. Locally the field test is working but I haven't gotten the image one to pass yet. Assuming its something simple I'm missing.

kbasarab’s picture

Status: Needs work » Needs review
FileSize
3.39 KB
5.36 KB

Alright. I think I got the image piece figure out. The createImageField() function didn't add it in anywhere so I added it there.

Status: Needs review » Needs work

The last submitted patch, 1308564-31-FAIL.patch, failed testing.

kbasarab’s picture

Status: Needs work » Needs review

Moving back to needs review as the patch that was meant to fail is the only one that failed.

guile2912’s picture

If you dont want a file do be tested, you should name it "whatever-do-not-test.patch" ^^
As explained on the advanced patch contributor guide http://drupal.org/node/1054616

tim.plunkett’s picture

@guile2912 in this case, the point was to prove that it would fail. It's called Test Driven Development.

amontero’s picture

FileSize
3.94 KB
6.03 KB

Maybe I'm being picky here, but using time-dependent tokens may cause false assertion failures in some edge cases. In this case, using a short date only would trigger it if testbot run crosses midnight.
I'm not familiar with d.o testbot internals and maybe test and assert happen very quick in succession, don't know. Since testbot runs last a few minutes at least, it seems a reasonable risk to consider. I just changed [date:short] token by [site:name] in kbasarab's tests.

Also, token_replace is always called with the option 'sanitize' set to TRUE by default, so adding check_plain to output test.

Status: Needs review » Needs work

The last submitted patch, 1308564-36-FAIL.patch, failed testing.

kbasarab’s picture

Status: Needs work » Needs review

Good call @Amontero. There is definitely a chance it could happen overnight. I just had started with date just as a test and never changed off it.

amontero’s picture

So, if code is already reviewed & tested can this issue go RTBC or the same reviews apply to tests?

amontero’s picture

Status: Needs review » Reviewed & tested by the community

Assuming tests need not to receive review other than Drupal testbot, changing to RTBC.

catch’s picture

Don't we need something to indicate that you can use tokens here when entering the help text?

amontero’s picture

That would be certainly desirable.
Before a patch reroll, what would be the best way to be translator friendly/compliant in the backport?
Something like this?

$desc = t('Current help text in the description field.') . ' ' . t('New sentence stating you can use tokens now.')

Also, for the help text we could use "This field supports tokens." string as 'token' contrib module currently does.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests

Looks like this still needs work? It does not seem to still need tests, however. :)

amontero’s picture

Issue tags: +Needs tests
FileSize
4.04 KB
4.92 KB

Adding "This field supports tokens." to 'Description' field help text. Almost there, aren't we?

amontero’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 1308564-44-FAIL.patch, failed testing.

Deciphered’s picture

Status: Needs work » Needs review
Issue tags: +melb-code-sprint-2012-10
FileSize
1.21 KB
4.04 KB
5.54 KB

Re-rolled patch for changed Field module structure.

Status: Needs review » Needs work

The last submitted patch, tokens_field_help_text-1308564-47-FAIL.patch, failed testing.

amontero’s picture

Status: Needs work » Reviewed & tested by the community

Patch applies and works.

webchick’s picture

This looks good to me, but we are over thresholds atm. :(

webchick’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Woohoo! Below thresholds again.

Committed and pushed to 8.x. Thanks. Moving back to 7.x. I think this is eligible for backport (note that it does include a string addition, but on the backend), but would not hurt to get David's two cents on this.

amontero’s picture

Status: Patch (to be ported) » Needs review
FileSize
5.7 KB

Backport to 7.x to allow reviewing until we get David's input.

Status: Needs review » Needs work

The last submitted patch, issue-1308564-token-support-in-field-descr-D7.patch, failed testing.

amontero’s picture

Status: Needs work » Needs review
FileSize
6.34 KB

Ops. One test was missing.

Deciphered’s picture

An Interdiff would be nice (against the D8 patch) if you have time.

EclipseGc’s picture

David_Rothstein’s picture

I was pointed to this issue in IRC and I see there is also a secret request for my feedback in some of the comments above :)

Anyway, it seems pretty reasonable to me.... though I must say I wonder what happens if you've written a field description that is trying to refer to tokens rather than use them? Example from the User module (this isn't a field, but I don't think it's too much of a stretch to imagine an actual field that has a similar description):

function user_admin_settings() {
....
  $email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');

  $form['email_admin_created'] = array(
.......
    '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,

So, um, we should think about that a bit.

I'm also not sure I see the point of the new help text in this patch if all it's going to say is "This field supports tokens". That doesn't mean much on its own, and probably isn't guaranteed to be true for all fields (in Drupal 7) anyway if, as the patch shows, some fields like those provided by the Image module need to do extra work to support this... So maybe that can just be left off and leave the help text to the Token module, or something?

EclipseGc’s picture

Well, they'd have to switch to using the html entities in order to display that right? I think it's a totally valid concern, but I also think it's worth it. Still...


$email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');

is REALLY ugly. That being said, I still think it's probably worth it.

With regard to your second issue, I 10000% agree. I spent a good 10-15 minutes just hunting around for a good example of tokens supported by this field. However, since this is a field #description, I'm not sure any field has to do extra to support it, unless the field is trying to expose it's own values to the help text as well, which seems an incorrect way to use it since you can get help text for a field you've not yet filled out. :-S

Eclipse

amontero’s picture

Version: 7.x-dev » 8.x-dev

Since #57 seems applicable to 8.x, raising visibility to maintainers to discuss because of already commited patch.

amontero’s picture

Assigned: amontero » Unassigned

Unassigning.

Deciphered’s picture

I propose that #514990: Add a UI for browsing tokens is a precursor to this aspect of this issue, because once that issue is resolved we would want to implement the actual token browser in lieu of the help text.

amontero’s picture

Status: Needs review » Needs work
Issue tags: +Quick fix, +Needs tests, +token, +Needs backport to D7, +melb-code-sprint-2012-10

The last submitted patch, issue-1308564-token-support-in-field-descr-D7-54.patch, failed testing.

amontero’s picture

Status: Needs work » Active

Since #514990: Add a UI for browsing tokens has ben moved to 9.x and seems to me that the token browser perhaps will not make it in 8.x, let alone 7.x, how about a limited no-token-browser and no-help-text-addition-if-necessary backport feedback? What the gurus say?

amontero’s picture

And for starters, here's my feedback:

#58: I agree 100% with EclipseGc points. Displaying tokens not meant
to be parsed looks ugly, but worth the hassle IMO.

#57: Anyway, it seems pretty reasonable to me.... though I
must say I wonder what happens if you've written a field description
that is trying to refer to tokens rather than use them?

Possible solutions:

  • Just warn about it in the release notes
  • Write an update hook to use EclipseGc workaround
amontero’s picture

Issue summary: View changes

Formatting

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • webchick committed efa195a on 8.3.x
    Issue #1308564 by amontero, madmatter23, kbasarab, guile2912, Deciphered...

  • webchick committed efa195a on 8.3.x
    Issue #1308564 by amontero, madmatter23, kbasarab, guile2912, Deciphered...

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • webchick committed efa195a on 8.4.x
    Issue #1308564 by amontero, madmatter23, kbasarab, guile2912, Deciphered...

  • webchick committed efa195a on 8.4.x
    Issue #1308564 by amontero, madmatter23, kbasarab, guile2912, Deciphered...

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

  • webchick committed efa195a on 9.1.x
    Issue #1308564 by amontero, madmatter23, kbasarab, guile2912, Deciphered...

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

apaderno’s picture

Version: 8.9.x-dev » 9.1.x-dev

The status of this issue is a little confusing. An automatic comment says there is a commit for Drupal 9.1.x.
Is the status correct, or should it be Needs work?

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Version: 9.4.x-dev » 8.0.x-dev
Status: Active » Fixed
Related issues: +#514990: Add a UI for browsing tokens

I agree with @apaderno that this is confusing.

This was committed to 8.x and re-opened for backport to Drupal 7, then moved back to 8. From what I gather it was re-opened because simply adding 'this field support tokens' is not very useful to the user and should be improved. Searching the issues I see that there is an issue to improve the user experience with using tokens, #514990: Add a UI for browsing tokens. It seems reasonable to put effort into that issue instead of trying to do something in this old, rather untouched issue.

Therefor setting to fixed at the time this was committed.

Cheers,

Status: Fixed » Closed (fixed)

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