When creating a datetime field in other areas of drupal DateAPI allows you to order a select list of year values in the reverse of the default (with the largest date at the top instead of at the bottom). This is done by indicating a range of "+3:-3" instead of the default "-3:+3". But when I try to do something similar with the date filter (as my employer is very particular about the order of the date-select list), views tells me that I'm not following the right format. Would it be possible to remove this limitation, since dateAPI doesn't seem to actually have a problem with handling ranges in this format?
I can reverse the absolute date range, but the restriction exists for relative.
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#10 | 426920.10-reverse-year.patch | 1.81 KB | deviantintegral |
Comments
Comment #1
KarenS CreditAttribution: KarenS commentedI'm confused. I can't actually see any place in the Date API where this would work. Where are you able to do this and get a reverse order? And if you want things to work differently, can you provide a patch to do what you are trying to do?
Making it work would be a fine feature request, but it wasn't designed that way and I can't see any place that it will work that way now.
Comment #2
patcon CreditAttribution: patcon commentedSorry, I update my post later with that final line, but it was a little vague: I can't reverse the order wih relative dates anywhere, but I can with absolute dates in the filter date range input (with the "select" option) so that the filter can list either forwards or backwards.
Comment #3
okokokok CreditAttribution: okokokok commentedMerely taking out the check in date_api_filter_handler.inc::extra_options_validate() is not enough.
Comment #4
donquixote CreditAttribution: donquixote commentedHere is what you can do:
in date_api.module,
function date_range_years(),
line 2460 ff (EDIT: line number applies to an older version of date module)
(ok, yes, i will make this a patch soon)
And probably you should change the validation in
date_api_filter_handler.inc::extra_options_validate()
maybe reusing the date_range_years() function.
Comment #5
donquixote CreditAttribution: donquixote commentedI was on the wrong version, resulting in wrong line numbers.
The following patch is against HEAD, but I have no idea if it works.
Comment #6
okokokok CreditAttribution: okokokok commentedWorks well for me!
Comment #7
aidanlis CreditAttribution: aidanlis commentedDoesn't work for me ... the input is accepted but the list of years is not shown, only the selected year is shown in the drop down.
Comment #8
donquixote CreditAttribution: donquixote commentedAre you using the patch #5 on the lates dev version?
And are you talking about views filters or about node creation?
(on my test install it works well in both)
What exactly did you type in the configuration?
Comment #9
benkewell CreditAttribution: benkewell commentedcurrently running into the same issue on my view list
want to order the exposed date filter options in reverse order
although i believe it should be the date filter to handle reverse year range values,
the code below is what i used to solve the issue without hacking into date.module code
put the code in a new module and enable it
of course the code has to be modified to suite your site
the idea is to use hook_form_alter to hook into views exposed form
if it's the exposed form that need to be modified,
use #after_build to rewrite select options order before rendering
Comment #10
deviantintegral CreditAttribution: deviantintegral commentedI don't think the patch in #5 is needed anymore since #977002: #date_year_range breaks when #default_value is set on a date_select field got committed. Either way, I think a much cleaner method is to have a flag to indicate that the years should be reversed. The attached patch adds a #date_year_range_reverse flag, that when TRUE reverses the order of the years.
Comment #11
patcon CreditAttribution: patcon commented@deviantintegral, I appreciate the effort in making the patch, but my vote is still for simply switching the order of the text field components. I think that makes sense, as long as it's prominently and concisely explained in the help.
Actually... if this could be granular for months and dates as well, then maybe a checkbox wooouuuld make more sense...
Comment #12
deviantintegral CreditAttribution: deviantintegral commentedI agree; that would be better DX. However, I don't think date_popup supports that, so unless we do a patch upstream to support that in the JS for the popup calendar. I did think about reversing days and months as well, I just couldn't think of a situation where that was needed. But if we did go that route, I'd suggest renaming the property to #date_display_reverse, containing an array with possible values 'days', 'months', and 'years'.
Comment #13
LGLC CreditAttribution: LGLC commentedThanks deviantintegral. I used your patch but added in a couple of extra lines to ensure the default element stays as the first choice (which is a blank year in my case):
I have a lovely Date of Birth field now. Thanks!
Comment #14
rvilarSubscribe
Comment #15
alit CreditAttribution: alit commented@BenKewell, thanks for the snippet! #9 needs one small correction - must call array_reverse with the second parameter to preserve array keys:
The same solution for D7:
Comment #16
alit CreditAttribution: alit commentedUPD: #15 doesn't work properly. Here is the correct code:
Comment #17
Stephen OllmanThis finally worked for me in D7.
Custom module called 'date_reverse'.
Replace 'media' with your view name. or remove the view validation if you don't want to limit it to a view.
*********************
<?php
/**
* Implementation of hook_form_alter().
*/
function date_reverse_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$view = $form_state['view'];
if ($view->name == 'media') {
$form['date_filter']['#after_build'][] = 'date_reverse_views_exposed_form_date_order';
}
}
}
function date_reverse_views_exposed_form_date_order($element, &$form_state) {
$options = &$element['value']['year']['#options'];
$label = array_slice($options, 0, 1, TRUE);
$reverse_years = array_reverse(array_slice($options, 1, NULL, TRUE), TRUE);
$options = $label + $reverse_years;
return $element;
}
Comment #18
deviantintegral CreditAttribution: deviantintegral commentedAmazingly, the patch in #10 applies directly to 7.x-2.x and is working fine for me. No matter what direction this issue goes, it should be for 7.x anyways.
Comment #19
podarokbot restart
Comment #20
podarokComment #21
pedrospUsing Date 7.x-2.8, and dealing with a birthdate field with form API, default date should be NULL, and year select list as descendant.
Here's my recipe:
As @devianintegral said in #18, patch #10 against date/date_api/date_api_elements.inc still applies.
There is 4 parts on the patch, but the last one failed:
- Update of info text about #date_year_range_reverse
- Declaration of a new option/flag: #date_year_range_reverse and set it as FALSE as default
- Update of the function that create the select list and reverse if flag is true
- FAILED function date convert update? // no such function present, seems not to affect desired output.
Manual modification of date_api_elements after patch:
Sorry not to provide an updated patch, still no such skills yet.
UPDATE/EDIT 12 oct 2014
Simpler code, and I removed the array pop/unshift logic because keys were reseted.
Comment #22
podarokComment #23
nerdcore CreditAttribution: nerdcore at OpenConcept Consulting Inc. commentedI believe this has been resolved in #206127