I'm looking for the simplest way in Drupal 7 to set simple dynamic content in the default value of a text field.

Specifically, I have a "Copyright notice" field, and I want the default value to be like this: "© Copyright Our Organisation [YEAR]", where [YEAR] is the current year. When the user creates a node in 2012, the form field is pre-populated "© Copyright Our Organisation 2012", and will be saved as this and stay as 2012 unless editted. When the user creates a node in 2013, the form field is pre-populated "© Copyright Our Organisation 2013" but all the old nodes made in 2012 don't change. EDIT: The field should still be editable, so users can replace it where it's not appropriate (e.g. if one piece of content they're uploading is actually "© Copyright Some Other Organisation 1981"): but the default should reflect the current year.

There seem to be a few options, but none of them seem particularly clean:

  • Using tokens. It seems that this requires either the token_filter module or the token_insert module. Then, the default value could include [current-date:custom:Y]. However, it's not so clean. For token_filter, the token from the default won't be translated until the user manually changes the input filter to "token filter" and hits save - bad for the user. For Token Insert, I believe the input field type would need to change to long text, and I'm not even sure it would work in the context of setting the default value.
  • Custom module. Something like, setting the default value to include some gibberish string like "_!YEAR!_", and then use something like hook_form_alter(); to find and replace. Or, defining some custom field that does this. Both involve fixing this specific combination of field and content firmly into code, which seems undesirable.
  • D7 CCK module. It exists (as a dev module) and apparently it has some feature to do with setting lists using PHP that didn't go into D7 core. Sounds not very applicable to this case however.
  • PHP filter. While you can set the Default Value field to use the PHP filter, in testing I found it doesn't actually apply the filter to the content you enter into the default value field. For example, putting © copyright Our Organisation <?php echo date('Y'); ?> into a default value field with PHP Filter applied just dumps that exact string, © copyright Our Organisation <?php echo date('Y'); ?>, into the field on the edit/create form, giving a user without access to the PHP filter nothing but some escaped text resembling PHP code, and giving a user with PHP filter access PHP code in the content of the field which continually processes, rather than becoming static text.

Am I missing something? Is there another way, or a variant on the above that is simpler?

  • With further testing, it seems like all the options based on filters keep the input content dynamic: it seems they would save the token or the PHP code into the field data, meaning a node created in 2012 but viewed in 2013 would have an undesired 2013 copyright notice. I can't find anything to apply the dynamic logic only the first time the default value is applied. – user56reinstatemonica8 Aug 7 '12 at 18:20
  • I am not sure why you would expect the old nodes to change their copyright note when a new node is created. If a node is talking of something copyrighted in 1981 from Company A, why would the copyright notice of that node change to show the current year, when a new node for a product made from Company A is created? – apaderno Sep 13 '12 at 18:36
  • I really don't understand the comment... Why would the existing node change? I'm just talking about pre-populating the field with something dynamically created, that is editable in the form, and saves as static content. Content that had already saved should never change unless changed manually - it's just a dynamic default – user56reinstatemonica8 Sep 13 '12 at 20:21
  • You said, "but all the old nodes made in 2012 don't change." That is what I would expect to happen: You are creating a new node, not editing the old nodes. – apaderno Sep 13 '12 at 20:34
  • Ah, okay: I specified that because, if you use options like tokens or a field with a PHP filter, then the token or PHP is saved to the node and so the text as rendered in the final output will become different when the year changes (or at least it will when the cache is refreshed). – user56reinstatemonica8 Sep 13 '12 at 23:07

you can write a module and implement hook_node_save(). If the node is new just set the value of the field (a simple text string to something like:

$node->field_copyright_notice[LANGUAGE_NONE][0]['value'] = "&copy; Copyright Our Organisation ".date('Y');

So in your own module you could do:

function MYMODULE_node_presave(&$node) {
 // Only do this for new nodes, eg nodes without a NID.
 if (!isset($node->nid)) {
     // Set our copyright text string.
     $node->field_copyright_notice[LANGUAGE_NONE][0]['value'] = "&copy; Copyright Our Organisation ".date("Y");

// You can also hide the field for new nodes with hook_form_alter().
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_NODETYPE_add_form') {
    $form['field_copyright_notice']['#access'] = FALSE;
  • Not bad, I've edited the question to make it clearer that the field should still be editable, but that's no problem as this could be adapted to find and replace a token-like string. – user56reinstatemonica8 Aug 7 '12 at 21:15
  • Well ... my version makes the value your copyright string on 1st save. It is editable after that from the node_TYPE_edit_form. Which meets your (initial) requirements. You can always just make a text field for your field and use FAPI to preset the value on node_TYPE_add_form a specific default string as I did above ... the initial node add form would then stuff the default value into the form, and allow for editing prior to first save if need be. The code is very similar to what I have above ... – tenken Aug 8 '12 at 3:21
  • 1
    hook_node_presave() is invoked when a node is saved in the database, whatever the node was created, or updated. The code shown in this answer would not make the field editable, and would keep updating the field every time the node is saved. Also, what is the reason to have a field, when nobody can edit it? – apaderno Sep 13 '12 at 18:40

Why not you use hook_form_alter and add a default value to the text field?

You have to check that the default value is only when someone is in node/add/content-type, not in node/nid/edit.

    // You can also hide the field for new nodes with hook_form_alter().
    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
     if ($form_id == 'node_NODETYPE_add_form') {
       $form['field_copyright_notice']['#default_value'] = t('here go my text');

To alter a field implemented using the field API from another module, you need to implement hook_field_widget_form_alter().

function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($element['#field_name'] == 'field to change' && empty($element['#default_value']) {
    $element['#default_value'] = 'default value';

The code works only if the default value set from the module implementing the field doesn't set a default value for the field, or the default value is an empty string. This means that, when the field is created through the user interface, its default value needs to be an empty string.


Here is a similar question: https://drupal.stackexchange.com/questions/39748/set-a-token-as-the-default-value-of-a-new-field

And probably the easiest way: Field Default Token module

And yes, the solution has to apply to only the DEFAULT value, not the actual value of the field itself, or else the tokens will get updated whenever you re-render the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.