Search value of fields when using nodereference autocomplete with Views
kenorb - February 17, 2009 - 14:45
| Project: | Content Construction Kit (CCK) |
| Version: | 6.x-2.x-dev |
| Component: | Views Integration |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
I've got nodereference autocomplete which using Views.
How can I return nodes by searching for value on content fields.
Autocomplete always searching inside title:
... AND ( (node.type in ('parent')) AND (node.title LIKE '%firs%')I've tried different Relationship, Arguments and nothing.
I've read this article:
http://drupal.org/node/289738
But there is nothing about that.
Is there possibility to make it?

#1
Not currently. The autocompletion search is hardcoded on node title.
#2
Thank you for the answer.
So I'd like to report it as feature.
This feature is similar to this one: #303209: How to hide the referenced node title in select list
#3
Some info for developers:
Sample URL: nodereference/autocomplete/field_myfield/some_text_to_find
Here is function _nodereference_potential_references_views() which is executed to find references:
Result code:
<?php$result = $view->execute_display($display, $view_args);
?>
After query is builded in cck\includes\views\handlers\content_plugin_display_simple.inc via handler
<?php
class content_plugin_display_references extends content_plugin_display_simple {
function query() {
...
if ($options['string'] !== '') {
$match_clauses = array(
'contains' => "LIKE '%%%s%%'",
'equals' => "= '%s'",
'starts_with' => "LIKE '%s%%'",
);
$clause = isset($match_clauses[$options['match']]) ? $match_clauses[$options['match']] : $match_clauses['contains'];
$alias = $this->view->query->ensure_table($options['table']);
$this->view->query->add_where(NULL, "$alias.$options[field_string] $clause", $options['string']);
/* Example $options variable:
Array
(
[table] => node
[field_string] => title
[string] => some_text_to_find
[match] => contains
[field_id] => nid
[ids] => Array
(
)
)
*/
}
elseif ($options['ids']) {
...
}
}
}
?>
#4
First version of patch which allow you to search for value of other CCK fields which has been added as Fields into your View.
It's fully working.
And if you want to hide the Title, you should make changes described here:
http://drupal.org/node/303209#comment-1263205
But there are some couple small things to do, which is:
- Replace empty Title with some values from Fields
Example patched code in nodereference_autocomplete() should looks like:
<?phpforeach ($references as $id => $row) {
// Add a class wrapper for a few required CSS overrides.
if (!empty($row['title'])) {
$field_value = $row['title'];
} else {
$field_value = ereg_replace("\n", ' ', $row['rendered']); // delete new lines
$field_value = ereg_replace(' +', ' ', $field_value); // delete duplicated spaces
$field_value = trim(strip_tags($field_value)); // strip html tags and white spaces
}
$matches[$field_value ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
}
?>
But if you apply this patch, some validation function have to be patched as well.
#5
#6
See also on lines 783-791 of cck\modules\nodereference\nodereference.module:
<?phpfunction _nodereference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
...
783: $options = array(
784: 'table' => 'node',
785: 'field_string' => 'title',
786: 'string' => $string,
787: 'match' => $match,
788: 'field_id' => 'nid',
789: 'ids' => $ids,
790: );
791: $view->display_handler->set_option('content_options', $options);
...
}
?>
Here's where the $options variable is set. The commented-out section in #3's code sample is an example of an $options variable. Well, it appears that it's hard-coded to search the title field (see line 785 specifically).
Note that if you are NOT using Views, you can use hook_db_rewrite_sql() to rewrite the WHERE clause of the autocomplete function's search of the DB.
#7
subscribe. i'm using Views in Nodereference Autocomplete fields and looking for ways to increase the flexibility of searches. Will check this out. Thanks.
#8
I have been working on this problem as well. I think my approach is a little different than kenorb's, but AFAIK this patch is working.
To reiterate the current problem and this solution, here is a simple example.
Content type 1: Product
Product has a Title that is a name for the product. Product also has a CCK field named 'SKU'.
Content type 2: Invoice
Invoice has a nodereference field named 'product'.
On the Invoice entry form I need to be able to enter the SKU of the product. I can create a view that returns a list of Product SKU's and reference the view in the product field (on the Invoice content type). The title can be hidden by using the workaround explained in #303209: How to hide the referenced node title in select list. Now the product autocomplete will display SKUs.
The problem: If the user attempts to type the product SKU, no results will display. The autocomplete is searching for the products title.
The patch: This patch only affects the use of nodereference with the "Advanced - Nodes that can be referenced (View)" setting. With this patch the autocomplete searches the first active field that is setup in the view. Also, the autocomplete validation is changed to search the view if the title doesn't match.
#9
from #8: "With this patch the autocomplete searches the first active field that is setup in the view"
Problem is that the order of views fields already determines how autocomplete results are displayed, so hardcoding a feature on the first one loses flexibility
I'm thinking maybe the view's exposed filters could be used for that. Autocompletion search happens on the 1st exposed filter (or on title if no exposed filter), other exposed filters are discarded.
I have no idea whether it's actually doable, but at least conceptually it seems to make sense.
#10
I've applied patch at #3 and now anytime I try to save a new node with a value inside a cck nodereference with a view I get a message: " cck_filed_name: this post can't be referenced."
it looks like it has some kind of validation problem. What can be happening?
#11
arojoal: #3 patch is not finished so it will not work
#12
I have tried everything for a solution for this and have came up short. This seems like something that between Drupal, CCK and Views2, should not be a problem. Here is my delima:
Have node type: "Resorts" which has name, location, images (over 7,000 nodes)
Have node type: "Property Listing" which has a CCK node reference to the resort name (and a bunch of other fields)
Have node type: "Exchange Item" which has a CCK node reference to the user's Property Listing
...so there is a node, referencing a node, that is referencing a node.
I have built a View that pulls node type "Exchange Item" and have an exposed filter for "Resort" (using a relationship to Property Listing) and no matter what I've tried, I cannot get the Resort field to be an autocomplete textfield rather than a select list. As you can imagine, with it being a select list and having over 7k items, it is a page/server load issue.
Any help to accomplish an autocomplete field would be MOST appreciated!!!