When adding date popups to forms in custom modules I would like to be able to have more control over some of the underlying jquery datepicker settings. In particular I would like to disable the month and year dropdowns so the user needs to click forwards and back through the months and I would like to be able to specify the minimum date - so the user can't select a date in the past. I know I can check for the latter in the form validation, but it would be nice if past dates were removed from the picker interface.
Currently I can see that I can override these settings by editing date_popup.module and adding these lines to the end of the $settings array.
'changeMonth' => false,
'changeYear' => false,
'minDate' => 0,
But I'd much prefer to be able to control these settings from my custom module when I'm defining the form element. E.g. something like this:
$form['delivery_date'] = array(
'#date_label_position' => 'within',
'#type' => 'date_popup',
'#date_format' => $display_date_format,
'#default_value' => $default_date,
'#date_timezone' => date_default_timezone_name(),
'#date_year_range' => '-0:+1',
'#size' => 15,
'#settings' => array(
'changeMonth' => false,
'changeYear' => false,
'minDate' => 0,
),
);
As I wrote this post I realised that it wouldn't be too hard to implement. So I'm attaching a patch for the date_popup.module that should do it. This will allow any of the jquery datepicker settings to be configured from the form element definition, exactly as my example above.
- Drasgard.
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | override_datepicker_settings-510464-2.patch | 623 bytes | eliosh |
| #13 | override_datepicker_settings-510464-1.patch | 711 bytes | googletorp |
| #12 | date_popup_settings_alter.patch | 450 bytes | alexpott |
| overrideSettingsArray.patch | 1.1 KB | drasgardian |
Comments
Comment #1
drasgardian commentedComment #2
elioshYou are THE man.
This patch works like a charm.
Hope to see this ASAP on a new version.
Comment #3
elioshComment #4
Jorrit commentedI think in some cases it would be better to make the jQuery settings "first class citizens". I personally would like to supply maxdate and mindate. It would work perfectly with the suggested patch, but if it would be made available by using '#maxdate' and '#mindate' (or something similar), the validator function would also be able to enforce these settings. In addition to that, I also would like to disable certain specific dates using the beforeShowDay callback. For this, also support in the validator function would be useful. For other settings, such as the animation settings, it is not necessary to have them as separate # keys in the element definition and this patch would work great there.
Comment #5
Jorrit commentedI have created a patch that adds the functionality I mentioned. Because more issues request this, I've created a separate issue at #625264: Improvements to date_popup module. I would be very happy if you could take a look at it.
Comment #6
j0rd commented+1 for this feature integrated. Perhaps not this patch as I have no tried it, but I need the ability to add the jquery UI features when I edit the content type's field.
Also it would be nice to provide a hook in date_popup_process_date so we can overwrite the settings in code as well.
--
Drupal & Ubercart Themes
Comment #7
alonpeer commented+1 for integrating this feature.
Comment #8
mnlund commented+1 for integrating this feature.
Comment #9
mnlund commentedHow about a simple drupal_alter to make additions to the $settings array?
On line 243 in date_popup.module a
drupal_alter('date_popup_settings', $settings);would be sufficient.Comment #10
raphael waeselynck commented+1 for integrating this feature. I do not understand how such a useful feature is still not integrated almost one year after its creation...Furthermore the patch is quite simple, efficient...and has been tested, so... what are we waiting for ? :)
Comment #11
sdelbosc commentedI like the idea suggested in post #9. My team will test it and deliver a patch if it is working.
Comment #12
alexpottBuilding on the idea suggested in #9 - I've put it in date_popup_js_settings_id function because then you can target the #id
For example... in a site_helper_example.module file...
Comment #13
googletorp commentedSince it can be quite hard to know which element is being created and it depends on the context of the form, I think the best option for this, is to add it as an array to the form element.
I've taken the idea by drasgardian in the orignal post, and tweaked it a bit, in this new patch.
Comment #14
kvvnn commentedThanks for this. Could something similar help us with http://drupal.org/node/852706 (restrict selection to certain days)?
Or, is there a way to add the actual years / months / days to the form element for manipulation?
Comment #15
mattiasj commented+1 for integrating this feature.
Comment #16
marthinal commented@drasgardian thanks for the patch and start the issue :)
Comment #17
googletorp commented#14: The datepicker in it's current form doesn't allow canceling out or only allowing mondays. So that's not a limitation because of Drupal. The patch from #13 allows setting a max and min date which would make it possible to create an allowed data range. With all the datepicker settings exposed, any limitation would come from the jQuery javascript plugin used, and not the Drupal form definition.
Comment #18
kvvnn commentedgoogletorp : Yeah. I ended up pushing allowed dates to Drupal.settings, restricting all dates except those using jQuery on the month / year select box changes, and then setting up a validation function for submit in case someone gets around the javascript.
Comment #19
karens commentedThere are three different approaches in this issue, the first was marked reviewed and tested and then others were added. Pretty hard to do anything with all this confusion.
- I need confirmation about *which* patch is proposed.
- I need people to confirm that that patch works, both in jquery ui 1.6 and jquery ui 1.7 (because both are used in D6)
- I need people to confirm that the patch does not break anything for people who are not trying to override the settings.
Comment #20
googletorp commented#19: The patch from #13 is a very simple fix, that allows users to edit the default values that is provided with the date module. If you really really wanted to break things, it would be possible if you supplied invalid values for the settings. But this is no different from any hook in Drupal really.
One thing to note is that Patch #13 and patch #0 provide the same functionality. #13 is made with the aim of doing the same thing in a more simple way and use a more unique key in the form item (#datepicker_settings vs #settings).
Comment #21
karens commentedAnother issue, #625264: Improvements to date_popup module, addresses the same idea and we need to have a single issue with a single patch. I can't do anything at all until we have a single issue with a single patch. One of these has to be marked a duplicate and everyone needs to work on the other until we have a patch that everyone agrees will work.
Also the comment in #20 did not answer any of my questions.
Comment #22
googletorp commentedI'll try to answer a bit better.
#13
Patch #13 is what is on the table now, which is an improved version of patch #0. It's the most simple way to make it possible to change the default settings per form field IMO. Unless any ones steps up with something better, this is what we want.
Wont effect them.
If nothing is done, same same defaults that has been used so far will be used. So with no change, it wont break or even change anything.
Works fine.
The patch allows people to change the settings that is passed to jquery ui. This gives power and responsibility to the developer. Since all that is changeable is the settings that is already passed along to UI 1.6 / UI 1.7 this patch will work great with both versions. The supported settings is not something we can control, so it will be up to the developer not to only use settings that jQuery can handle. They could change those settings with JS anyways if they really wanted to, this just add convenience.
That was all the questions you wanted answered. I can understand you want a single issue, but I still wanted to update this issue with current status.
Comment #23
elioshUpdated patch #13 to last date_popup version.
Comment #24
arlinsandbulte commentedBump,
can someone take a closer look at KarenS's comments in #21 and provide a suggested direction?
Comment #25
Jorrit commentedThe difference between this patch and the one in #625264 is that this one just allows adding items to the javascript date picker options object, while the other adds more advanced stuff like validation and interpretation of some of the date picker options.
I think the two patches can be combined by combining the advanced stuff with the option to override the date picker options that are not covered by patch #625264.
Comment #26
rukaya commentedI prefer patch #12 because it allows you to target specific date popup fields which I think a lot of people are going to need. It really seems like this ought to be integrated into the edit cck field form too like the yearRange is.
Comment #27
lperalta commented#23 working fine in dev without breaking default functionality.
To use it, go to your project_extras.module file (if you hadn't created it yet, now is the time.) and use the something like the following:
* in this example i'm adding settings to a date_popup from a view's exposed date filter.
Comment #28
Jorrit commentedI have combined the patch from comment #23 with my patch from #625264. It's posted at #625264: Improvements to date_popup module
Comment #29
arlinsandbulte commentedThanks!
I am going to mark this as duplicate now that this issue has merged with #625264: Improvements to date_popup module