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.

For Drupal 7, see http://drupal.org/project/field_default_token.

For Drupal 9/10 Entity Prepopulate can be used, see Entity Prepopulate examples.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

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

Subscribe

manoloka’s picture

subscribe

Wario’s picture

subscribe

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

FileSize
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.

HTH.

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

Subscribe

magpie5212’s picture

Subscribe

krisrobinson’s picture

Subscribing

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:
dd

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

subscribing

andreic’s picture

subscribing

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 button Drupal 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...
Thanks!

Pascoual’s picture

Project: Token » Drupal core
Version: 7.x-1.x-dev » 7.x-dev
Component: Code » field system
FileSize
599 bytes

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

#24
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.

katannshaw’s picture

@maximpodorov: Nice module! Thanks.

katannshaw’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.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

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

Kris77’s picture

I'm tried https://www.drupal.org/project/field_default_token with Drupal 8.5 and same to work.

Official Version of this module for Drupal 8?

imingridm’s picture

Hi guys, I have made a request form and for people who are logged in they do not need to type "name" and "login" in a way that these fields are filled in according to the person who is logged in. I downloaded this module and tried several in defalt value like [user: name] etc ... Does anyone know how I can do this? Thank you in advance

jonathanshaw’s picture

Hi ingrid.martins, please don't use existing issues like this that are bug reports or feature requests in order to get support. For that, Drupal Answers on stackexchange is often best.

imingridm’s picture

For some days I've been asking for help, if I had known that asking in the wrong place would have a jet response I would have done before;) Sorry

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

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.

JasonLuttrell’s picture

Hi all, I see this is still Active with the latest version of Drupal 9. I tried the module Field Default Token. It would not install because of an error. I don't recommend it because the module doesn't appear to have good support, it has not been updated in years, and there are a number of open issues. I know it's been a while but it would be great if this could still be addressed. Thanks.

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.

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

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ressa’s picture

Issue summary: View changes

Adding Entity Prepopulate for Drupal 9/10 in the Issue summary, and default value example.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.