When a node reference field uses the autocomplete widget, and has possible values defined by a view, the autocomplete will still show the unselectable values.

I would consider this a bug, but it still doesn't let you select the invalid choices, so it works as expected in that case.

CommentFileSizeAuthor
#3 cck_autocomplete-nid.patch1.69 KBRoboPhred

Comments

RoboPhred’s picture

Title: NodeRef autocomplete shows unselectable nodes when using advanced-view » Get node context to noderef by view
Category: feature » support

Turns out the reason it wasn't acting as I expected was due to faulty php-driven arguments in my view.

How would I pull the existing node (if any) for a view-populated-noderef? My goal is to prevent the noderef from selecting itself.
I poked at the arg() function, but the url only contains "nodereference/autocomplete/[field]/[entered value]"

RoboPhred’s picture

Category: support » feature

So it turns out the main issue is specific to autocomplete fields. When using a list box, the arg() function can be used to pull the nid from the url just fine. Same when the autocomplete is validated.

The main issue, and what led me to believe this was a bug in autocomplete, is that the autocomplete uses its own menu callback and thus a different url. So while making an autocomplete that depends on an nid-aware view still "works" in that you cannot submit invalid (based on the nid) values, the autocomplete will still show them.

Since any solution to this would require the autocomplete being nid-aware, the best solution would probably be to append the nid (if any) on to the end of the autocomplete url and let others use views' default php argument. Extra points if views' default argument "Node ID from url" can work with it, but doubtful, as I assume that checks "arg(1) == 'node'".
The issue with this solution, however, is that there would now be two different places to read the Node ID from depending if the autocomplete is searching or validating...

Probably the cleanest solution, which would work for all content types, would be to provide a function similar to what views itself does, a cck_get_context() function which will return the current node (if any) that the view is being ran for.

I will try my hand at making a patch for this.

RoboPhred’s picture

Title: Get node context to noderef by view » NodeRef list from views: Get node context from view
Status: Active » Needs review
StatusFileSize
new1.69 KB

I spent a few hours learning to do what turned out to be a very simple change.

This patch adds the nid to the autocomplete menu path, so the entire thing becomes "nodereference/autocomplete/[field]/[nid]"
If no node is defined, then the nid is set to 0.

This does not include the cck_get_context() function I mentioned above.

I would like some feedback on whether this is the correct way to go about solving this.

RoboPhred’s picture

Status: Needs review » Needs work
chichilatte’s picture

Works perfectly for me, thank youuu! Two and a half years later :)

I'm using D6.22, Node Reference 6.x-2.9

One change though: in your View, the default argument should be PHP code...

if (arg(0)=='node')
    return arg(1);
else
    return arg(3);