Is it expected behavior to show the NID after the node title when using autocomplete for a node reference?
I am getting node title [nid:858] showing in the field after it loads but not in the selection list.

Comments

dan3h’s picture

Yes, this is normal.

I don't think autocomplete has any hidden fields that it stores anything in. So it is the contents of that visible field that are getting submitted with your form, and you need to have a unique identifier for the node you are referencing, because there could be more than one node with the same title. When the form gets submitted, the node id is used to indicate your choice (and is what ultimately gets stored in the noderef field under the hood), and the node title is simply used as a sanity-check.

MBroberg’s picture

Thank you for the reply - is there a way to get the NID to show up in the select list when it is generated, in case there is more than one of the same node title? In my case a node title will be a student name and the NID will be the student ID number, so that would be very helpful.

dan3h’s picture

I believe you can get anything you like to show up in the autocomplete field dropdown. I haven't tried this myself, but this looks like it might do it for you:

http://www.appnovation.com/how-implement-drupal-ajax-autocomplete

I am not clear on the difference between the values you see in the dropdown, and the value that ends up in the field if/when you choose an item from the dropdown.

Dan

halloffame’s picture

On the other hand, how do you get rid of it (the nid on autocomplete box)?

dan3h’s picture

Getting rid of it would be more complicated than it seems. See my comment #1 as to why the nid is needed there in the first place. So if you wanted to remove it, you'd have to have a hidden field where the nid is stored, and then just show the title in the visible box.

But even then, what if you have two nodes that happen to have the same title? How is a user to know which one to select? The nid is there in part to guarantee that they're unique.