I am attempting to use an exposed Views Filter of the type Date: Date(node) on a Blog to allow a user to limit the Blog listing by Year or Year and Month.
I have set up the filter with a granularity of Month. I am also using Ajax, though I do not think that matters.
The exposed form for the filter has two drop down form elements, one for Year and another for Month.
When both Year and Month have been selected, it works fine.
If only one of the two is selected no filtering by date is done. This may be by design but I find it confusing.
What I would like to see is the ability to have a user select only the year and see all blogs for that year. If they choose to, they could further refine the search by selecting a Month. I suppose they could set just the month and see blogs for that month in any year (strange!!).
As it is, if both are not set, no filtering is occuring. If a user has selected a Year without also selecting the month, they see all blogs for all time when they probably think they should be seeing the blogs for the selected year.
Is there a better way to do this?
Comments
Comment #1
30equals CreditAttribution: 30equals commentedYeah, this is a problem indeed. My workaround for now is using hook_views_query_alter, check if the year filter is set, and add alter the query.
Comment #2
David4514 CreditAttribution: David4514 commentedI am hardly an expert Drupal developer. I thought that I had attempted that and found that the information I needed had already been cleared by the time the hook was driven. But then, I tried so many things it is a bit fuzzy.
Could you share a bit of what you actually did, ... code examples??
Thanks
Comment #3
KarenS CreditAttribution: KarenS commentedThe underlying issue is that the widget needs to check if the user is filling out all parts of the date. This only affect the select widget.
http://drupalcode.org/project/date.git/commit/4790d2c
Comment #4
30equals CreditAttribution: 30equals commentedSo with this commit, there isn't even an option anymore to only select the 'year' part if you set the granularity to month & year ? Not exactly the direction i was hoping for with a fix ;)
Comment #5
KarenS CreditAttribution: KarenS commentedThat is the way it was designed to work. The bug is that it was confusing that you could do it wrong. Changing this behavior to allow an either/or selection would take quite a bit more work and I have no time to do that nor plans to do that. None of the other selections has any option to work this way, only the drop-down selector *might* be made to do it, so it will also be confusing that they don't all do the same things.
I'll make it back into a feature request, but I have no plans to do anything with it.
Comment #6
vinoth.3v CreditAttribution: vinoth.3v commented+1
Comment #7
DamienMcKennaWhat would also be useful would be to add checkboxes to control whether the date parts are displayed, e.g. to only show the Year field.
Comment #8
talbet CreditAttribution: talbet commentedI needed similar functionality on a project I am working on and put something together after reading #1689274: Views exposed filter: when granularity is year-month and only year is specified, only results for January are returned:
It seems to work so far and I hope that it is useful, but I would also love to know if anyone can see any problems with using this as I am still fairly new to Drupal.
Comment #9
rodrigoaguileraThe problem that I see in the code of the last comment is that when you submit the form you lose the year-month select boxes and you get only month. Or at least is what I'm experiencing
Comment #10
Maurice M. CreditAttribution: Maurice M. commentedThanks talbet, that worked for me! Should be implemented as a checkbox in the date module in my opinion.
Comment #11
MrPeanut CreditAttribution: MrPeanut commentedI am trying to use the code from #8 but it doesn't seem to be altering the form. I've simplified it down to just this with no luck:
I am on Date 7.x-3.x. Would there possibly be a difference due to that?
Comment #12
DrCord CreditAttribution: DrCord commented#8 worked great for me, thanks.
I also don't see any missing year like in #9 but I am using a separate exposed filter for year from the one for month, so you can search independently by year or month.
@MrPeanut - I am using date 7.x-2.9. I am unsure how much changed in date 7.x-3.x, but I expect that could be your issue...
Comment #13
itapplication CreditAttribution: itapplication commentedAny solution for Drupal 8?
I want to add exposed filter for month in Drupal 8. Any help will be appreciate.
Thanks in advance.
Comment #14
sandyjai CreditAttribution: sandyjai commentedanyone please help me to this step by step , for implementing the #8 code. I put this in template.php but not working, the Module name is my theme name right?
Comment #15
sandyjai CreditAttribution: sandyjai commentedComment #16
icf-vpathak CreditAttribution: icf-vpathak commented#8 worked for me too. Thanks
Comment #17
DamienMcKennaComment #18
DamienMcKenna@sandyjai: It doesn't go in a theme, it goes in a custom module, take a look at the documentation for help doing this: https://www.drupal.org/docs/7/creating-custom-modules
Comment #19
sri_techie CreditAttribution: sri_techie commented#8 worked for me too. Thanks
Here is my code