Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am creating a form in Drupal 7 with the form API. Right now, I can create an ordinary calendar input field with the following code:

   $form['services_tab']['schedule_datepicker'] = array(
    '#title' => t('Pick the desired date'),
    '#type' => 'date_popup',
    '#date_year_range' => '0:0',
    '#date_format' => 'Y-m-d',
    '#prefix' => '<div id="datepicker-schedule">',
    '#suffix' => '</div>',
  );

Right now I can get the beautiful calendar popup to appear. What I am trying to achieve is enable or disable certain dates based on a rule like: disable all Saturdays and Sundays; or even remove the click state from specific dates based on a list of national holidays...

Is this something that can be achieved with PHP coding, or does it need to be done with jQuery? In case jQuery is the solution, I would appreciate any hints on how to do that... The calendar is a table, but table cells don't have an ID or something that could help me find and disable them based on a rule...

Thank you.

share|improve this question
1  
This "[How to disable weekends][1]" answer will help you for disabling weekends and some days. [1]: stackoverflow.com/questions/501943/… –  Nikit Jan 30 '13 at 5:07

2 Answers 2

up vote 5 down vote accepted

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: http://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.

share|improve this answer
    
Thank you, Jonhattan! I have successfully set the minDate and it disables all the past days. This is a great beginning for me. I wonder if I can set something like a maxDate, as I want to keep only a range of some 15 days enabled. I can see there is a 'fromTo' attribute, but have no clues on what it is going to do. My idea is to disable all dates in the past (done) and all dates in the future (some 15 days from now()). In the middle, I will check the date pickeer javascript documentation to see how to individually disable some of the open days. –  Marcos Buarque Feb 8 '13 at 12:25
1  
yes, there's also a maxDate: api.jqueryui.com/datepicker/#option-maxDate –  jonhattan Feb 8 '13 at 14:44
    
Thanks, for some reason, when I was looking into the documentation of the date_popup field I couldn't find information on the maxDate option. This worked for me and restricted the calendar to a maxDate: '#datepicker_options' => array( 'minDate' => 0, 'maxDate' => 30, ), –  Marcos Buarque Feb 22 '13 at 18:58
    
jonhattan, I am sorry to ask again. I am pulling my hair trying to execute a custom function. I have implemented your model and it works great for me. But now I want to execute a custom function to allow only specific dates. I see you have suggested the holidays as an exercise for the reader ;-) This reader is almost crazy trying to solve this problem. If it isn't a huge additional work for you, do you mind posting how you would solve this? Don't bother about details, I just need an overall help to do the call to customFunction(date). Thank you! –  Marcos Buarque Feb 22 '13 at 22:49
    
Marcos, I've updated my answer with a simple example of the usage of a custom function. –  jonhattan Apr 18 '13 at 19:29

I'm creating a form in Drupal 7 too with the form API. Right now, I try to disable Mondays. I include a js file in my module "Reservas" (reservas.module):

function reservas_init() {
$path = drupal_get_path('module', 'reservas');  

// load ui.datepicker from core
drupal_add_library('system', 'ui.datepicker');
// and our js
drupal_add_js($path . '/js/cal_improve.js');
}

and the code in file .js (cal_improve.js):

(function ($) {
    Drupal.behaviors.cal_improve = {
          attach: function (context) {
          for (var id in Drupal.settings.datePopup) {
          Drupal.settings.datePopup[id].settings.beforeShowDay =$('#onlyMondays').datepicker({beforeShowDay:onlyMondays});                  
          }
        }
      }


    onlyMondays: function(date){
          return [(date.getDay() == 1), ""];
    }

})(jQuery);

The calendar popup appear but mondays are enables. Any idea?. Thank you in advance!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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