Does the Views Selective Exposed Filters work for taxonomy, node reference, or other types of filters?

I really need one for node reference. Thanks.

Comments

infojunkie’s picture

Status: Active » Fixed

It should work for all types of exposed filters.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

patcon’s picture

Status: Closed (fixed) » Active

Hopefully I'm not off-protocol by opening this, but it seemed to be bang-on, topic-wise:

You say that it should work on any filter, but it seems that the date filter might be the exception. Am I missing something, or is this outside the scope of the module. If it is outside the scope, any pointers in the right direction (or explanations that it's nigh-on impossible), would be greatly appreciated!

Can't believe the low usage on your module, by the way! I'm going to start pimping it out, as it's pretty much a necessity. Auto-submit alone makes this a life-saver! Thanks :)

patcon’s picture

Component: Documentation » Code
infojunkie’s picture

The current version works for all filters of type select, checkbox, or radio. There may be exceptions of course, and I'm always thankful for bug reports.

The version I'm currently working on will also work for text filters, which the module will convert to a select box containing existing values.

roball’s picture

Title: Views Selective Exposed Filters, which filter is it for? » Views Selective Exposed Filters does not seem to work with date filters
Version: 6.x-1.x-dev » 6.x-1.0-beta1
Component: Code » Views Filters Selective

Hm, I have a exposed "Date: Date (node)" filter (coming from the date module) where users can select the year of a publication. The Date year range has been set to "2005:2010" - thus the selection list always offers to choose one from these 6 years. However, there are no publications from the year 2010 at all, so the selection list should not offer that option. However, after enabling the "Views Selective Exposed Filters" module there is no difference - there are still all years offered. Is this a bug? Thanks.

patcon’s picture

Sorry, never said thanks for the reply, infoJunkie! I'm seeing the same issue roball -- just assumed Date must be more complex, and so it was almost understandable that it wouldn't work so easily as other select lists :)

sheilaj’s picture

In order for the selection list to be limited to the items - dates, in this case - present in the result set, this option would have to be checked in the exposed filter ui. I used a 'Date: Date (node)' exposed filter as you did and I found that the option to 'Limit list to result set' was not presented. The option to 'Limit list to selected items' was also not presented and this option is produced by the Views module, not the Views Selected Exposed Filters module. Same thing happens with other types of date filters (Node: Post date, User: Created date).

I don't have a solution, just trying to narrow down the problem.

infojunkie’s picture

Title: Views Selective Exposed Filters does not seem to work with date filters » Views Selective Exposed Filters does not work with date filters
Category: support » bug

@roball: The module isn't picking up that the date filter is exposing a drop-down, and that's why it's not presenting a 'Limit list to result set' option, as noted by sheilaj.

@sheilaj: If you want to help out with this issue, you can find out the structure of the date exposed filter by placing a dsm($form['options']); in function views_filters_selective_form_views_ui_config_item_form_alter before the $overrideable check. This will help us understand why the check is failing. Or you can remove this check altogether to force the option to appear. But upon execution of the view, you will get SQL errors because the views_handler_field_filters_selective field handler is trying to inject the date filter into the SELECT clause of the query, and it's obviously getting the name of the date field wrong. That's the tricky part.

denniemans’s picture

Subscribe

tsi’s picture

subscribing

Steven Monetti’s picture

Status: Active » Needs review

Hey,

Here is a fix that I've developed. It works for me so try it out:

Add this code in this function: views_filters_selective_form_views_exposed_form_alter in file views_hacks/views_filters_selective/views_handler_field_filters_selective.inc around line 87.

//Date module fix
switch($form[$filter_element]['value']['#type']) {
  case 'date_select':
             
    //Unset #tree (which gives an error)
    unset($form[$filter_element]['#tree']);
            
    //Unset All option
    unset($options['All']);
       	    
    //Change the format of the value to be what we selected            
    foreach($options as $i => $option) {
      $options[$i] = date($form[$filter_element]['value']['#date_format'], strtotime($option));
    }   
			
    //Sort by date (associative sort)
    asort($options);
			
    //Add any to the top of the array with key = ''
    $options = array_reverse($options, true);
    $options[''] = '<Any>';  
    $options = array_reverse($options, true); 
			
    break;
}

..so that the function looks like this:

/**
 * Implementation of hook_form_FORMID_alter() for views_exposed_form.
 */
