FAPI element 'date_popup' in block doesn't work with block cache enabled.

Created on 21 September 2012, over 12 years ago
Updated 20 March 2025, about 1 month ago

I'm creating a form which I'm showing in a block.
This form contains a date popup field.

I noticed the datepopup javascript is not working when block caching is enabled.
(It only works right after a cache clear.)
Somehow drupal_add_js isn't triggered anymore.

I fixed it by adding

'cache' => DRUPAL_NO_CACHE

to my block.

I couldn't find the real problem..

Code below.

/**
 * Implements hook_block_info().
 */
function hotels_block_info() {
  $blocks = array();
  $blocks['hotels-search'] = array(
    'info' => t('Hotels search block'),
   //it works if we add the line below
   //'cache' => DRUPAL_NO_CACHE, 
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 * 
 * Show a block with a the search form for hotels.
 */
function hotels_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'hotels-search':
      $block['subject'] = t('Search & book hotel');
      $block['content'] = drupal_get_form('hotels_search_form');
      break;
  }
  return $block;
}

function hotels_search_form($form, &$form_state) {
  $form['start'] = array(
    '#type' => 'date_popup',
    '#label' => '',
    '#date_format' => 'd-m-Y',
    '#date_label_position' => 'hidden',
    '#title' => t("From"),
    '#attributes' => array('placeholder' => t("Start date")),
    '#required' => TRUE,
    '#size' => 20,
    '#date_year_range' => '-0:+2',
  );

[...]
}
🐛 Bug report
Status

Active

Version

2.6

Component

Date Popup

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024