Recently a client wanted to show a list of uploaded File:description values from uploaded files. Using the file_entity module Files in Drupal become 1st class citizens in Drupal and 1 piece of information they have is a description. Out of the box Drupal 7/8 have basic file information such as type, extension, size, location — but no description.
I wanted to list all uploaded files and provide a way to search across all the provided descriptions submitted by students. Unfortunately, at this time you cannot do this with Drupal 7 or 8 (alpha 12). Not being able to use the file:description field in Views is discussed in this issue queue item for Drupal 6 (yikes!! the history here …). How can we make a simple custom search filter across multiple fields on our site using Views?
Views in Drupal 7+ has this great Filter called Global: Combine Fields Filter.
What this filter allows you to do is to take any number of (text) fields and combine their result into a searchable value. What it’s essentially doing is creating a filter which is creating the following MySQL (or equivalent) WHERE clause:
… WHERE … and (col1 and col2 and col3 LIKE ‘%search_parameter%’)
But, the nice thing is that you don’t have to put many text fields in your Views Exposed Filters.
Here is the configuration for a simple Page view that amasses many of the site’s content fields into a View and uses the Combine Filter to make a simple site search at the path /content-search:
Here is the configuration page for the filter itself, note I basically exposed the filter to visitors and I selected all the content fields as the combined fields that will be searched:
If you’re in the Views Admin UI and have preview SQL query enabled in your Views Advanced Settings tab, you will see the View has the following Where clause appended to it:
WHERE (( (node.status = '1') AND( (CONCAT_WS(' ', node.title, ' ', field_data_body.body_value, ' ', field_data_field_affiliation.field_affiliation_value, ' ', field_data_field_email.field_email_email, ' ', field_data_field_faculty_www_link.field_faculty_www_link_url, ' ', field_data_field_first_name.field_first_name_value, ' ', field_data_field_last_name.field_last_name_value, ' ', field_data_field_location.field_location_value) LIKE '%test%') ))) ORDER BY node_created DESC LIMIT 10 OFFSET 0
- Obviously Drupal’s core Search will do better.
- For any mini search reports you create, you may want to enable Views Result Caching for the view so subsequent searches will be faster. Note large cache tables could in the long run slow down your site.
- Also note that MySQL has a default limit for GROUP_CONCAT_MAX_LEN and you may need to adjust it on your system if you encounter odd behavior in your searches of custom data.
- Searching in general is hard — I recommend installing Apache Solr or a similar 3rd party tool on your website.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!