Drupal - Customize days that can be clicked on a Calendar popup
You can pass some Datepicker options from php to the date_popup element through the '#datepicker_options' key:
<?php
$form['date'] = array(
'#title' => t('Pick the desired date'),
'#type' => 'date_popup',
'#datepicker_options' => array(
'minDate' => 0,
),
);
With this method you can pass almost any option but those that accept a function as value, as beforeShowDay
, the one needed to restrict weekends or holidays according to the answer referenced by Nikit: https://stackoverflow.com/questions/501943/can-the-jquery-ui-datepicker-be-made-to-disable-saturdays-and-sundays-and-holid
Hence, javascript is required. First you need to include a custom js file from your module code:
<?php
drupal_add_js(drupal_get_path('module', 'FOO') . '/FOO.js');
and provide some code in FOO.js
to extend the settings set by date_popup module with our own options.
Here's an example that add the basic option to disable weekends for all datepicker widgets in the page:
(function ($) {
Drupal.behaviors.FOO = {
attach: function (context) {
for (var id in Drupal.settings.datePopup) {
Drupal.settings.datePopup[id].settings.beforeShowDay = $.datepicker.noWeekends;
}
}
};
})(jQuery);
Extend to holidays is left as an exercise to the reader :)
Note: the three occurences of FOO
in my examples doesn't need to be the same literal, ie: you can provide a BAZ
behaviour in BAR.js
from FOO.module
.
Update: to extend the above for custom days availability, just add a function returning true/false for the day received by parameters.
(function ($) {
Drupal.behaviors.FOO = {
attach: function (context) {
for (var id in Drupal.settings.datePopup) {
Drupal.settings.datePopup[id].settings.beforeShowDay = checkDate;
}
}
};
function checkDate(date) {
if ((date.getDate() % 2) == 0) {
return [false, 'Even days not available'];
}
else {
return [true, 'Odd days are fine'];
}
}
})(jQuery);
You can find a more complete example in Date Restrictions module.