Currently, where granularity of a date exposed filter and contextual filter is set to year-month and user only specifies a year, the view returns results for January of the input year.
Instead, you would expect that results matching the whole year are returned.

Possible related issue:
#259308: Allow "fuzzy" granularity

Issue fork date-1689274

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

anrikun’s picture

If someone is interested in a solution to this issue, here is some code to add to a custom module to get this working.
It only applies to the case described above, that is, when exposed widget is date_select and granularity is month:

<?php
/**
 * Implements hook_form_FORM_ID_alter(); alter views_exposed_form.
 */
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  $form['#validate'][] = 'MYMODULE_views_exposed_form_validate';
}

function MYMODULE_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'] == 'Y-m'
     && empty($form_state['input'][$key]['value']['month'])) {
      $handler->options['granularity'] = 'year';
    }
  }
}
?>
gagarine’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev

Still an issue in 7.x

gagarine’s picture

Issue summary: View changes

minor rewrite

gagarine’s picture

To fix that for contextual filter:

function MYMODULE_views_pre_build(&$view){
    if($view->name=="archive") {
        // Test if we only have a year instead of Y-m
        if(!empty($view->args[0]) && drupal_strlen($view->args[0]) == 4){
            $view->argument['field_published_date_value']->arg_format = 'Y';
            $view->argument['field_published_date_value']->options['granularity'] = 'year';
        }
    }
}
wooody’s picture

Do I need to add this code in template.php file in the theme directory.. or I need to create new module...?
Thanks

anrikun’s picture

@gagarine: you forgot to mention that in if($view->name=="archive"), "archive" must be replaced by the actual name of the view the fix must apply to.
@wooody: you need to add these to a custom module.

anrikun’s picture

Issue summary: View changes

Also a bug for contextual filter.

walidvb’s picture

