I'm using the selective filters options in my views and it is REALLY helpful.

Would it be possible to take this a step further and not display the entire exposed filter if there are no terms in the filter?

Thanks.

------------------------------------------------

Requested feature:

- Default behaviour of module would be to disable the widget if there are no results.
- Additional checkbox in filter configuration that says "Hide if empty" that will completely hide widget instead of disabling.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

infojunkie’s picture

Thanks for the request. I've also received another request to disable the filter if there are no terms. So I guess this will be an additional option.

xl_cheese’s picture

Great!

I'm using Product Attributes exposed filters. It displays all the options within the products in the result set of a particular attribute.

With this functionality I can keep the same taxonomy menu structure and be able to display products with different attributes cleanly. Not sure if that makes any sense, but it'll help me a TON.

infojunkie’s picture

Version: 6.x-1.0-beta1 » 6.x-1.x-dev
Category: support » feature
Status: Active » Fixed

Implemented in the latest dev. Please try t (12 hours from now) and let me know how it works for you.

xl_cheese’s picture

I've installed the latest dev. I'm not sure that I see any options to add this functionality? Should I see something when editing the exposed filter?

Thanks.

infojunkie’s picture

Maybe you downloaded the dev too early, before the new changes were integrated. I checked now and the new code is in. You should find an option "Hide if empty" inside the selective filter options.

xl_cheese’s picture

I have the option now. When I select it an save I get this error message on my view page.

I'm using product attributes exposed filter.

* user warning: Table 'thepinkg_drp1.content_type_exhausts' doesn't exist query: SELECT DISTINCT(field_manufacture_exhaust_value) FROM content_type_exhausts WHERE nid IN (1018,1017,1016,989,990,973,886,982,981,890,991,983,992,997,1001,861) in /home2/thepinkg/public_html/sites/all/modules/views_hacks/views_filters_selective/views_filters_selective.module on line 168.
* user warning: Table 'thepinkg_drp1.uc_product_options' doesn't exist query: SELECT DISTINCT(oid) FROM uc_product_options WHERE nid IN (1018,1017,1016,989,990,973,886,982,981,890,991,983,992,997,1001,861) in /home2/thepinkg/public_html/sites/all/modules/views_hacks/views_filters_selective/views_filters_selective.module on line 168.
* user warning: Table 'thepinkg_drp1.uc_product_options' doesn't exist query: SELECT DISTINCT(oid) FROM uc_product_options WHERE nid IN (1018,1017,1016,989,990,973,886,982,981,890,991,983,992,997,1001,861) in /home2/thepinkg/public_html/sites/all/modules/views_hacks/views_filters_selective/views_filters_selective.module on line 168.

xl_cheese’s picture

I'm actually getting these errors just from the update to dev rather than selecting the hide if empty box.

infojunkie’s picture

Check the settings again. Perhaps you need to explicitly choose a field to restrict the values? If you've upgraded from an old dev this might very well be the case.

xl_cheese’s picture

I initally left the restrict field to default. I've changed it to title, I do not get any limiting of the filters. It shows all possible. The errors do go away however.

I also seem to have some illegal choice errors that show an empty view. For this error I only have the limit option set to title and hide unchecked.

infojunkie’s picture

In order for the restrict field to work, it needs to exactly correspond to the values in the exposed filter. You can create hidden fields in the view, and select those to restrict the exposed filter values.

xl_cheese’s picture

So you're saying I should not be able to use a current field that's in the view? I need to have a hidden one for the limiting to work?

The only way I've been able to remove all the errors in the view is to completely turn off limiting.

Would you be able to post a screenshot showing which options I should select to get it to work?

Thanks.

infojunkie’s picture

For a field to be used as limiting value for an exposed filter, it needs to contain the same values that go into the exposed filter drop-down. For example, if you want to limit a user drop-down, you need to use a "User: Uid" field and set this as your limiting field. You will probably want to hide this field since the view display should not show it to the end-user.

Does that make more sense?

Status: Fixed » Closed (fixed)

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

GiorgosK’s picture

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

I am using views 6.x 3.x
with latest views hacks 6.x-1.0-beta2

and either I don't understand how this is implemented or its not working properly

I have enabled it for a taxonomy term
- Limit "Taxonomy: Term" values to result set
- Further limit values to active filters
- limiting field > default
- Hide if empty

and there are cases that some widgets are empty but still shown

