More usable “Authored On” date field widget for Drupal Forms

The default Drupal "Authored On" date field is not very usable, as it expects users to enter dates in a specific, and not very easy format.  Drupal does have better options for date input entry, from both the Date and jQuery UI  modules.  The Date module provides a very convenient function for using date popup widget for date entry.  It’s also smart enough to use jQuery UI’s datepicker if the latter is installed.  Add the following lines of code to your own hook_form_alter() function to make your content editors happy.

  1. // change authored on date to use jquery datepicker
  2. if (isset($form['author']['date']))
  3. {
  4. $form['author']['date']['#type'] = 'date_popup';
  5.  
  6. // prevent extra validation since its not a string
  7. unset($form['author']['date']['#maxlength']);
  8. }