Removing [nid:n] in nodereference autocomplete

ack picture ack · Oct 4, 2009 · Viewed 8.8k times · Source

Using the autocomplete field for a cck nodereference always displays the node id as a cryptic bracketed extension:

Page Title [nid:23]

I understand that this ensures that selections are unique in case nodes have the same title, but obviously this is a nasty thing to expose to the user.

Has anyone had any success in removing these brackets, or adding a different unique identifier?

Answer

Grayside picture Grayside · Oct 4, 2009

Ultimately, you need to change the output of nodereference_autocomplete() in nodereference.module.

To do this properly, you want a custom module to cleanly override the function.

This function is defined as a menu callback, thus,

/**
 * Implementation of hook_menu_alter().
 */
function custom_module_menu_alter(&$items) {
  $items['nodereference/autocomplete']['page callback'] = 'custom_module_new_nodereference_autocomplete';
}

Then, copy the nodereference_autocomplete function into your custom module, changing it's name to match your callback. Then change this one line:

$matches[$row['title'] ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';

Dropping the nid reference.

$matches[$row['title']] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';

I believe the identifier is purely cosmetic at this point, which means you could also change the text however you like. If it is not purely cosmetic, well, I haven't tested to see what will happen in the wrong conditions.

I always meant to identify how to do this. Thank you for motivating me with your question.