5

How can I remove format description in date_popup form field?

$form['payable_by'] = array(
    '#type' => 'date_popup',
    '#title' => $amount !== '' ? t('Instalment payable date for instalment @label', array('@label' => $amount)) : t('Instalment payable date for new instalment'),
    '#title_display' => 'invisible',
    '#default_value' => $payable_by,
    '#parents' => array('instalment', $key, 'payable_by'),
        '#date_format' => 'Y-m-d',
        '#date_year_range' => '-0:+1',
  );

Please check the image

enter image description here

13

Normally, you would just set the #description property of the form element, and it would be rendered. With the date_popup element implemented by the Date module it seems it is not possible: The form element is made of two sub-elements for which the value of the #description property is not taken into consideration.

As there are two branches, I will report what I have noticed in the development snapshot for both the branches.

Branch 7.x-2

The "date_popup" element is defined with the following code.

function date_popup_element_info() {
  $timepicker = date_popup_get_preferred_timepicker();
  $type['date_popup'] = array(
    '#input' => TRUE,
    '#tree' => TRUE,
    '#date_timezone' => date_default_timezone(),
    '#date_flexible' => 0,
    '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
    '#timepicker' => variable_get('date_popup_timepicker', $timepicker),
    '#date_increment' => 1,
    '#date_year_range' => '-3:+3',
    '#date_label_position' => 'above',
    '#process' => array('date_popup_element_process'),
    '#value_callback' => 'date_popup_element_value_callback',
    '#theme_wrappers' => array('date_popup'),
  );
  if (module_exists('ctools')) {
    $type['date_popup']['#pre_render'] = array('ctools_dependent_pre_render');
  }
  return $type;
}

The element has a process function that execute the following code.

  $element['#tree'] = TRUE;
  $element['#theme_wrappers'] = array('date_popup');

  if (!empty($element['#ajax'])) {
    $element['#ajax'] += array(
      'trigger_as' => array('name' =>$element['#name']),
      'event' => 'change',
    );
  }

  $element['date'] = date_popup_process_date_part($element);
  $element['time'] = date_popup_process_time_part($element);

Looking at the code for date_popup_process_date_part(), I notice the following code.

  $sub_element['#value'] = $sub_element['#default_value'];
  // TODO, figure out exactly when we want this description. In many places it is not desired.
  $sub_element['#description'] = ' '. t('E.g., @date', array('@date' => date_format_date(date_now(), 'custom', date_popup_date_format($element))));

As the code that date_popup_element_process() executes before to return the element is the following one, there is still an alternative.

  $context = array(
   'form' => $form,
  );
  drupal_alter('date_popup_process', $element, $form_state, $context);

Implementing hook_date_popup_process_alter() you could remove the description.

function mymodule_date_popup_process_alter(&$element, &$form_state, $context) {
  unset($element['date']['#description']);
  unset($element['time']['#description']);
}

Similar code is present in the branch 6.x-2.

Branch 7.x-1

There are few differences with the branch I previously described. (See the code repository.)

The first is the code that in date_popup_process_date_part() sets the description, which is the following one.

  $sub_element['#value'] = $sub_element['#default_value'];
  // TODO, figure out exactly when we want this description. In many places it is not desired.
  $sub_element['#description'] = ' '. t('Format: @date', array('@date' => date_format_date(date_now(), 'custom', date_popup_date_format($element))));
  return $sub_element;

The second difference is that date_popup_element_process() (the process function for that element) is not invoking hook_date_popup_process_alter().

function date_popup_element_process($element, $form_state, $form) {
  date_popup_add();
  module_load_include('inc', 'date_api', 'date_api_elements');

  $element['#tree'] = TRUE;
  $element['#theme_wrappers'] = array('date_popup');

  $element['date'] = date_popup_process_date($element);
  $element['time'] = date_popup_process_time($element);

  if (isset($element['#element_validate'])) {
    array_push($element['#element_validate'], 'date_popup_validate');
  }
  else {
    $element['#element_validate'] = array('date_popup_validate');
  }
  return $element;
}

The only alternative in this case would be to implement hook_element_info_alter() to add a process function to the element.

function mymodule_element_info_alter(&$type) {
  if (isset($type['date_popup'])) {
    $type['date_popup']['#process'][] = 'mymodule_date_popup_process';
  }
}

function mymodule_date_popup_process($element, $form_state, $complete_form) {
  unset($element['date']['#description']);
  unset($element['time']['#description']);

  return $element;
}

The code for branch 6.x-1 is similar to the code of branch 6.x-2.

 

In both the cases, the comment reported in the Date module code suggests they will alter the code not to use the description that is now shown. For this reason, I would suggest to implement the necessary code in a custom module, and remove that code once the Date module is changed.

  • The implementation of hook_element_info_alter() to add a process function to the element worked for me, thanks – Jim Oct 22 '12 at 20:41
0

try this:

function promo_quote_date_popup_process($element, $form_state, $complete_form) {
      unset($element['date']['#description']);
      unset($element['date']['#title']);
      return $element;
}
function promo_quote_element_info_alter(&$type) {
  if (isset($type['date_popup'])) {
    $type['date_popup']['#process'][] = 'promo_quote_date_popup_process';
  }
}
0

In d 6.x there is no info_alter hook. But you can solve this by using pre_render.

In you form definition or by form alter you need to add a pre_render definition. Like this for example (in form definition):

$form['dateformname'] = array(
  '#title' => t('The date'),
  '#type' => 'date_popup',
  '#pre_render' => array('MODULENAME_date_prerender'),
);

And then implement that prerender function where you can unset the data sub element description. Example:

function MODULENAME_date_prerender($element) {
  unset($element['date']['#description']);
  return $element;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.