I am trying to implement that solution for drupal 7 (I believe anrikun's solution is d6/views2), without success. I tried several approaches.
The one I am trying now, is to set the filter to take a min and max value, and alter these according to the exposed filter input: https://gist.github.com/anonymous/999af94054b1b6c9218f

The other was based on the above mentionned solution, and is here:

function lezoo_form_views_exposed_form_alter(&$form, &$form_state) {
  if($form['#id'] == 'views-exposed-form-teaser-list-panel-pane-1')
  {
   $form['#validate'][] = 'lezoo_views_exposed_form_validate';

  }
}

function lezoo_views_exposed_form_validate(&$form, &$form_state) {

  $handlers = &$form_state['input'];
  foreach ($handlers as $key => $handler) {
    if (
      isset($form[$key]['value']['#type'])
      && $form[$key]['value']['#type'] == 'date_select'
      && $form[$key]['value']['#date_format'] == 'Y-m'
      && $form_state['input'][$key]['value']['month'] == '') {
      $form[$key]['value']['#date_format'] == 'Y';
    unset($form[$key]['value']['#granularity'][2]);
    unset($form_state['input'][$key]['value']['month']);
  }
}
dpm('form validation');
   // dpm($form);
   // dpm('form_state');
   // dpm($form_state);
}


function lezoo_views_query_alter(&$view, &$query) {
  //If a date was selected in the exposed filter, ignore the default date filter '> today'
  $conditions = $view->query->where['1']['conditions'];
  $date_index = 3;
  if(isset($view->query->where['1']['conditions'][$date_index]) &&
    //if a date exposed filter was set
   $view->query->where['1']['conditions'][$date_index]['field'] != 'node.nid')
  {
    unset($view->query->where['1']['conditions'][2]);
  }
} 
mrpeanut’s picture

Issue summary: View changes

@anrikun — Do you happen to have an updated example for Drupal 7 / Views 3?

@walidvb — Were you able to get your code in #6 working with Drupal 7 / Views 3? Doesn't seem to be working for me (but that could be user error).

doppel’s picture

Subscribing to this issue.

any news for D7?

undertext’s picture

Version: 7.x-2.x-dev » 7.x-2.7
StatusFileSize
new1.42 KB

Trying to find some workaround.
Here is a patch. Didn't fully test it yet. But it works in my case.

undertext’s picture

StatusFileSize
new2.08 KB

Forgot to include some code in the patch.
Here is the right one.

undertext’s picture

Status: Active » Needs review
StatusFileSize
new2.14 KB

Also added check if filter is really exposed.

Status: Needs review » Needs work

The last submitted patch, 11: views_date_exposed_filter-1689274-11.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: views_date_exposed_filter-1689274-11.patch, failed testing.

The last submitted patch, 9: views_date_exposed_filter-1689274-9.patch, failed testing.

The last submitted patch, 10: views_date_exposed_filter-1689274-10.patch, failed testing.

michaellenahan’s picture

@undertext - thank you. I can confirm that the patch in #11 works for me with date-7.x-2.8

PS With apologies for being off-topic, if anyone here is looking for how to swap the order of the dropdowns, so that 'Year' comes before 'Month', this works for me.

function MODULENAME_date_select_process_alter(&$element, &$form_state, $context) {
  if ((!empty($element['year'])) && (!empty($element['month']))) {
    $element['year']['#weight'] = 0;
    $element['month']['#weight'] = 1;
  }
}
nickonom’s picture

Even though the date-7.x-2.8 was released long time ago (on July 29, 2014 - 19:03) and it is not the current recommended version I downloaded and installed it just to make this work, as Michaell Enahan confirmed it did for him, however unfortunately all I got in CLI was:

patch < *.patch
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/date_api/date_api_elements.inc b/date_api/date_api_elements.inc
|index 57e4161..9cd4d9c 100644
|--- a/date_api/date_api_elements.inc
|+++ b/date_api/date_api_elements.inc
--------------------------
File to patch: date_api/date_api_elements.inc
patching file date_api/date_api_elements.inc
can't find file to patch at input line 23
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/date_views/includes/date_views_filter_handler_simple.inc b/date_views/includes/date_views_filter_handler_simple.inc
|index b84eac3..70fdc99 100644
|--- a/date_views/includes/date_views_filter_handler_simple.inc
|+++ b/date_views/includes/date_views_filter_handler_simple.inc
--------------------------
File to patch: date_views/includes/date_views_filter_handler_simple.inc
patching file date_views/includes/date_views_filter_handler_simple.inc
Niremizov’s picture

Also need to notice, that there is no check inside exposed_submit() - method for date_select filter type, without this check date popup stop working. Here is my version of exposed_input function, that also checks for default value of filter (I would just leave it here, probably would be useful for someone).

  /** 
   * Adds special handling for date_select form_type field.
   * 
   * {@inheritDoc}
   * @see views_handler::exposed_submit()
   */
  function exposed_submit(&$form, &$form_state) {
   if ($this->options['exposed'] && $this->options['form_type'] == 'date_select') {
      $date_form_state = $form_state['input'][$this->options['expose']['identifier']]['value'];
      $date_form = $form[$this->options['expose']['identifier']]['value'];
      $granularity = date_format_order($this->format);
      foreach ($granularity as $key => $value) {
        $is_value_submited = isset($date_form_state[$value]);
        // Form wasn't submited but field has default value, or form was submited
        // but with field is empty - in both cases we needed unset.
        if ((!$is_value_submited && empty($date_form[$value]['#default_value'])) ||
            ($is_value_submited && empty($date_form_state[$value]))) {
          unset($granularity[$key]);
        }
      }
      $this->format = date_limit_format($this->format, $granularity);
    }
  }

skylord’s picture

StatusFileSize
new2.18 KB

Here's reroll of patch in #11 with fix from #19 for date 2.10.

skylord’s picture

Version: 7.x-2.7 » 7.x-2.10
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 20: views_date_exposed_filter-1689274-20.patch, failed testing. View results

damienmckenna’s picture

Version: 7.x-2.10 » 7.x-2.x-dev
skylord’s picture

Status: Needs work » Needs review
StatusFileSize
new2.11 KB

Hm. After 4 years noticed patch not apply cleanly while updating old site. ;) It was because it was made after #1876168-170: Exposed grouped filter for date not working applied... So, here's fixed patch tested on 2.12

Status: Needs review » Needs work

The last submitted patch, 24: views_date_exposed_filter-1689274-24.patch, failed testing. View results

kunal_sahu made their first commit to this issue’s fork.

kunal_sahu’s picture

StatusFileSize
new4.64 KB

providing an patch . please review and update the patch . Thanks