Hi,

I'm doing a custom module using the date_popup form element, I need to set up a restriction on the calendar popup minDate jquery attribute but the #datepicker_options configuration array is not passed to the element configuration function. Here is my element

	$form['event_start_date'] = array(
			"#title" => t("Select the event start date"),
			"#type" => "date_popup",
			'#datepicker_options' => array('minDate'=>"0"),
	);		

I debug up to the part where the configuration array is merge with the incoming one but the datepicker_options array is empty in the line 332 of the date_popup module.

 $settings = $element['#datepicker_options'] + array(
    'changeMonth' => TRUE,
    'changeYear' => TRUE,
    'autoPopUp' => 'focus',
    'closeAtTop' => FALSE,
    'minDate' => '1',
    'speed' => 'immediate',
    'firstDay' => intval(variable_get('date_first_day', 0)),
    //'buttonImage' => base_path() . drupal_get_path('module', 'date_api') ."/images/calendar.png",
    //'buttonImageOnly' => TRUE,
    'dateFormat' => date_popup_format_to_popup(date_popup_date_format($element), 'datepicker'),
    'yearRange' => $year_range,
    // Custom setting, will be expanded in Drupal.behaviors.date_popup()
    'fromTo' => isset($fromto),
  );

Thank you for your help

Julian Mancera

Comments

Sylvain Lecoy’s picture

Seems also that the 'fromTo' value is not doing anything, and in the code, the $fromto variable is never assigned. Thus the 'fromTo' value is always FALSE by default;

I don't know either how to use the 'date_popup' element in a custom module form.

Any help would be appreciated!

Sylvain Lecoy’s picture

It is actually because the component is created on user focus(). See date_popup.js.

To fix it, I had to manually create the component through .datepicker in a custom script, then adding the class date-popup-init to avoid recreating it, then applies my custom settings.

It really mimics the javascript furnished by date popup module but it was the only way to overrides default behaviour.

haggins’s picture

Another method is to overwrite #process callback of your date_popup field. There you can overwrite the process function for the date part which itself passes datepicker settings to date_popups.js which passes it to .datepicker

See date_popup_element_process() and date_popup_process_date_part() how the default processes looks like.

If you also want to set function properties like onSelect or onClose you need your own script, ie:

(function ($) {
  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {

      for (var id in Drupal.settings.datePopup) {
        var datepicker_settings = Drupal.settings.datePopup[id].settings;
        datepicker_settings.onSelect = function(dataText) {
          alert('selected: ' + dataText);
        };

        $('#' + id).datepicker(datepicker_settings);
      }
    }
  };
})(jQuery);
thipa’s picture

This code is not working when validation fail. Any fix would be helpful?
datepicker_settings.onSelect = function(dataText) {
alert('selected: ' + dataText);
};

haggins’s picture

Can you please provide more information about what exactly is not working and how to reproduce the issue?

thipa’s picture

I want to control position of pop up calendar. so i reproduce this with beforeShow option. Every thing works fine as expected. But when the form validation fails, that beforeShow isn't call. when i log this value in console, it prints 'undefined'. Thanks.

haggins’s picture

Do you mean after form submission with validation error the beforeShow event doesn't work anymore? If so: is it important whether the validation error occurs on the date field or not?

thipa’s picture

Thanks.It fixed, i changed the js file called place, then it works.

Malakof’s picture

Hello, sorry to bring this thread back to life, but the code above is not working in my case. I have the following drupal behavior at the top of my script, and a datepicker in a webform. I want to be notified when a date is selected in the datepicker. In my form template i drupal_add_js a js starting with :

(function ($) {
    Drupal.behaviors.booking_form_tma = {
        attach: function (context, settings) {
            for (var id in Drupal.settings.date_popup) {
                alert('found');
                var datepicker_settings = Drupal.settings.date_popup[id].settings;
                datepicker_settings.onSelect = function(dataText) {
                    alert('selected: ' + dataText);
                };
                $('#' + id).datepicker(datepicker_settings);
            }
        }
    };

The behavior attach code gets called only once, after the page gets loaded, never reach the alert: of course the datepicker is not present in the div at this time. When the datepicker icon is clicked, the datepicker gets injected but the behavior is not called and so nothing happen. None of the alert in the code above are shown.

Note i change datePopup to date_popup who was undefined in my sources, but no matter what there is no date related in Drupal.sttings on the debugger.

Any clues ? Thanks.

haggins’s picture

Try Drupal.settings.datePopup instead of Drupal.settings.date_popup

bluetegu’s picture

Issue summary: View changes

I used hook_date_popup_process_alter for that, as settings tends to merge nicely.

function MY_MODULE_date_popup_process_alter(&$element, &$form_state, $context) {
  if (isset($context['form']) && @$context['form']['#form_id'] == 'YOUR FORM ID') {
    // add settings to the date popup widget
    $settings = array('minDate' => '0'); // avoid past day selection
    $return_id = $element['YOUR DATE ELEMENT']['#id'];
    $js_settings['datePopup'][$return_id] = array(
      'settings' => $settings
    );
    drupal_add_js($js_settings, 'setting');
  }
}