If possible with the new Field API, it would be helpful to be able to use tokens in the default values for fields. This was requested by Bevan in the comments on another issue (http://drupal.org/node/691078#comment-3813316), but I couldn't find where a separate issue was ever opened for it.

EDIT: There is amodule for 7.x based on code in this issue at http://drupal.org/project/field_default_token


Dave Reid’s picture

I thought I had looked into it before and found it almost impossible to be able to do this on a consistent basis. I'll take a look again.

Dave Reid’s picture

Status: Active » Postponed

Also considering CCK in D6 didn't support tokens in it's provided fields (text, etc) this isn't too high a priority.

jide’s picture


manoloka’s picture


Wario’s picture


Dave Reid’s picture

Title: Allow tokens to be used in the default value for a field » Allow tokens to be used in the default value and help text for a field
clashar’s picture

kmh’s picture

2.41 KB

Hi guys, I wrote a small module for supporting tokens in default values of fields. Its quite a bruteforce solution - its checking all "*_node_form" forms and alters the content by replacing all occurances of tokens by their specific values using token_replace(), but maybe it will be usefull anyways.

Its actually my first post here on drupal.org so if I did something wrong by posting here a minimodule I am sorry for that!

amontero’s picture

Hi, guys.

For the help text part, you might be interested in #1308564: Basic token support in fields' help texts.
It is a trivial patch (take a look yourself) and it works for me, but more peer reviewing is definitely needed.
Maybe it gets into core if there is enough peer review.


grasmash’s picture

amontero, I revised your patch a bit. check issue #1308564: Basic token support in fields' help texts (patch provided)

If you're interested in this feature, please support the issue!

amfriedman’s picture


magpie5212’s picture


krisrobinson’s picture


deggertsen’s picture

Status: Postponed » Active

Can we move this issue to active? This seems like a perfectly valid feature request and it's not like it's being postponed based on any other pending issues right?

@khm I have not tested your module, but it would obviously be better if we could integrate your work into a patch somehow for this module as that would be more easily tested and helpful.

rogical’s picture

Quite agree!

See default value with token replacements, it has been solved in D6.

Currently, only viewfield supports this:

amontero’s picture

Title: Allow tokens to be used in the default value and help text for a field » Allow tokens to be used in the default value of a field

As mentioned above, there is an ongoing issue for #1308564: Basic token support in fields' help texts. madmatter23 added there a patch to support them in default values that might be of interest here.
As described in this comment I've split token support in help text and default values in different issues to ease them.
Token support for help text patch passed tests successfully. You might be interested in reviewing it, also.
Issue linked and title updated.

fpedraza’s picture


andreic’s picture


Sk8erPeter’s picture

I'm begging all the "subscribers" to finally realize that BIG GREEN "Follow" BUTTON, and PLEASE read this:
Stop subscribing, start following

The "Follow" buttons look like this at the top of the issues:

Drupal Follow button

You can also unsubscribe with hovering over the "Following" button, when "Unfollow" appears:

Drupal Following buttonDrupal Unfollow button

It's really annoying to see a new post on the dashboard, and clicking on it interestedly , and after that, seeing another unnecessary "subscribing" text...

Pascoual’s picture

Project: Token » Drupal core
Version: 7.x-1.x-dev » 7.x-dev
Component: Code » field system
599 bytes
PASSED: [[SimpleTest]]: [MySQL] 39,558 pass(es). View

Inspired from madmatter23 patch, I've just made a patch to allow uses of token in default value of fields.

Test it.

amontero’s picture

Status: Active » Needs review

Setting status to NR in order to let the tests run.

drupalycious’s picture

I tested the patch and it seems some part is failing:

Drupal$ curl http://drupal.org/files/token-support-default-field-settings-1070878-20.patch | git apply
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   599  100   599    0     0   1697      0 --:--:-- --:--:-- --:--:--  3346
error: patch failed: modules/field/field.module:487
error: modules/field/field.module: patch does not apply
amontero’s picture

@sp-drupy: Patch applies cleanly against latest 7.x-dev for me. Which version are you applying the patch to?

Pascoual’s picture

I've also add the clear option : a boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated.

l.498 :
$items[$key][$innerKey] = token_replace($innerValue, array(), array('clear'=>true));

amontero’s picture

Status: Needs review » Needs work

Patch failed to apply.

pokurek’s picture

Assigned: Unassigned » pokurek

Patch #24 works well. But if the field needs value validation, it is not being saved. LIke email field, numeric ...
There is another problem with value max length. If I have a ZIP field with for example 6 characters max, so the token is not inserted into the field when token is longer than 6 characters.
And what about select options?

This could be used only for clear text longer than token.

pokurek’s picture

Assigned: pokurek » Unassigned

Sorry, for assign. :( My fault.

GiorgosK’s picture

It does not work for custom entity fields only for core drupal entities

_vid’s picture

Thanks @Pascoual and @madmatter23;
Patch in #20 worked great for me in a fresh D7.19.

I won't change the status because of potential changes to the patch in #24, but I'm excited to have token defaults available.

sebto’s picture

Thanks. I used this patch. It works for me.

balatin’s picture

Great module! Patch in #20 worked.

szantog’s picture

Temporary (and ugly a bit) workaround:

 * Implements hook_field_widget_form_alter().
function MODULE_hook_field_widget_form_alter(&$element, &$form_state, $context) {
  // Prevent replace tokens on field admin ui form.
  if (!is_null($element['#entity'])) {
    // Determine the $user. If entity has uid, try to load. If can't, use the
    // global $user.
    if (isset($element['#entity']->uid)) {
      if (!$user = user_load($element['#entity']->uid)) {
        global $user;

    // Build data for token replacement.
    $data = array(
      $element['#entity_type'] => $element['#entity'],
      'user' => $user,
    // Eg. the body field, the default value is stored in
    // $element['#default_value']
    if (isset($element['#default_value']) && !is_array($element['#default_value'])) {
      $element['#default_value'] = token_replace($element['#default_value'], $data);
    // Eg. the link field, the default value is stored in
    // $element['#default_value']['url']
    else if (is_array($element['#default_value'])) {
      foreach ($element['#default_value'] as $key => $value) {
        if (is_string($value)) {
          $element['#default_value'][$key] = token_replace($value, $data);
    // Eg. the text field, the default value is stored in
    // $element['value']['#default_value']
    else if (isset($element['value'])) {
      if (isset($element['value']['#default_value']) && !is_array($element['value']['#default_value'])) {
        $element['value']['#default_value'] = token_replace($element['value']['#default_value'], $data);

I didn't check the patch, but are you sure, the tokens aren't replaced on field settings form? (admin/structure/types/manage/[type]/fields/[field_name]
I think, also needs test. And are we sure, the token replacement should be run automatically on every fields? Maybe a checkbox in field settings form to turn it on/off?
And how is this feature in drupal 8? Is the d7 the proper version to work on?

cuman’s picture

It works for me. Thanks!

_vid’s picture

I got an error when I did a --stat on the patch in 24.

git apply -v --stat token-support-default-field-settings-1070878-24.patch
fatal: corrupt patch at line 19

Maybe it's just missing the blank line at the end of the file.

maximpodorov’s picture

The new module http://drupal.org/project/field_default_token can be used as a solution. It's based on #32 and supports even entityreference fields.

amontero’s picture

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

@maximpodorov: Thanks! Updated summary to add your link.
Since this issue is filed against Drupal Core and we're past feature freeze with no 8.x working code, let's push it to 9.x .

maximpodorov’s picture

@amontero: Unfortunately, my module does not address all issues yet. I expect to release new version soon.

maximpodorov’s picture

Current version of https://drupal.org/project/field_default_token is quite good, IMHO.

jayhawkfan75’s picture

@maximpodorov: Nice module! Thanks.

jayhawkfan75’s picture

Issue summary: View changes

Add module link

jetwodru’s picture

Hopefully included in D8, NOT postponed to D9, often, I found Views very flexible with rewrite results and token support whereas the core Fields module are pretty useless without token support, hidden field, read-only fields and etc.
Hence, I switched to use Webforms which support some tokens in fields though I'm supposed to do it in Content Types.

jetwodru’s picture

Thanks, #38 works for both node and Entity Forms, great. Since D6 does support tokens in fields, no reasons that D7 removed this critical support and then postponed to D9. It makes Drupal fields pretty useless and worse than previous versions. This issues was raised in 2011, wondering why it was not taken into D8. Thanks

christofa’s picture

drumm’s picture

Sorry, automated testing is not yet set up for 9.x.

docans’s picture

Hi i need to be able to do this in drupal 7 for ubercart attribute options names to use token. Can any one help

Xano’s picture

Version: 9.x-dev » 8.x-dev
Issue summary: View changes

Hi i need to be able to do this in drupal 7 for ubercart attribute options names to use token. Can any one help

@docans: This is the issue queue, where we work on Drupal core. Please ask your questions in the support forums or the #drupal-support channel on IRC.

We can probably still get this into Drupal 8 in a minor version, so moving to the 8.x queue.

alibama’s picture

I'm wondering whether https://www.drupal.org/project/token_embed_views + https://www.drupal.org/project/field_default_token would allow for a dynamic tokenized value in a field... has anyone tested this?

maximpodorov’s picture

What is "dynamic tokenized value"?

alibama’s picture

a token value that can be updated- use case: I want a form to be prepopulated with the values from the previous submitted content submission. so the way I would do that is to create a view that shows the most recent submission and inserts those values in the form. those values would be editable, so if someone wanted to update the values they could, and the next time they went to the form after that those new values would be in place.... not sure if this is entirely clear, thanks for following up

maximpodorov’s picture

https://www.drupal.org/project/field_default_token works with any tokens which are either global, or based on entity being edited. If you can provide the correct token with your case, it will work.

nithinkolekar’s picture

another issue #2022785: Advance this module to an "Automatic field" module, to give automatic pattern/choices for any field is in the race and IMO this feature should be implemented in Auto Entity Label instead of tweaking core. any suggestion?

blogers’s picture

Its amazing this module thanks so much

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.

jonathanshaw’s picture

Version: 8.1.x-dev » 8.2.x-dev
budda’s picture

grguth’s picture

This is now even more confusing that a token can be placed in the help text for a field, but not in the default value in 8.1. If either eform or contact form is going to be a replacement for webform, you need a way to move various values, such as user names, email, etc., into a form to populate it with reasonable values to avoid burdening the user with reentering information that the system already has.

maximpodorov’s picture

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.