5

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?

Michał Pękała
  • 2,359
  • 5
  • 22
  • 32

1 Answers1

6

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 ;
  }
}
Dan U.
  • 1,357
  • 3
  • 12
  • 19
  • Thanks dor Your reply, Dan. I hope there's some simpler solution. Writing a hook for such a simple purpose I find not so nice, especially when many other forms like this appear and the module will become just a large switch instruction. ;-) – Michał Pękała May 03 '10 at 09:12
  • It would be nice if there was an easier way ... I checked some of the posts on http://drupal.org/project/issues/date?text=default and there doesn't seem to be, yet. E.g. see http://drupal.org/node/326439, http://drupal.org/node/331000, others. – Dan U. May 03 '10 at 10:40
  • I haven't found these posts befre. Thanks. Anyway, it's kind of bizarre that the simple feature is absent, while You can use strtotime(). Let's hope they'll make up for this. :) – Michał Pękała May 03 '10 at 11:26
  • Let us know if you find a better way! Also please upvote if you find this helpful. – Dan U. May 03 '10 at 11:40
  • Sure, I'll post the best solution found. I'd vote Your answer if only I had >=15 reputation that is requires to vote up. – Michał Pękała May 03 '10 at 11:55
  • I just tried this out and it does replace the value in the array with my date, but when the form is rendered it goes back to whatever the default was. Just curious if this actually worked for you guys. It could be problem with module weight for me. – jdwfly Apr 13 '11 at 23:21