I have also tried with creating a hidden "taxonomy term" of the same kind as the exposed filter
- Limit "Taxonomy: Term" values to result set
- Further limit values to active filters
- limiting field > (the hiden taxonomy field above)
- Hide if empty

but still I can see empty exposed filters
can you give an easy to follow example so that I know I am using it the right way ?

workaround is to modify the views-exposed-form.tpl.php to prevent empty widgets from showing up

<?php if (!empty($q)): ?>
  <?php
    // This ensures that, if clean URLs are off, the 'q' is added first so that
    // it shows up first in the URL.
    print $q;
  ?>
<?php endif; ?>
<div class="views-exposed-form">
  <div class="views-exposed-widgets clear-block">
    <?php foreach($widgets as $id => $widget): ?>
      <?php if (!empty($widget->widget)): ?>
      <div class="views-exposed-widget views-widget-<?php print $id ?>">
        <?php if (!empty($widget->label)): ?>
          <label for="<?php print $widget->id; ?>">
            <?php print $widget->label; ?>
          </label>
        <?php endif; ?>
        <?php if (!empty($widget->operator)): ?>
          <div class="views-operator">
            <?php print $widget->operator; ?>
          </div>
        <?php endif; ?>
        <div class="views-widget">
          <?php print $widget->widget; ?>
        </div>
      </div>
      <?php endif; ?>
    <?php endforeach; ?>
    <?php if (!empty($sort_by)): ?>
      <div class="views-exposed-widget">
        <?php print $sort_by; ?>
      </div>
      <div class="views-exposed-widget">
        <?php print $sort_order; ?>
      </div>
    <?php endif; ?>
    <?php if (!empty($items_per_page)): ?>
      <div class="views-exposed-widget">
        <?php print $items_per_page; ?>
      </div>
    <?php endif; ?>
    <?php if (!empty($offset)): ?>
      <div class="views-exposed-widget">
        <?php print $offset; ?>
      </div>
    <?php endif; ?>
    <div class="views-exposed-widget views-submit-button">
      <?php print $button ?>
    </div>
    <?php if (!empty($reset_button)): ?>
      <div class="views-exposed-widget views-reset-button">
        <?php print $reset_button; ?>
      </div>
    <?php endif; ?>
  </div>
</div>
GiorgosK’s picture

Category: feature » support
Status: Closed (fixed) » Active

its at least a support request

infojunkie’s picture

You're doing the right thing AFAICT. There's a condition that triggers the hiding of filters in views_filters_selective.module:

