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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Status: Active » Fixed

Not currently. The autocompletion search is hardcoded on node title.

kenorb’s picture

Category: support » feature
Status: Fixed » Active

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 with view

kenorb’s picture

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:

    $result = $view->execute_display($display, $view_args);

After query is builded in cck\includes\views\handlers\content_plugin_display_simple.inc via handler

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']) {
...
    }
  }
}
kenorb’s picture

Status: Needs work » Active
FileSize
4.3 KB

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:

  foreach ($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.

kenorb’s picture

Status: Active » Needs work
jessepinho’s picture

Status: Active » Needs work

See also on lines 783-791 of cck\modules\nodereference\nodereference.module:

function _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.

ccshannon’s picture

subscribe. i'm using Views in Nodereference Autocomplete fields and looking for ways to increase the flexibility of searches. Will check this out. Thanks.

kswan’s picture

Status: Needs work » Needs review
FileSize
2.67 KB

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 with view. 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.

yched’s picture

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.

arojoal’s picture

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?

kenorb’s picture

Title: Nodereference autocomplete doesn't match all of view's fields » Search value of fields when using nodereference autocomplete with Views
Component: nodereference.module » Views Integration
Category: bug » feature
Status: Needs work » Needs review
Issue tags: -Usability

arojoal: #3 patch is not finished, it needs more work.

marty.true’s picture

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!!!

patcon’s picture

@ #9

Sorry, yched, did you mean that the first exposed filter should be searched instead of the username when present, or were you saying this might be a future feature?

I've got an exposed filter that's working in preview (set as the first filter item), but only the username will turn up any autocomplete results. Any thoughts?

Thanks yo!

EDIT: Reread, and sorry, I guess I misunderstood the verb tenses, and thought it was a current feature :)

kswan’s picture

FYI, I expanded on the patch in #8 and refactored it into a separate module.

It is available on github at http://github.com/kswan/super_nodereference.

This module solves the use case described in #8.

patcon’s picture

kswan, I love you. Just sayin.

fin.

ccshannon’s picture

Me too. But more of a "I like you; I really really like you." Great work!

Aren Cambre’s picture

Title: Nodereference autocomplete doesn't match all of view's fields » Nodereference autocomplete doesn't match all of view's field
Issue tags: -Usability

This is bug because the UI misinforms the user. The UI suggests matching of any of the view's data since it all shows up in the autocomplete drop down. In fact, only the node title is matched.

While the above informal module may be a workaround, this problem is still a bug and should be fixed in core.

