5

When I have an entity reference field with an autocomplete widget, is there a way to add a field to the info that appears in the autocomplete choices? Seems like the default field is Title, but this in my case might be ambiguous.

6

An easier solution is to modify the 'label callback' property of the node entity.

function mymodule_entity_info_alter(&$entity_info) {
    $entity_info['node']['label callback'] = '_mymodule_label_callback_set';
}

function _mymodule_label_callback_set($entity, $entity_type){
    if($entity_type == 'node' && $entity->type == 'my_node_type'){
        //Compute new label here
       $label = 'test';
       return $label;
    }
}
  • This code appears to change the label everywhere that it appears, not just in the autocomplete. – Tony May 23 '12 at 21:35
  • You could add more logic so that it only fires on certain URLs, like backend edit forms. – Kevin Sep 7 '12 at 12:24
  • Forgot to add, the callback override would be set permanently, but you could grab/copy the default label code from the entity to populate it otherwise. – Kevin Sep 7 '12 at 12:30
  • Great solution - It may not give uber context (as it only has type & entity not where it's being rendered) but it's reliable – electblake Jul 15 '13 at 21:20
3

You must create a selection entityreference plugin. In you can see an exemple in selection directory of plugin directory of entityreference module.

  • I am flagging this answer as accepted, since it hint at the right, clean direction, however if you care to provide more instructions on how to write a minimal sufficient code for such a plugin, I would be extremely grateful. I am not an advanced programmer and it's difficult for me to say what methods must be implemented to get it done. I'm acting on nodes which have a subtitle field, and I want the ajax to display "Title - subtitle (nid)" instead of "Title (nid)". Thank you. – Artur Apr 17 '12 at 9:40
  • More info on entityreference plugins: drupal.org/node/1962284 – milesw Apr 17 '13 at 7:12
  • Also see my anwser on drupal.stackexchange.com/a/82665/526 – zroger Aug 16 '13 at 13:23
2

For the non-coder solution that only affects the field:

What you could do is create an entity reference list from a view and populate your widget with the view.

Create a new view Untick Create a block and Create a page Then click continue and edit. That should get you to the master view.

Click Add to add a new display and choose entity reference. Give your display a easily recognisable name. Add your fields/ filters etc and order them as required. I usually change the items to display under pager to display all items, but I'm not sure how required this is.

Under the Format heading beside Format, click settings and set your field(s) that are to be searched when auto-completing. Under the Format heading beside Show, click settings and set your fields to be inline. Save your view.

Now head over to edit your entity reference field. Assuming you already have your field pretty much set up the only bit you might need to change is under: Entity selection mode Choose: "Views: Filter by an entity reference view" Assuming your field reference target type and view list match up you should get a dropdown appearing under "View used to select the entities" with your newly created view listed in there. Choose it and save.

Now all going well when you add a new entity your field reference will contain a list of entities based on your view.

0

You can create a "Reference" display on a view which you can use. If you created this, go back to your autocomplete field and select the created reference display. But with this, you only alter the text which is displayed in the autocomplete box. The search still goes on the node title.

  • Actually, using a simple module, I created Live Preview display view, hoping that it will be used by Entity Live Preview module. Unfortunately I cannot get it to work. Altering the text would be just fine, I need to append just two more fields to suggestions. Care to give more instructions, please? – Artur Apr 4 '12 at 17:19
  • Do you work with Drupal 6 or 7? – Lance Apr 5 '12 at 6:28
  • It's version D7 – Artur Apr 13 '12 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.