Possible to not display exposed filter if it's empty?

Created on 16 October 2010, over 13 years ago
Updated 19 June 2023, about 1 year ago

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.

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States xl_cheese

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡«πŸ‡·France jvieille

    This did not work for me.
    Here is my code:

    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[] = array($pk => $pv);
    				$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){
    			watchdog("v", print_r($v, true));
    			$new = views_filters_selective_removeRecursive($new,$v);
    		}
    		$form = $new;
    	}
    }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[] = array($pk => $pv);
    				$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){
    			watchdog("v", print_r($v, true));
    			$new = views_filters_selective_removeRecursive($new,$v);
    		}
    		$form = $new;
    	}
    }
Production build 0.69.0 2024