Skip to main content

Blog Post

How To Do A Combined Name Search

by David Younker
September 9, 2014

Recently I developed some functionality for a client that I realized might be useful to the greater Drupal community. This post describes how to set up a combined fields filter in Views to allow searching for terms and matching more than one field.

Let’s say you have a single search box and you want to be able to match more than one field, in this case, a user’s first, or preferred name (both different fields on the user entity). Using a view with exposed filters to show the results, we can use combined filters to make this possible.

If there is a user named David who has the preferred name set as "Dave", he won’t show up for search results as "Dave" with our original view:

How to do a combined name search in Drupal

So we want to update this view to allow him to be found by searching for David or Dave. First, the view display is set up with the appropriate fields:

How to do a combined name search in Drupal

We modify the view, replacing the exposed filter for First Name with a Combine Fields filter. This lets you select multiple fields for combining. We can set the label the same as the First Name filter so the user will not know the difference. Select the two fields that need to be combined and apply the changes to the display.

How to do a combined name search in Drupal

Once these changes are made, search results now return the relevant user in both searches:

How to do a combined name search in Drupal

Once this is done, your view is now configured to search multiple fields for an optimized user experience. It’s recommended to export these changes to a feature to capture the changes in code.

Additional Resources

A Business Case for Features | Mediacurrent Blog Post

Drupal Features Module 101 | Mediacurrent Blog Post

Conditionally Open a Link in a New Tab in Views Without the PHP Filter | Mediacurrent Blog Post

Headshot

Meet team member, David Younker

David brings over 10 years professional development experience to his role as Drupal Developer at Mediacurrent. Over the past six years, his career has centered on creating new Drupal sites from the ground up, upgrading existing sites, developing custom modules, and contributing back to contrib modules.

David’s interest in programming started from an early age. After earning his degree in Computer Engineering from the University of Central Florida, David built a strong portfolio of development experience at digital agencies and also worked in the information security field for companies like EarthLink and AVG.  Through this experience, he explored a number of different languages and frameworks. Prior to Mediacurrent, David first became involved with Drupal while working on a Drupal 6 websites for Florida Hospital. Over time, he had the chance to upgrade many of these sites to Drupal 7.

When he's not writing code or attending Drupal events in his home state of Florida, David can be found restoring arcade games, tinkering on new projects, and spending time with his wife and daughter.

 

Learn more about David >

Related Insights