tags:

views:

55

answers:

1

The simple versoin of my question: I need a CCK Node reference field to search on two different fields in a node. What's the best way to do this?

Some Background

I run a calendar for a physical therapy program. Each lecture has a reading list. Readings are their own content type and the lecture has a autosuggest node reference field that currently only searches the reading's title. I need it also to search on an additional cck field, the readings author.

I attempted to do this a custom view, but I need a very simple title OR author search, and we're stuck with AND for a little bit longer. Is there a workaround? Do I need to create a small module to do this? If so, wheres a good place to start to learn how to do that? (I've made custom modules before but none that involve interfacing with views/cck). I'm not really sure where to go with this right now.

Thanks for the help!

+1  A: 

There isn't really a neat way to accomplish this, because searching only in the title is a "feature" of the Node Reference module (which is part of CCK). However, you can create a custom View to provide the results for the autocomplete, and use hook_views_query_alter() to change the query that the View execute. The view you create should be selected in the configuration page of the field.

Below is a sample implementation that changes the query to search both the title and the body of nodes. You'll probably need to customize it a little bit to get exactly what you want.

function mymodule_views_query_alter(&$view, &$query) {

    if ($view->name == 'my_custom_view' && $view->current_display == 'content_references_1') {

        // Remove the original title constraint
        unset($query->where[0]['clauses'][2]);

        // Duplicate the argument (keyword to search for), so
        // it is passed to both the title and the other field
        $query->where[0]['args'][] = $query->where[0]['args'][1];

        // Add the custom where clause
        $view->query->add_where(0, "(node.title LIKE '%%%s%%' OR node_revisions.body LIKE '%%%s%%')");

    }

}
Vinicius Pinto
Yes! Thank you. This was infinitely helpful. It took me a long time to figure everything out but I've got it now. Thank you!
mattmcmanus