Drupal Views: combined filters

How to search content across multiple content types, without limiting your data model.

Photo of Salvador Molina Moreno
Mon, 2016-01-25 10:32By salva

When building a Drupal site, it's very easy to be tempted to build the data model based on the features and options that one can get out of the box in other contributed modules (and I had my rant about it). And that's fine. Sometimes. For small sites, taking this path can pay off in development speed, since it will reduce the number of steps needed to accomplish something, as well as offer more reusability of components, and simplify overall configuration of the site. For large scale sites, this might become a performance problem as the software (and the content grows).

An example of simplicity in the short term vs performance in the long run is often easily explained with the Views module. A few years ago, it was "too easy" to decide to share plenty of fields between different Content Types, because of all the speed gained in development. In a recent project, we chose not to do this, and keep things as split as possible. However, we still needed to search content across different content types, using a single textbox. Well, it turns out this is very easy to achieve in Views too.

Entering Views combined fields filter

Views Combined fields filter

Since a few years ago, the Views module includes a Global filter that allows a site builder to add filtering options based on the value of multiple fields. Our use case was as follows:

  • Two ContentTypes with a "Full Description" field (textarea), called field_{type}_description.
  • A site section that should allow searching by the contents of both of those content types.

That might seem a case where one would jump into code and implement a hook_views_query_alter() to amend configuration as needed. However, it turns out this can be easily done in config with the mentioned filter. First things first, we add all the fields that we want to include in the combined fields filter. We needed the content title, plus the Full Description of the contents (which appears twice because the label of the fields is the same, even though the fields are completely different.

Combined fields Filter - fields

Finally, all we have to do is add the filter, which is in the "Filters" section of the View configuration, as any other filter (see first picture of this post). After adding it, we'll be presented with the standard settings popup for the filter. From there, we select all the fields that will be considered part of the filter, click "Apply", and then save the view. The other options are the standard ones provided by other Views filters (e.g: operator, label, description, etc).

Combined Fields Filter - settings

Now we're ready to embed the view in a page and use it. This is just a suitable solution for many use cases, but not for all of them. While in our case we kept the goodness of the Views module and a sane data model, the combined fields filter wouldn't be suitable in a case where we had several more fields to filter by, as it would just add more overhead to the underlying database query. However, it proves to be an useful tool to have in your kit!