Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal CCK Date: how to set datetime field's default value to a fix date?

I have a CCK datetime field and would like to set its default value to 31 May 2011. When I go to the configuration of the field I can set the default value to Now, Blank or Relative.

Relative is to be set by a PHP's strtotime argument. However, it fails when I set it to

  • 31 May 2011 --> I get today in the node add form
  • last day of May 2011 --> I get an error on the field configuration page The Strtotime default value for the To Date is invalid.

(that should normally work according to http://php.net/manual/en/function.strtotime.php)

Do You have any idea how to set it to default to 31 May 2011?

like image 982
Michał Pękała Avatar asked May 03 '10 06:05

Michał Pękała


1 Answers

I think absolute dates are not yet supported in the "Customize Default Value" part of the CCK Date setup page. You should be able to do this via hook_form_alter in a custom module however (replace module name, $form_id, and field name with yours):

function mymodule_form_alter(&$form, $form_state, $form_id) {   
  if ($form_id == 'myform') {
    $mydate = date('Y-m-d', strtotime('31 May 2011')) ;
    $form['field_my_date'][0]['#default_value']['value'] = $mydate ;
  }
}
like image 67
Dan U. Avatar answered Oct 22 '22 19:10

Dan U.