3

I'm building a custom form in Drupal 7 and i need to use the date_popup widget in it. It all works as expected, except the title of the field.

My code is:

  $form['filter']['start_date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'd-m-Y',
    '#title' => t('Start date'),
  );

  $form['filter']['end_date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'd-m-Y',
    '#title' => t('End date'),
  );

But it keeps showing the default label "Date" for the field, as you can see on in the picture below. date_popup widget wrong title

Can you please let me know how can i put Start date and End date as field labels instead of just Date, or correct me if i'm doing something wrong

4

Just use the following code:

'#date_label_position' => 'none',

your code :

$form['filter']['start_date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'd-m-Y',
    '#title' => t('Start date'),
    '#date_label_position' => 'none',
  );

  $form['filter']['end_date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'd-m-Y',
    '#title' => t('End date'),
    '#date_label_position' => 'none',
  );
| improve this answer | |
3

You can unset the labels like below:

function <module_name>_date_popup_process_alter(&$element, &$form_state, $context) {
  if ($element['#name'] == '<fieldname>') {
    unset($element['date']['#title']);  
  }
}
| improve this answer | |
  • I don't want to unset the title, i just want to use titles i need, not default one. – Srdjan Marjanovic Apr 2 '15 at 8:33
  • The above code should unset the default – Kartagis Apr 2 '15 at 8:38
  • This is the best place to update the title, simply don't use the unset part. – Chris Rockwell Jun 21 '16 at 18:42
2

Solution i came up with is following:

  $form['filter']['start_date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'd-m-Y',
    '#title' => t('Start date'), //set desired title
    '#title_display' => 'invisible', //but hide it from showing
  );

Next, implement hook_element_info_alter():

function my_module_element_info_alter(&$type) {
  if (isset($type['date_popup'])) {
    $type['date_popup']['#process'][] = 'my_module_date_popup_process'; //name of the process function, can be anything
  }
}

and then set default title equals the title from form element:

//process function called from hook_element_info_alter
function my_module_date_popup_process($element, $form_state, $complete_form) {
  if ($complete_form['form_id']['#id'] == 'edit-my-form-id') {
    $element['date']['#title'] = $element['#title'];
  }
  return $element;
}

Result is like this: enter image description here

| improve this answer | |
  • Although this works, you can do it in one function by implementing hook_date_popup_process_alter() and set the title there. – Chris Rockwell Jun 21 '16 at 18:41
1

I only got this to work in Drupal 7.43 by hacking the date_popup module.

I added the following code in line 336 to the file date_popup.module found in sites/all/modules/contrib/date/date_popup/date_popup.module (your installation might omit the contrib folder)

// Fixes bug where title wasn't getting updated in hook
if (isset($element['#title'])) {
  $element['date']['#title'] = $element['#title'];
  $element['#title_display']  =  'invisible';
}

Hope this helps someone, I took a long time to figure it out.

| improve this answer | |

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.