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

Files: 
CommentFileSizeAuthor
#24 token-support-default-field-settings-1070878-24.patch667 bytesPascoual
Test request sent.
[ View ]
#20 token-support-default-field-settings-1070878-20.patch599 bytesPascoual
PASSED: [[SimpleTest]]: [MySQL] 39,558 pass(es).
[ View ]
#8 tokens_as_default.zip2.41 KBkmh

Comments

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.

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.

Subscribe

subscribe

subscribe

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

StatusFileSize
new2.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!

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.

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!

Subscribe

Subscribe

Subscribing

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.

Quite agree!

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

Currently, only viewfield supports this:
dd

Title:Allow tokens to be used in the default value and help text for a fieldAllow 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.

subscribing

subscribing

StatusFileSize
new1.03 KB
new1.17 KB
new1.15 KB

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

Project:Token» Drupal core
Version:7.x-1.x-dev» 7.x-dev
Component:Code» field system
StatusFileSize
new599 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.

Status:Active» Needs review

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

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

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

StatusFileSize
new667 bytes
Test request sent.
[ View ]

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));

Status:Needs review» Needs work

Patch failed to apply.

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.

Assigned:pokurek» Unassigned

Sorry, for assign. :( My fault.

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

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.

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

Great module! Patch in #20 worked.

Temporary (and ugly a bit) workaround:

<?php
/**
* 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?

#24
It works for me. Thanks!

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.

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.

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 .

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

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

@maximpodorov: Nice module! Thanks.

Issue summary:View changes

Add module link

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.

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

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

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

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.