5

I was at a local Drupal group meetup last week where a member ran through a presentation of the Views module. I was really excited when I saw that he was able to add dropdowns to the Filter Criteria that he was using as I need exactly that functionality for something that I am building.

The thing is: I can't find the settings at all in my build. I remember that there was a 'settings' link at the bottom of his Filter Criteria area, but I have nothing like that.

Could anyone explain where I can find this? Am I missing and additional module?

  • Are you talking about exposed filters to the user, or selecting a value for the filter in the views interface? – googletorp Jul 8 '11 at 8:28
  • I am talking about exposed filters to the user. Allowing them to select a value for the filter from a dropdown and then filtering the view from there. – Martin Duys Jul 8 '11 at 14:08
10

The filters in views and how they are exposed to the user reacts a bit differently depending on the type of filter that is used. The type of filter that used, is controlled by defining the data to views using hook_views_data and hook_views_data_alter. This is a bit code heave and not always something you need to do your self as many modules including views does a lot of this for you.

Two common filters used is

  • views_handler_filter_in_operator
  • views_handler_filter_string

The two are are fundamentally different because their assumptions and requirements are very different.

The first one, is the one that you would like to use. It's used when you store a value that has a defined list of possible values. An example could be content types which is the example that @Danielle used in her post. Since nodes can't have any random value for their content type, it's possible for views to display a list of checkboxes in the Admin interface when using this filter and a select list / multiple select list for the user when it is exposed.

The second one is used when a string is stored in the database. Using nodes as an example, the title field would be a good candidate for such a filter. Since titles can be any text, it doesn't make sense to expose title selection of a node as checkboxes or select lists, instead you get some other options like you are seeing in your view. You can filter by if the string contains a word (beginning /end / somewhere), it's length etc.

If the one that did the views integration didn't bother using the in_operator type of filter, and describe to views (by using a php function) which options are available, you will have to do it using hook_views_data_alter.

What you need to do is:

  1. Create a subclass of views_handler_filter_in_operator
  2. Implement get_value_options
  3. Tell views about your new handler.

In code it would look like this

/**
 * Define my custom in_opeator
 */
class my_custom_in_operator extends views_handler_filter_in_operator {
  // overwrite the get_value_options function.
  function get_value_options() {
    if (isset($this->value_options)) {
      return;
    }
    $this->value_options array('option_1' => t('Option 1'), 'option_2' => t('Option 2'));
  }
}

/**
 * Implements hook_views_data_alter().
 */
function my_custom_views_data_alter(&$data) {
  $data['entity_type']['property']['filter']['handler'] = 'my_custom_in_operator';
}

And you're done. In reality it's actually quite simple. But finding out how to do is not as views is quite complex.

  • Googletorp ( & @Martin Duys) great answer, thanks ! I tried and placed code in MODULENAME.views.inc but got no result. With what am I suppose to replace entity_type & property in $data['entity_type']['property'] ? node and field name ? – Kojo Jul 9 '14 at 16:39
  • @googletorp i did same according above with one change $this->value_options =array('option_1' => t('Option 1'), 'option_2' => t('Option 2')); but i am unable to find this in exposed view filter. – Fawwad Aug 27 '15 at 18:04
5

Are you allowing multiple values in your exposed filter? If so, the selections will appear in a select box with a scroll bar by default. If you force single selection only, your exposed filter will appear in a drop-down by default. I hope this helps!

  • I think you have it, but where do I allow multiple values? – Martin Duys Jul 8 '11 at 7:37
  • Have a look at this screenshot from an example exposed content type filter I have implemented for my client to manage content on the website. Unchecking multiple yields your dropdown. – Danielle Jul 8 '11 at 7:57
  • Sorry to be a pain, but where did you go to get that screenshot? Views | Select the View | Displays | Filter Criteria | and then? – Martin Duys Jul 8 '11 at 8:13
  • No worries! Here is a screen of the whole Filter Criteria dialog box. Note the "expose filter to visitors" in the upper left corner. – Danielle Jul 8 '11 at 9:21
  • Weird. My Screenshot looks totally different to yours? Where do I go to allow those multiple values? I am on version 7.x-3.0-rc1 by the way. – Martin Duys Jul 8 '11 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.