[Drupal 7] How to remove the default date format option added by date module?

| | 2 min read

On a recent Drupal project we had developed, we had to use the Drupal date module to display the date in a popup in a custom form. However we noticed that a date description was appearing on the form. If you are facing the same situation in your Drupal site then read on to know how to remove the default date format option added by the date module.

We used the following code to display the pop-up date in our custom form.

<?php 
  $form['from_date'] = array(
	  '#type' => 'date_popup',
	  '#date_format' => 'Y-m-d',
	  '#title' => t('Date:'),
	  '#date_label_position' => '',
          '#description' => '',
	  '#size' => 10,
          '#required' => 'true',
	  );
?>

When we added the above code to the form, the date pop-up appeared but with an additional date format option.(Refer the screen shot).

date.jpg

While using a pop-up, we don't need the format, right?. So here is how we got rid of the text.

We have two solutions for this, one which is 'tricky' and the other which is 'proper'.

  1. The easy way

    The easiest way is to hide the class using CSS. However we won't recommend this as this is not the correct way to do this.

  2. The genuine Drupal way(Strictly our opinion).

    The correct way is to alter the element information using hook_element_info_alter.

For eg:
/**
 * implements hook_element_info_alter() 
 * 
 */
function hook_element_info_alter(&$type) {
  if (isset($type['date_popup'])) {
    $type['date_popup']['#process'][] = 'mymodule_date_popup_process_alter';
  }
}

/**
 * function to remove the description from date_popup
 * 
 */
 function mymodule_date_popup_process_alter(&$element, &$form_state, $context) {
  unset($element['date']['#description']);
  return $element;
}

Inside the hook_info_alter, we have called a custom function, if the element type is a date_popup. Inside the custom function, we had simply removed the description from the date element by unsetting it.

Hope this works for you too.