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?