Moving to D7 because new features go in the latest version and get backported. (D8 is technically the latest, but it's embryonic for assignment since D7 is not even in beta.)

Changing to needs work because the code will need modification to work in D7.

Aren Cambre’s picture

Title: Search value of fields when using nodereference autocomplete with Views » Nodereference autocomplete doesn't match all of view's fields
Project: Content Construction Kit (CCK) » Drupal core
Version: 6.x-2.x-dev » 7.x-dev
Component: Views Integration » field system
Category: feature » bug
Status: Needs review » Needs work
Issue tags: +Usability
yched’s picture

Title: Nodereference autocomplete doesn't match all of view's field » Nodereference autocomplete doesn't match all of view's fields
Project: Drupal core » Content Construction Kit (CCK)
Version: 7.x-dev » 7.x-2.x-dev
Component: field system » nodereference.module
Issue tags: +Usability

Noderef is not part of core D7.

patcon’s picture

Branjawn’s picture

I need the resolved for the following circumstance:
church directory. node created called Family.
Use userreference to assign family members.
Only searches userid so if John Doe's user name is freakydeeky, then searching for John or Doe does not bring him up.

I know I should have made a content type of Profile or User or something but we're too far beyond that now :P

KarenS’s picture

Version: 7.x-2.x-dev » 6.x-2.x-dev

This is an impossible combination of D6 and D7 issues. The patch is for D6, as are most of the comments. The D7 version of nodereference has no views support at all yet so this cannot be a bug in the way views support is implemented. #21, posted after the issue was changed to D7, looks like it is referring to the D6 version.

I'm changing it back to a D6 issue, which is the only context that it really makes any sense at the moment.

pvanerk’s picture

subscribe

Cyberwolf’s picture

Title: Search value of fields when using nodereference autocomplete with Views » Nodereference autocomplete doesn't match all of view's fields
Component: Views Integration » nodereference.module
Category: feature » bug
Status: Needs review » Needs work
Issue tags: +Usability

Subscribing.

HoKe’s picture

subscribing

gorillaz.f’s picture

It's really cool and useful to #14 super_nodereference .
By the way, what about a super user reference with using something like content profile ??

kevinwalsh’s picture

Boobaa has also just released http://drupal.org/project/text_noderef which should work for many use cases

Aren Cambre’s picture

Project: Content Construction Kit (CCK) » References
Version: 6.x-2.x-dev » 7.x-2.x-dev
Component: nodereference.module » Code: node_reference

Moving this issue to nodereference's new home.

dabeast’s picture

subscribing

interestingaftermath’s picture

kswan (#14) - Thank you! It works great for 6.x

Just a quick note to anyone else using it... be sure to have nid as one of the fields on your view :)

tfrech’s picture

Issue tags: -Usability

I added #14 super_nodereference module. First thing I noticed is that some font and alignment changes happened on my page. My header shifted to the left rather than centered, all of the module config page fonts got slightly bigger. Before I use this, I wonder if someone could explain why this happens and what I can do to prevent it.

tfrech’s picture

Nevermind #31. I had copied the module files one file at a time and apparently copied them wrong. All seems fine now.

Aren Cambre’s picture

Issue tags: +Usability

(Adding tag back.)

gthing’s picture

Been playing around with super_nodereference for a few days and have run into a few problems. Looking a bit closer while trying to use it with computed field, I realize it is not properly storing the field like the regular node reference widget. Here is the field array stored with the built-in CCK node reference widget:

Array ( 
	[0] => Array ( 
		[nid] => 89260 
		[_error_element] => field_supernoderef_field][0][nid][nid 
	) 
)

And here is what super_nodereference is storing:

Array ( 
	[0] => Array ( 
		[value] => Some Test Node [nid:75] 
		[_error_element] => field_noderef_field][0][value][value 
	) 
)

Essentially super_nodereference is just storing the text of the search result in [value] rather than the node ID in [nid]. I'm going to take a look at it and see if I can straighten it out, but would appreciate some help.

kswan’s picture

This issue is not the correct place to post issues related to super_nodereference. The github issue queue is appropriate. My d.o contact form is easily available as well. BTW, #34 is resolved in github.

yched’s picture

Category: bug » feature

Recategorizing

tko’s picture

Is this at all possible in 7.x ?

davery’s picture

I did it with views 7.x-3.3, references 7.x-2.0.
Modify references_plugin_display.inc in /references/views starting line 80 to look like this:


      $table_alias = $this->view->query->ensure_table($this->view->base_table);

//BEGIN EDITS
      //$this->view->query->add_where(NULL, $table_alias . '.' . $options['title_field'], $value, $operator);
      $this->view->query->add_where('ac_conds', $table_alias . '.' . $options['title_field'], $value, $operator);

        foreach($this->view->display['references_1']->handler->handlers['field'] as $key=>$field)
        {
                $custom_table_info = $field->field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'];
                if( $key == $options['title_field'] )
                {
                        continue;
                }
                if(is_array($custom_table_info))
                {
                        $custom_table_alias = key($custom_table_info);
                        $custom_title = $custom_table_info[$custom_table_alias]['value'];
                }
                $this->view->query->add_where('ac_conds', $custom_table_alias . '.' . $custom_title, $value, $operator);
        }
        $this->view->query->where['ac_conds']['type'] = 'OR';
//END EDITS

    }
    elseif ($options['ids']) {
...

It worked for the field i used, and looks to me like it should work with arbitrary fields. YMMV.

Lendude’s picture

My solution, I wrote a small module (for D7) to override the autocomplete function that userref uses. I reset the search string to '' and limit to 200 (to retrieve the full set of results from the view, higher limit needed if you work with bigger sets), then just checked the results against the original search string and toss everything that doesn't match. Overhead is obviously a lot higher this way, but atleast it does what I expect it to do : search in your result set (and not in an arbitrary field)

here is the full code I used in my .module file:

/**
 * Implements hook_menu_alter().
 */
function userref_searchall_menu_alter(&$items) {
  $items['user_reference/autocomplete/%/%/%']['page callback'] = 'userref_searchall_user_reference_autocomplete';
}

/**
 * Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing users
 */
function userref_searchall_user_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);

  $options = array(
    'string' => $string,
    'match' => $instance['widget']['settings']['autocomplete_match'],
    'limit' => 10,
  );
  if (module_exists('views') && !empty($field['settings']['view']['view_name'])) {
    $options['string'] = '';
    $options['limit'] = 200;
  }
  $references = user_reference_potential_references($field, $options);

  $matches = array();
  foreach ($references as $id => $row) {
    // Markup is fine in autocompletion results (might happen when rendered
    // through Views) but we want to remove hyperlinks.
    $suggestion = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '$2', $row['rendered']);
    if (stripos($suggestion, $string) === FALSE) continue;
    // Remove link tags Add a class wrapper for a few required CSS overrides.
    $matches[$row['title'] . " [uid:$id]"] = '<div class="reference-autocomplete">' . $suggestion . '</div>';
  }
  drupal_json_output($matches);
}

this can probably be improved a lot but it might be useful for some people

Len

sachbearbeiter’s picture

@38 which parts i have to modify, if the field i want to search through is named field_txt_surname?

thanks sb

sachbearbeiter’s picture

if you really need the functionality of #38 use the hacked and attached references_plugin_display.inc.txt file (for usage remove the .txt part -> references_plugin_display.inc)

maybe some maintainer wants to implement this ...

if you use the modified autocomplete, other fields then the title are used, but if you have selected one item, still the title is shown in the autocomplete after insertion - maybe someone knows a workaround for this ...

but this is better than nothing ...

+ thanks to #38 for initial idea

jason.fisher’s picture

Using #41 successfully, thanks!

evil_cz’s picture

Issue summary: View changes

Using #41 - does not work under recent version of Drupal and the module.
Is there any progress about this issue?
Any plans for implementation to the module?

ikeigenwijs’s picture

Came across same issue.
Using tokens to rewrite output of the title field to include the wanted fields to match by auto complete does not help either.
#14 looks promising, but the overhead would kill our project.

Found this project that solves the problem at there end
https://www.drupal.org/project/acdx_references