Problem is with entity reference, when using a view to specify nodes to select, and an autocomplete. I build the view, tell it what field to search on under Format/Settings, but it *always* searches on entity id.

Hook_views_query_alter is your friend, but dsm doesn't help you. You have to actually hit the url that the autocomplete hits with a search string and put var_dump in your hook implementation to see the actual conditions. Otherwise you don't see where to grab the string the user types in the autocomplete.

Long story short, the solution is this:

function mymodule_views_query_alter(&$view, &$query)
{
        if($view->name == '<whatever your node selection viewname is>')
        {
                //looking to achieve what this line says, but the DatabaseCondition object is protected
//              $query->where[0]['conditions'][0]['field']->conditions[0]['field'] = 'field_data_field_<mysearchfieldname>.field_<mysearchfieldname>_value';

                $a = $query->where[0]['conditions'][0]['field']->conditions();
                $b = new DatabaseCondition();
                // you don't see the contents of the second argument below inside dsm($query)
                $b->condition( 'field_data_field_<mysearchfieldname>.field_<mysearchfieldname>_value', $a[0]['value'], 'LIKE');
                $query->where[0]['conditions'][0]['field'] = $b;
        }
}

Voila.

Comments

Wolf_22’s picture

So long story short, you were trying to use Entity Reference combined with a custom View to search for entity references but key from an alternative field? (I.e. - entity-reference field which is of "user" type but instead of keying from the username, you created a View to key from say, a first name or last name?)

If so, please elaborate on what you did because I'm struggling with the same issue at the moment: http://drupal.org/node/1906344

For the life of me, I just can't get my entity reference field (which is referencing Users) to key from the field_last_name that I created through the use of a custom view. In the view, I've told it to accept a contextual filter of "Last Name" so that when someone types into the field, "Smith", that will be keyed from field_last_name... But it never seems to work and I have no clue why. The view itself is an Entity Reference type of view, so I'd think it should work.

Any insight is GREATLY appreciated.

davery’s picture

Yeah, that's what i did. But it will never work ootb -- it always searches for title.

Try sticking the above in a module with the appropriate view & field names changed. If you run into trouble post what happens.

Wolf_22’s picture

I managed to get this to work, davery but only with the assistance of applying a patch that goes into entity reference:

http://drupal.org/files/entityreference-views-autocomplete-1791914.patch

For more information about that patch, go here: http://drupal.org/node/1791914

After applying this, it now works BEAUTIFULLY (I selected every name field I have in my View to search from / with and now my auto complete can reference ANYONE, not only by their last name, but also their first and their middle!) It's awesome.

Let me know if you have any issues with this and I'll try to help you out. I'm not usually big on applying patches like that, but this one was causing a Threat Level Midnight issue... I'm happy I took the gamble.

r4f’s picture

Can I just say a big THANK YOU!!