Using the Date Widget with Drupal's Form API

I recently had to use the Date module in a custom booking form that was constructed using Drupal's awesome Form API. Now I'm sure someone is going to come along and prove me wrong but I couldn't find any documentation on the available widget types, form properties or much of anything to be honest. I did find a few wrong examples that were doing some weird hacks like overwriting #process. Here are a few examples to get you going with the Drupal Date widget that I found by digging through code, looking at other modules and just outright guessing. Below shows how to:

  1. Select box Drupal date widget
  2. Set the date format
  3. Set the date year range
  4. Set the date minute increment
<?php

$form['custom_date_field'] = array(
     
    // The drop down select widget.     
    '#type' => 'date_select',

    // This follows PHP's standard date letters. This is the format
    // that will be outputted.
    '#date_format' => 'd m Y H i',

    // The first number is how many years to go back, and the second forward.
    '#date_year_range' => '-0:+1', 

    // The minute increment.
    '#date_increment' => '15',
);

?>

As I find any more attributes and their interesting formats i'll be sure to post back.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.