Dear all,

great work on BEF, I like them very much. However there is one thing I do no know how to solve.

Say I have a car sales site, and one vocabulary with brands (BMW, Skoda, …).

Then I add come cars (content type) and every car gets a brand as a taxonomy flag.

Now I use BEF to filter the available cars for the clients, but I need to only display available brands. Say I have two Skoda cars and no BMW, so I only want to show Skoda in checkbox list for the clients. Otherwise it is confusing and the people are getting "no results" way too often…

I have searched thoroughly and only found solution working in 2.x version in D6. I was not able to change it to work under 7.x 3.x. heres the link:

http://drupal.org/node/463678

Would anyone be able to help me with that? Thanks!

Comments

Yuri’s picture

Issue summary: View changes

This is a feature that is often needed indeed.

Yuri’s picture

In the meantime here is a workaround: http://wearepropeople.com/blog/limiting-select-options-for-exposed-filter with some alternatives in the comments.

LuisFernando’s picture

I'm also in need of this functionality. Having filters that show no results is not ideal.

jmaag’s picture

I've had this same request, but for entity reference filters. I solved it similar to Yuri's link (#2), but using Views to handle the database querying. I had an entity reference field on a Research content type, referencing Specialty nodes. The end result was a research listing page with a select list of specialties to filter the research. I'll explain it in my particular case because I think it's easier to grasp, but can be extended to any entity reference relationship between content types.

I created a View that output every Specialty node id that was referenced on the Research node. I used this output to remove no results content in a form alter. Here was my process for the view:

  1. Create a View (I used an Attachment display since I didn't want a page or block cluttering the website)
  2. Add "Content: NID" as the only field
  3. Add published and the content type (the one used in the exposed filter. For me this was "Specialties")
  4. Add relationship "Entity Reference...A bridge to the Content entity that is referencing Content via REFERENCE_FIELD_MACHINE_NAME"
  5. Check the box to "Require this relationship"
  6. Be sure the pager section is set to "Display all items"

The preview should now show all the Specialty Node IDs that are referenced by Research nodes. I use this output to remove items from the exposed filter on the research list page. Here's the code with comments and all caps where appropriate:

/**
 * Implements hook_form_alter()
 *
**/
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {

  // Remove options from exposed filter select list that have "No results found".
  if ($form['#id'] == 'EXPOSED_FORM_MACHINE_NAME') {

    // Getting the node IDs from the View I created. 
    $used_nids_view = views_get_view_result('VIEW_NAME', $display_id = 'VIEW_DISPLAY_ID');
    $used_nids = array();

    foreach ($used_nids_view as $obj) {
       $used_nids[] = $obj->nid;
    }

    // Remove duplicate nids
    $used_nids = array_unique($used_nids);
    $filter = 'EXPOSED_FIELD_NAME'; // Typically 'field-...'

    // Loop filter options, unsetting those options that aren't used.
    foreach($form[$filter]['#options'] as $key => $item) {

        if( !is_numeric( $key )) continue;
        $nid = $key;
        if (!in_array($nid,$used_nids)) {
            unset($form[$filter]['#options'][ $key ]);
        }
    }

  }
}
mikeker’s picture

Status: Active » Closed (works as designed)

What you're asking for is called faceted search and usually requires additional indexing on the server by a Apache Solr or similar. Take a look at the FacetAPI module for this, keeping in mind that you will not be able to use BEF to render your exposed filters if you're using FacetAPI.

jalapi’s picture

There is a great module available for this. Check it out. Works with BEF as well!
https://www.drupal.org/project/views_selective_filters

RealGecko’s picture

#6 works brilliantly.

canardesign’s picture

#6 saved me hours of coding, simple and effective, thanks !

Spanners’s picture

I got stuck on this problem for a little while and then found Selective Better Exposed Filters.

Thanks to tonytheferg for this post on Drupal Stack Exchange.

I was already using Better Exposed Filters to show my two sets of taxonomy terms as radio buttons. The it was simply a matter of ticking the following two options in the BEF settings on my View:
1. Show only used terms
2. Filter terms based on filtered result set (you might not need this - this is so any other filters that are selected will cause the displayed terms to change to show only the terms in the current set of listed nodes)

neeban’s picture

Do we have default out of box same functionality in drupal 10 ?