views:

991

answers:

5

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?

+7  A: 

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.

Grayside
+3  A: 

What Grayside has posted will work... as long as you don't have two nodes with the same title. In other words, if you want to do as Grayside has proposed, you need to be aware that the nid is not entirely unimportant. The nodereference_autocomplete_validate() function does two things. It checks to see if there is a node that matches, and if so, it passes the nid on, setting it to the $form_state array. If it can't find a node, it will set an error. If the nid is present, it will be used to get the node, which also is faster, the code is here:

preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $value, $matches);
    if (!empty($matches)) {
        // Explicit [nid:n].
        list(, $title, $nid) = $matches;
        if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) {
            form_error($element[$field_key], t('%name: title mismatch. Please check your selection.', array('%name' => t($field['widget']['label']))));
        }
    }

This just checks to see if there is a nid and checks if that node matches with the title, if so the nid is passed on.

The 2nd option is a bit slower, but it is here errors can happen. If you follow the execution, you will see, that if will try to find a node based on title alone, and will take the first node that matches. The result of this, is that if you have two nodes with the same title, one of them will always be used. This might not be a problem for you, but the thing is, that you will never find out if this happens. Everything will work just fine and the user will think that he selected the node he wanted to. This might be the case, but he might as well have chosen the wrong node.

So in short, you can get rid of the nid in the autocomplete callback, but it has 2 drawbacks:

  1. performance (little)
  2. uncertainty in selecting the correct node.

So you have to think about it, before going this route. Especially, since you most likely wont be able to find the problem of the selection of the wrong nodes, should it happen. Another thing to be aware of, is that the nid showing up, also brings some valuable info to the users, a quick way to lookup the node, should they be in doubt if it is the one they want, if several nodes have similar titles.

googletorp
Thank you for clarifying the details. If you can assume unique node titles, then it is *only* a performance hit on submit, not a point of nodereference corruption.
Grayside
ultimately you can use a javascript that put the [nid: %id] part in a hidden input field then reconstruct the text field before submitting the form
gpilotino
A: 

Do you know if it's possible to override this function, to provide your own list of auto-complete options? I don't mind the [nid: 123] part. But I don't want to use the title here. I'd prefer to build my own query and offer up a more customized autocomplete list to my users.

In my particular case, my titles say something like this: Doe, John xxx-xx-1234. (The title is created by the auto node title module.) I want the auto complete options to look like this: John Doe, 1983 (where 1983 is the year he was born).

I tried using Views. But it looks to me like the title always gets stuck to the end like this:

John Doe, 1983 - Title: Doe, John xxx-xx-1234

Any suggestions?

bh
A: 

I got Grayside's answer to work, but I had to use MENU alter, instead of the FORM alter he posted. No biggy!

function custommodule_menu_alter(&$items) {

$items['nodereference/autocomplete']['page callback'] = 'fp_tweaks_nodereference_autocomplete'; }

Brad Klaver
Ack! Typo. Thank you, will change in my answer.
Grayside
A: 

@bh:

In order to remove the "- Title: ..." at the end of autocomplete rows, the easiest solution (no coding) is to add the title field in the view and exclude it from the display.

Hope that helps.

Laurent - Agence Web Coheractio

Agence Web Coheractio

related questions