function views_filters_selective_form_views_exposed_form_alter(&$form, $form_state) {
  static $guard = FALSE;
  if ($guard) return;
  $guard = TRUE;

  

  // Go through each filter checking for a 'selective' setting.
  foreach ($form_state['view']->filter as $filter_id => $filter) {
    if (empty($filter->options['exposed'])) continue;
    if (empty($filter->options['expose']['vfs_selective'])) continue;

    // Form element is designated by the element ID which is user-configurable.
    $filter_element = $form['#info']["filter-$filter_id"]['value'];
    
    // Execute a clone of the view but without pager.
    $view = $filter->view->clone_view();
    if (empty($filter->options['expose']['vfs_active'])) {
      $view->set_exposed_input(array('dummy' => TRUE));
    }
    else {
      $view->set_exposed_input($filter->view->exposed_input);
    }
    $view->set_display($filter->view->current_display);
    $view->pre_execute();
    $view->set_items_per_page(0);
    $view->execute();
    if (empty($view->result)) continue;


    // Filter the results.
    list($limit_type, $field_id) = explode(':', $filter->options['expose']['vfs_field']);
    if ($field_id) {
      // Filter on a field name.
      $field_alias = $view->field[$field_id]->field_alias;
      $options = array();
      foreach ($view->result as $row) {
        if (isset($row->$field_alias)) {
          $options[] = $row->$field_alias;
        }
      }
    }
    else if ($handler = _views_filters_selective_get_handler($filter)) {
      $oids = array();
      foreach ($view->result as $result) {
        $oids[] = $result->{$view->base_field};
      }
      $oids = array_filter($oids);
      $options = empty($oids) ? array() : call_user_func($handler, $view->filter[$filter_id], $field_id ? $view->field[$field_id] : NULL, $oids);
    }
    else {
      drupal_set_message(t('Could not find a selective filter handler for %filter.', array('%filter' => $filter->definition['title'])), 'warning');
    }

    drupal_alter('views_filters_selective_options', $options, $view->filter[$filter_id], $field_id ? $view->field[$field_id] : NULL, $oids);
    if (!empty($options)) {
      if ($filter->options['expose']['optional']) {
        $options[] = 'All';
      }
      if (in_array($form[$filter_element]['#type'], array('select', 'checkboxes', 'radios'))) {
        $form[$filter_element]['#options'] = _views_filters_selective_reduce_options($form[$filter_element]['#options'], $options);
        
      }
      else {
        $options = array_combine($options, $options);
        if (isset($options['All'])) {
          unset($options['All']);
          $any_label = variable_get('views_exposed_filter_any_label', 'old_any') == 'old_any' ? '<Any>' : t('- Any -');
          $options = array_merge(array('All' => $any_label), $options);
        }       
        
        
        //Date module fix
        switch($form[$filter_element]['value']['#type']) {
          case 'date_select':
            
            //Unset #tree (which gives an error)
            unset($form[$filter_element]['#tree']);
            
            //Unset All option
            unset($options['All']);
       	    
       	    //Change the format of the value to be what we selected            
            foreach($options as $i => $option) {
                $options[$i] = date($form[$filter_element]['value']['#date_format'], strtotime($option));
            }   
			
			//Sort by date (associative sort)
			asort($options);
			
			//Add any to the top of the array with key = ''
            $options = array_reverse($options, true);
   			$options[''] = '<Any>';  
    		$options = array_reverse($options, true); 
			
          break;
        }
        
        
        $form[$filter_element]['#type'] = 'select';
        $form[$filter_element]['#multiple'] = FALSE;
        $form[$filter_element]['#options'] = $options;
        $form[$filter_element]['#default_value'] = 'All';
        $form[$filter_element]['#validated'] = TRUE; // avoid invalid selection error
        unset($form[$filter_element]['#size']);
                  
		
      }
    }

  }
  
  $guard = FALSE;
}
infojunkie’s picture

@stewy85: Thanks for the fix. I will likely create a new hook to allow other filter types to be handled by 3rd-party modules, and include your fix in my own implementation of the hook.

patcon’s picture

thanks stewy and infojunkie! You guys are awesome.

Just in case anyone needs a patch for a makefile to tide them over in the meantime...

(this is on dev by the way, not beta1, for anyone who's trying to patch)

patcon’s picture

Version: 6.x-1.0-beta1 » 6.x-1.x-dev

Oh hey, this doesn't work for me, but I've got a bit of a complicated set-up -- "date_filter field doesn't exist in "field list" column" or something to that affect.

But I'm using a user base table for my view, with a join to a the data module table with a custom unix date field. ie. don't let my scenario discourage anyone from trying, but I can't personally vouche for it working :)

infojunkie’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

This module is no longer maintained for D6 and has been deprecated in favour of https://drupal.org/project/views_selective_filters for D7. Please test there and reopen if necessary.