if ((empty($form[$filter_element]['#options']) || array_keys($form[$filter_element]['#options']) === array('All')) && !empty($settings[$filter_id]['vfs_hide_empty'])) {

Can you tell if the code goes inside this block? Use dsm('inside'); inside the block. Otherwise, please find out which condition prevents the code from going inside.

scuba_fly’s picture

I think I have the same problem.
The 'label' shows up if all terms of that vocabulary are hidden because there are no nodes of that term.

To make myself a bit more clear:
I have a vocabulary lets call it 'colors'.
I have a view that currently shows nodes of which none of them have a color assigned. but there might be a node with a color in the future.
So in that case i don't want to show the label for the vocabulary 'color'.

If i check the checkbox 'Hide if empty' in the exposed filter taxonomy term, the terms lets call them 'yellow, black, white' are hidden but the label 'color' still shows up.

I've installed the latest dev version from 2011-Jul-28 but that did not help either.

glintwine’s picture

Same problem, but I solved it.
When you add exposed filter, It is necessary to leave the default value in Filter identifier. For example: field_product_color_value_many_to_one. If it is cut to color, the label will not be hiding.

I was searching for a very long time this mistake.

scuba_fly’s picture

Thanks for your reply, but how do i find the default value if I've already changed it?

abugge’s picture

Version: 6.x-1.0-beta2 » 7.x-1.0-alpha2

Is this something that is conna be inccluded in the future 7.xx branch?
I desperatly need at solution to this problem.

1kenthomas’s picture

Issue summary: View changes

Looks like this would require a programmatic (template at least) solution in the 7.x branch (I just tested). I'll post example code here once (if) I have it.

infojunkie’s picture

Project: Views Hacks » Views Selective Filters
Version: 7.x-1.0-alpha2 » 7.x-1.x-dev
Component: General » Code

Changed the project as Views Selective Filters has moved to its own module.

david_garcia’s picture

Category: Support request » Feature request
Issue summary: View changes
Status: Active » Needs work

Updated issue description.

ConradFlashback’s picture

+1

jakabadambalazs’s picture

Actually, it should be hidden/removed if empty or only one available choice is present for filter - there is not much use for a selective filter if there is only one option to select.

victoriachan’s picture

Agree with #25 that this would be great for D7! Thanks!

timwood’s picture

Any thoughts or work being done for this as a feature request for the 7.x version?
Thanks!

alison’s picture

Here here! This would be fantabulous!!!!!!!!!!

lukasss’s picture

you can use a theming widget:

function YOURTHEME_preprocess_views_exposed_form(&$variables) {
  if ($variables['form']['#id'] == 'views-exposed-form-VIEWNAME-DISPLAYID' && empty($variables['form']['field_NAME_FILTER_selective']['#options'])) {  
    dsm($variables);
    unset($variables['widgets']['filter-field_NAME_FILTER_selective']);
  }
}
adam1’s picture

I am looking for the same feature – is there any progress in this case?

Richard15’s picture

According to lukasss #29, I put in my template.php:

function PORTO_SUB_preprocess_views_exposed_form(&$variables) {
  if ($variables['form']['#id'] == 'views-exposed-form-VIEWNAME-DISPLAYID' && count($variables['form']['FIELD_FIELD']['#options']) < 2)
  {
    unset($variables['widgets']['filter-FIELD_MACHINE_NAME_value_selective']);
  }
}

I flagged the option "Required" on Filter Criteria, so I don't have the option "Any".
In that case change "2" with "3" on the code.

Thanks again lukasss!

spelcheck’s picture

#29 works great, thanks lukasss! Also thanks Richard15 for cleaning it up further.

MPeli’s picture

#29 works fine. Thank you!

wjhessels’s picture

Got it working with #29 and #31 Thanks!

Engineer_UA’s picture

#29 How to build the proper " Foreach (): endforeach; " structure in case of more than one widget?

cozzamara’s picture

Please insert the code #29 into the module to make it easy....Thanks

cozzamara’s picture

If the module's manteiner can add the property "#access" to the selective fields, the hiding of empty field can do with

hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id)

with this code:

function hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id){
	if (module_exists('devel')){
		dsm($form);
	}
	$type = '#type';
	$options = '#options';
	$access = '#access';
	foreach ($form as $key => &$formVar){
		if ( is_Array($formVar) && array_key_exists ($type, $formVar ) && $formVar[$type] === 'select'){
			if (array_key_exists ($options, $formVar) && is_array($formVar[$options]) && empty($formVar[$options])){
				$formVar[$access] = FALSE;
			}
		}
	} 
}

Please review and test it. The hook can be called also by View Selective Filter module.

cozzamara’s picture

This code works only with select field, but can be applyed also to the others that have "#access" property.
But the property is a "must have".

cozzamara’s picture

After too many test I finally found a code that seam works good:

function hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id){

// To check the array tree, not necessary in production site
	if (module_exists('devel')){
			dsm($form);
	}
// end checking 

	$type = '#type';
	$options = '#options';
	$access = '#access';
	$filter_prefix = 'filter-field_';
	$theme = '#theme';
	foreach ($form as $key => &$formVar){
		if ( is_Array($formVar) && array_key_exists ($type, $formVar ) && $formVar[$type] === 'select'){		
			if (array_key_exists ($options, $formVar) && is_array($formVar[$options]) && empty($formVar[$options])){
				if (array_key_exists ($theme, $formVar)){
					$form[$key][$access] = 'FALSE';
					unset($form['#info'][$filter_prefix.$key]);
				}

// To check the array tree, not necessary in production site
//				print('<pre>');
//				print_r($formVar);
//				print('</pre>');
// end checking

			}
		}
	} 
}

Please some one test it

cozzamara’s picture

I'm sorry but #39 not work properly.....failed on test

lukasss’s picture

#36 - I think this is custom problem.

cozzamara’s picture

Why #41 @lukasss? Adding this code into the module I think is better so you can avoid to change template.php.

I tested SUCCESSFULLY THIS CODE and now works very good on my site. Please test it

I would to change the status in "needs review" but I'll wait the maintainer will do.

