I understand how to use the onDate function to check dates and return {selectable:false} to stop a date from being selectable.

But I can't get my head round how to do this if there are multiple date_popup fields which have different selectable dates (both the number of fields and the dates for each are variable) - how do I find the right instance of the datepicker and assign the right function/settings.

Any clues would be appreciated.

(Of course it would be lovely if the date_popup fields had a '#disallow_dates' field - or in my case '#allow_fields' since I have more disallowed than allowed but until then ... any ideas?)



adaddinsane’s picture

Is it impossible?

adaddinsane’s picture

Well, not entirely but it's been made hard.

Can I make some comments?

Currently the date_popup element overwrites the #theme_wrappers which is frustrating because it means I can't intercept any actions on that element.

However that wouldn't have helped anyway, because rather than using "#attached" to add the Javascript and settings, the module writes them directly with drupal_add_js().

If '#attached' was used I could have used #theme_wrappers to intercept and modify the settings to add the necessary code to disallow dates. (Adding my function and the disallow dates to the options.)

Obviously there is hook_js_alter() but I'll have to create spaghetti code to figure out which dates belong to which set of settings (even assuming I can - I haven't looked yet).

(Not that I don't appreciate the work - I really do, it's just that I need this and I can't have it.)

KarenS’s picture

If you know of a better way to do it that won't break other things, propose a patch.

adaddinsane’s picture

Hi Karen - sorry if it came across as unhappy. I know you've put in a huge amount of work over the years. Trouble is I have three days to finish this commercial project and it's a brick wall.

Anyway I'll be seeing what I can do in a day or so.

adaddinsane’s picture

Okay. I've done it :-)

Not as hard as you might expect but a bit fiddly because the 'beforeDayShow' callback gives you the date, but not the element so there's a bit of jiggery-pokery to make sure we select the right dates.

I've got a patch which I've attached - but its type is txt, sorry I run on Windows which can be a pain sometimes. I have no idea if the patch will work so I've also attached a zip of the module with the new code.

We have two new options: with '#date_disallow' all dates are allowed except the ones specified, while with '#date_allow' all dates are disallowed except the ones specified.

 * #date_disallow and #date_allow
 *   Together these define what dates can be made unselectable in
 *   a date popup. If both are  'all' all dates remain selectable.
 *   If neither are set to 'all' all dates are unselectable, this
 *   is because that choice makes no sense.
 *   To define a date or range of dates as allowed/disallowed define
 *   an array as follows:
 *   '#date_disallow' => array(
 *   	'YYYY-MM-DD' => array('aClass', 'aTip', 'yyyy-mm-dd'),
 *   	... repeat as required
 *   )
 *   or
 *   '#date_allow' => array(
 *   	'YYYY-MM-DD' => array('aClass', 'aTip', 'yyyy-mm-dd'),
 *   	... repeat as required
 *   )
 *   aClass will be inserted as a class for the date or dates specified
 *   by this definition. Likewise 'aTip' will be a pop-up hint on the date
 *   or range of dates (this is per the 'beforeShowDay' callback function).
 *   For either of these insert an empty string if not required.
 *   The second date (if specified) gives the end date (inclusive)
 *   of a date range specified by this line of the array.You can have
 *   as many definition lines as you wish. The definitions apply only
 *   to the element they are assigned to, different dates can be applied
 *   to different elements in the same form (as you would expect).

I haven't touched anything else in the code.

adaddinsane’s picture

Status: Active » Needs review
13.26 KB

Ooops, forgot to remove the debug code - just uploading the patch this time.

Also doesn't currently check the dates in the element validation.

adaddinsane’s picture

And that patch didn't work because of GMT/BST issues - I guess it's pretty limited on that front.

Anyway here's a newer version...

arlinsandbulte’s picture

Status: Needs review » Fixed

Date 7.x-1.x is unsupported.
This issue should be verified in the latest version of Date (7.x-2.x-dev) and re-opened if still valid.


arlinsandbulte’s picture

Status: Fixed » Closed (won't fix)

should have marked "won't fix" instead