Wednesday, 2 February 2011

Your very own date field

So here's the problem: you have your own hand-built form and you want to include a date field.

The difficulty that I keep running into with Drupal 7 is that everything is tailored around entities - the whole (amazing) Field API - requires an entity to operate on.

But I just want a configuration page with a date field (or in my case a whole bunch of date fields).

Well I have researched long and hard and come up with something very very easy:

$form['date'] = array(
  '#type' => 'date_popup',
);

Which will do it all for you. Watch out for the help text that says it should be "date-popup" because that's wrong. Your default date should be in the form "YYYY-MM-DD HH:MM:SS", you can also set timezones and lots of other useful stuff.

Find the date_popup.module file and search for "date_popup_element_info". It gives you all the defaults for this element type.

1 comment:

Kaustav Das Modak said...

Hey thanks! That was a real shortcut! Now I need to figure out if this will work with web services :)