I have a date field that needs an unusual default date: fixed day and month, but year=thisyear. That's outside the standard default date options.

I didn't want to write a custom module for one form field, so I tried this in template.php

function secta_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == 'qic-node-form') {  // form to make a new QIC entry

  $QICdefault= date('Y')."-12-01";
  $form["field_qic_join"]["und"][0]['#default_value']['value'] = $QICdefault; // set the default  join date to YYYY-12-01

  }   // This is in template.php so will only show if form is displayed using the theme template 


But there is no sign of the form field default changing. :-( What am I missing?

  • That form_alter array may not be right. IIRC, the syntax goes $form["field_qic_join"]['#default_value'] = date('Y')."-12-01"; Also, don't forget to clear caches. – murraybiscuit May 20 '15 at 14:27

Your Answer

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

Browse other questions tagged or ask your own question.