function hook_form_views_exposed_form_alter(&$form, &$form_state, $form_id){

	// Added just "select" html types
	// because they are most used checkbox fields.
	// But can be added all fields you need in "$type_to_remove"
	
	$type_to_remove = 	array('select');
	$type = 			'#type';
	$options = 		'#options';
	$access = 		'#access';
	$theme = 		'#theme';
	$info = 			'#info';
	$value_key = 		'value';
	$val_to_remove = 	array();
	
	foreach ($form as $key => &$formVar){
		if ( is_Array($formVar) && array_key_exists ($type, $formVar )) {
			
			$searchKey = array_search($formVar[$type], $type_to_remove, TRUE);
			if ($searchKey !== FALSE){		
				if (array_key_exists ($options, $formVar) && is_array($formVar[$options]) && empty($formVar[$options])){
					if (array_key_exists ($theme, $formVar)){
						array_push($val_to_remove, $key);
						$formVar[$access] = FALSE;
					}
				}
			}
		}
	}
	unset($formVar);
	
	if (!empty($val_to_remove)){
		
		foreach ($form[$info] as $key => &$infoVal){
			if (is_Array($infoVal) && !empty($infoVal) && array_key_exists($value_key, $infoVal)) {
				
				$keySearch = array_search($infoVal[$value_key], $val_to_remove, TRUE);
				
				if ($keySearch !== FALSE){
					unset($val_to_remove[$keySearch]);
					unset($form[$info][$key]);
				}
			}
		}
		unset($infoVal);
	}
	
	// To show variables with "devel" module installed
	// You can place this code everywhere
	// NOT NECESSARY IN PRODUCTION SITE
	if (module_exists('devel')){
			dsm($form);
	}
	// END UNNECESSARY CODE
}
cozzamara’s picture

...and this code works with ALL selective field empty and you have not specify one by one.

lukasss’s picture

then you'd better add a patch for this

cozzamara’s picture

It's ok...the patch you asked for...

cozzamara’s picture

Please empty cache after apply it

lukasss’s picture

Please correct the error of coding standards
https://www.drupal.org/docs/develop/standards/coding-standards

cozzamara’s picture

FileSize
2.13 KB

I'm sorry for the mistake.. Try this new one and please remember to empty the cache after apply it. Thanks.

lukasss’s picture

I'm afraid that not all errors have been fixed
do you use a dreditor when see this patch in the browser?

cozzamara’s picture

I'm sorry...I review again and then post another one.

cozzamara’s picture

FileSize
2.16 KB

On this file no errors are reported by Drupal Coder.
I hope is the final. Thanks

lukasss’s picture

space in 67 line and more...

lukasss’s picture

dsm($form)

it seems superfluous

cozzamara’s picture

dsm($form) is commented out. Not evaluated in php, and is indifference. I can also remove but nothing happen.
About #52 Drupal Coder not warning me. I checked it again.

lukasss’s picture

use dreditor! This is plugin chrome browser

cozzamara’s picture

FileSize
1.92 KB
cozzamara’s picture

It's ok now also "dreditor" doesn't report errors. I wait for response.

lukasss’s picture

Status: Needs work » Needs review
jvieille’s picture

This did not work for me as filters do not necessarily show up at the first level in the form array (I uses view_ef_fieldset).

Here is my code in views_handler_filter_selective.inc

function views_filters_selective_search_fields($haystack, &$return){
	foreach ($haystack as $k => $v) {
		if(!isset($pk)){
			static $pk = '';
		}
		if(!isset($pv)){
			static $pv = '';
		}
		if (is_array($v)) {
			$pv = $v;
			$pk = $k; 
			views_filters_selective_search_fields($v, $return);
		} else {
			if($v === 'select'){
				$options = $pv['#options'];
				if(empty($options)){
					$return[] = $pk;
				}
			}
		}
	}
}
function views_filters_selective_removeRecursive($haystack,$needle){
    if(is_array($haystack)) {
		if(isset($haystack[$needle])){
			unset($haystack[$needle]);
		}
        foreach ($haystack as $k=>$value) {
            $haystack[$k] = views_filters_selective_removeRecursive($value,$needle);
        }
    }
    return $haystack;
}



/**
 * Implements hook_form_views_exposed_form_alter().
 */
function views_filters_selective_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
 	views_filters_selective_search_fields($form, $return);
	$empty_filters = $return;
	if(is_array($empty_filters)){
		$new = $form;
		foreach ($empty_filters as $v){
			// $v is the filter
			$new = views_filters_selective_removeRecursive($new,$v);
		}
		$form = $new;
	}
}