开发者

Setting up Drupal node reference autosuggest to search on two separate fields

开发者 https://www.devze.com 2023-01-13 10:14 出处:网络
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?

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!


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%%')");

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号