I recently created a view(on a Drupal 6 site) for a client that wanted to be able to search his node results. Part of the requirement was that the search results only show results from this particular view. His 'search' sounded just like an exposed views filter. The search was to be against 5 fields that included: any taxonomy terms the node was tagged with, city, state and zip from it's address field and the title of the node. I created my view and exposed the filters on top with a styled the 'search' form and presented it to the client. It looked good but I knew there were too many fields.

Sure enough the feedback I received was that there were too many fields. They would like there to be a single search field which totally made sense. This presented a problem because we know that a search isn't happening with exposed filters. Limiting it to one field would have defeated the purpose since it would only have access to one of the data elements. I was stumped for a little while until I came across the computed_field project. Computed field is a cck field module that allows you to add "computed fields" to a content type. As you can imagine this opens up a world of possibilities to someone with some php knowledge.

Using computed fields I was able to turn the 5 data elements listed above into a single field. I know this isn't the best solution for someone with a ton of data, but it is a neat little trick that was great in a pinch.

To use computed field, install the module like normal and add a new field to your content type and choose the 'Computed' under type. You will be presented with a text area for your 'computed code' and told that you have access to: &$node, $field and &$node_field. Time to dive in.

Before we can construct the value of our computed field we need to know what info we are playing with. The first thing I wanted to grab were each of the taxonomy terms attached to the node. To do this I used the following code to loop through the terms and give me a $terms variable holding a string of taxonomy terms separated by a space.

$terms = '';
foreach($node->taxonomy['tags'] as $tag) {
  $terms .= ' ' . $tag;
}

This was the only piece of data I needed to process before constructing my field. The help text for the module explains that to set the value of the field you use: $node_field[0]['value']. Starting with that I added my city, state and zip + the $terms I just constructed.

$node_field[0]['value'] = $node->field_vendor_address[0]['city'] . ' ' . $node->field_vendor_address[0]['province_name'] . ' ' . $node->field_vendor_address[0]['postal_code'] . ' ' . $terms;

You will also need to set the type of data that you will be storing. Since I knew I was going to be using a string I set my data type to 'varchar' and choose a decent length.

Now when I save the node, my computed field will run through the php code I inserted and update the value of the string. The result looks something like this: 'New Fairfield Connecticut 06812 term1 term2 term3 '. The display for this field is set to hidden so the end user never even knows that it's there.

Now that we have this field, we can go into our view, add it as autocomplete exposed filter and let our users 'search' away. I guess I should say filter way ;)

comments powered by Disqus