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

30equals’s picture

Yeah, 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.

David4514’s picture

I 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

KarenS’s picture

Title: Using Date: Date(node) Views Filter With Month Granularity - No filtering occurs if only Year selected » Exposed widget using select filter is not checking that all parts are filled out
Status: Active » Fixed

The 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

30equals’s picture

So 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 ;)

KarenS’s picture

Title: Exposed widget using select filter is not checking that all parts are filled out » Make it possible to select only month or only year in exposed filter
Status: Fixed » Active

That 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.

vinoth.3v’s picture

+1

DamienMcKenna’s picture

What 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.

talbet’s picture

I 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:

/**
 * Impliments hook_form_views_exposed_form_alter
 *
 * Used to change exposed date filter to month only, as this is not available in the views UI
 */
function MODULENAME_form_views_exposed_form_alter(&$form, $form_state) {
  if( $form['#id'] == 'form_id') {
    // Switch date formatter to month only
    $form['field_date']['value']['#date_format'] = 'm';
    // Add extra validator to handle filter submit
    $form['#validate'][] = 'MODULENAME_views_exposed_form_validate';
  }
}
/**
 * Custom validator for exposed date filter
 */
function MODULENAME_views_exposed_form_validate(&$form, &$form_state) {
  $handlers = &$form_state['view']->filter;
  foreach ($handlers as $key => $handler) {
    if (!empty($handler->options['exposed'])
     && isset($form[$key]['value']['#type'])
     && $form[$key]['value']['#type'] == 'date_select'
     && $form[$key]['value']['#date_format'] == 'm'){
      $handler->options['granularity'] = 'month';
      $handler->format = 'm';
    }
  }
}

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.

rodrigoaguilera’s picture

Issue summary: View changes

The 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

Maurice M.’s picture

Thanks talbet, that worked for me! Should be implemented as a checkbox in the date module in my opinion.

MrPeanut’s picture

I 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:

function mrpeanut_form_views_exposed_form_alter(&$form, $form_state) {
    // Switch date formatter to month only
    $form['field_event_date']['value']['#date_format'] = 'm';
}

I am on Date 7.x-3.x. Would there possibly be a difference due to that?

DrCord’s picture

#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...

itapplication’s picture

Any solution for Drupal 8?

I want to add exposed filter for month in Drupal 8. Any help will be appreciate.

Thanks in advance.

sandyjai’s picture

anyone 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?

sandyjai’s picture

Issue tags: +Make it possible to select only month or only year in exposed filter
icf-vpathak’s picture

#8 worked for me too. Thanks

DamienMcKenna’s picture

Issue tags: -Make it possible to select only month or only year in exposed filter
DamienMcKenna’s picture

@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

sri_techie’s picture

#8 worked for me too. Thanks

Here is my code

<?php


/**
 * Implements hook_form_FORM_ID_alter(); alter views_exposed_form.
 */
function news_archive_form_views_exposed_form_alter(&$form, &$form_state) {
	if( $form['#id'] == 'views-exposed-form-news-page-1') {
    // Switch date formatter to month only
    $form['field_posted_date_value_1']['value']['#date_format'] = 'm';
    // Add extra validator to handle filter submit
    $form['#validate'][] = 'news_archive_views_exposed_form_validate';
  }
 
}

function news_archive_views_exposed_form_validate(&$form, &$form_state) {
	$handlers = &$form_state['view']->filter;

  foreach ($handlers as $key => $handler) {
    if (!empty($handler->options['exposed'])
     && isset($form[$key]['value']['#type'])
     && $form[$key]['value']['#type'] == 'date_select'
     && $form[$key]['value']['#date_format'] == 'm'){
      $handler->options['granularity'] = 'month';
      $handler->format = 'm';
    }
  }
}