With the autocomplete widget, there can be problems when several nodes have the same title (nothing in drupal prevents or event forbids that...)
currently, the nid is is retrieved from the user's entry with a simple
$nid = db_result(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.title = '%s'"), $node_field[$delta]['node_name']));
Meaning the actual node that gets referenced is just the first one with the right title that's retrieved by the query (AFAIK, the result is impredictable when several record match and no order is specified ?)
There should probably be at least a check on the "referenceable" types that were set for the field. There seems to be currently no guaranty that the node that eventually gets selected has a correct type.
Yet even that wouldn't provide a 100% reliable way to reference a node. I WILL definitely have several nodes having the same title and the same type...
Cant't think of a reliable way right now. Just wanted to highlight the problem (since there is some sort of a buzz going on about nodereference providing the long-awaited "node relationships" functionnality)
For my current and particular needs, I ended up adding a widget to reference a node by it's nid. I know nids should stay "in the box and out of the user's sight", but the form will only be accessible for admins, so...
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | nodereference.module_12.patch | 6.58 KB | yched |
| #24 | nodereference.module_11.patch | 4.31 KB | yched |
Comments
Comment #1
karens commentedAnd there's a related problem if you use a select list and have nodes with the same name -- you just get a list that repeats the title with no idea which is which. I'm one of the ones trying to make cck into my relationship application, and I also have a lot of repetition in node names, so I've run into this, too. I'm still noodling around on possible work-arounds...
Comment #2
yched commentedI didn't get to use the select widget - too many potential candidate nodes on my side - so I didn't figure there was also a problem there.
At least it's more of a usability issue : the select widget uses unambiguous nids for its internal values (I guess ?), so you know for sure that the nodes you selected get referenced. Problem is, you're not sure what to select in the first place :-)
These might be tricky issues. How do we "talk about" a node, if we don't want to involve explicit nid values ?
It seems there has been several attempts at relationships modules / API.
I guess some people around here already struggled with similar UI concerns ?
Comment #3
karens commentedI have played a bit with the relativity module which is kind of what I am trying to emulate. It has the same problem (only displays the title of potentially selectable node) but instead of presenting them in a drop-down select list, you click a link that says 'add to existing event' (or whatever the node type is) and it takes you to a list of events that you can select from. It still only displays the title, but the listing could be expanded to show other info from the node that would make it easier to pick the right one.
This is of course much more complicated that picking from a drop-down list or autocomplete box, but is probably the only way to ensure that you are selecting the right link. I could visualize creating the list using views, where you can filter it as necessary to find the link you want, then selecting it, but it would still be a 2-step process.
Other than that, I don't see a solution for anyone with a longish list of nodes that may or may not include duplication.
Comment #4
karens commentedSo, thinking further, maybe we need a third widget. Keep the two current ones, which will work fine for simple needs and short lists of available nodes, and create a third widget type that does the 2-step of taking you to a page to filter out the node you want, then attaches it.
I'm not sure I have time to work on this idea right now, but if you think it might work and want to give it a shot I can help you debug it.
Comment #5
robert.redl@easytouch.cc commentedCould this be a fast workaround?
Simply concetenate the nid with the title (in displaying the results, and also in the database queries)
--- Autocomplete field shows this: ---------------------------------------
[ John O]
John Doe [15] <= node id
John Doe [34]
The User would have no problem, because the Autocomplete function is somekindof full text search.
We could helpthe Use, if some other fields of the John Doe node would be included in the Autocomplete result set. And from the SQL point of view: Have a look at the phpmyadmin queries if you update one table row, you get a list of all columns in the WHERE statement (for somekind of 'this should really be the right row independent of all Index fields that exist') - and for us the node id should be unique and everything solved.
Thats some kind of thing I would like to have with CCK nodes. If I have for example a CCK content type of 'Peron' and I search for 'John' I would get my two nodes 15 and 34 with John Doe, but also would see f.e. the City and/or email of alls 'John')
--- Autocomplete field shows this: ---------------------------------------
[ John O]
John Doe - New York - john@doe.com [15]
John Doe - Chicago - johnny@gmail.com [34]
On the Widgets Settings Page, I would wish to have thepossibility to select which of the existing CCK content fields are displayed in the autocomplete result.
Autocomplete Field > edit > Widget Settings > Data settings > [v] Person *** [Multiple Select List of additional content fields to display] ***
Comment #6
robert.redl@easytouch.cc commentedAs a workaround I will now force the node title to be unique.
# vi modules/node.module
Next step will be to lookup the node-types that are allowed to
be connect in the nodereference.module as yched mentioned...
Comment #7
yched commentedThat's a noble effort, but forcing node titles to be distinct (even on a "per type" basis) is a serious limitation.
Cant' work on my sites, anyway :-)
Comment #8
robert.redl@easytouch.cc commented@yched
Could you pleasegivea high level example (on your application level) how you have your content-types organized, that you need the same title within the same content-type? Would be interesting to know what the problems are in 'real project life' above the module layers.
I also have seen around the forums a lot of attempts where people are trying to get independent of the node-title, leaving it automatically filled and only use cck fields for the data. I am also thinking about displaying info about the active selected referrer node - at the moment between user has choosen it from the list and before clicking submit button - to help the user with his selection, if there are multiples. (but we first have to fix the nodereference.module to respect the allowed types)
Comment #9
yched commentedWell, I have mase this site where they have a directory of all the bookstores in France (well, they hope to have them all at one point...)
So I have a node type "bookstore". The title of a "bookstore" nodes is the name of the shop. This is what comes natural, and integrates well with node title default display (link to the node page in teaser view, page title in node page view...)
Obviously, there might be (and there are indeed) several unrelated shops with the same name.
In fact, the only "unique" thing about a bookstore (apart from the node nid) is the phone number (and that's not even 100% bullet proof), but I obvioulsy don't want to use that as a node title :-)
Comment #10
yched commentedIn fact I think the perfect solution for this would involve two points :
drawback : the UI for this might not be a pleasant thing to do...
There should be a hidden textarea, that gets updated with the nid when the user selects a node in the autocomplete list. It's the value of that hidden textfield that is used to unambiguously get the desired referenced node)
drawback : the current autocomplete widget probably doesn't support the notion of having different source (visible) and target (hidden) textareas...
Comment #11
yched commentedOops. I meant "textfield"s instead of "textarea"s in my previous post.
Comment #12
karens commentedI also must have the ability to allow duplicate node names. For example, I have a cck node for events and many events have the same name, esp repeating events. Trying to add enough extra text to make all of those names unique would make them too long to display well in a calendar.
I agree with yched on the requirements, I had tried to do something like this in one of the iterations of the nodereference field, but it didn't work (because the autocomplete only stored the name, so the database update picked up the wrong one). Yes, it will be difficult to figure out how to do it...
Comment #13
marcoBauli commentedthe solution that easytouch is proposing at #5 seems univoque and valid, always from a final user point of view ;)
also thanks easytouch for the workaround at #6, but pasting those lines into my node.module and uncommenting the array gives me following error:
Parse error: parse error, unexpected $ in /home/kiteatla/public_html/drupal/modules/node.module on line 2525Comment #14
yched commented@kiteatlas :
It would help if you pointed which of the lines in easytouch's code is the n° 5255 in your modified node.module file :-)
Besides (and IMO), easytouch's comments are only valid as temporary workarounds if you do need them. Two reasons for that :
- nids should be kept out of the user's sight unless it's really necessary.
It's an internal and technical notion, and some users will be confused more than anything else.
AFAIK, node urls are the only "visible" part of a drupal site where nids appear (some contrib modules might prove me wrong, but you get the point...). Enable URL aliases, and nids become invisible...
- we certainly do not want to force node titles to be different - That's a big constraint to add only for userreference.module !
So we have to find a cleaner fix.
More thoughts about that in my next post - I've had some ideas ! :-)
Comment #15
yched commentedSome more thoughts on the two points I outlined in my comment #10 above :
I had an idea while (re-)reading this nodereference feature request, in which JonBob mentions that a view (Views Module) could be used to precisely define referenceable nodes for a nodereference field (note : grrrreat idea, but not coded yet).
I think the natural solution for our current issue would be to have the admin use this view's "fields" section to define the additional node fields to display in the select box (select widget) or the completion list (autocomplete widget).
This way we have for free : the UI for the fonctionality (view definition form...), plus hooks to node fields of all CCK types, plus hooks to node fields of every other (views-aware) type.
We might ship an additional Views "plug-in", to provide for the default display (all the fields on one line, separated by a dash, for instance), and have it themable :-)
The patch for this shouldn't be too hard to code, but unfortunately I have no time for this before 2 or 3 weeks... If anyone feels like it :-), else I probably will.
Drupal's current autocomplete textfield widget can't do that, BUT...
if the envisioned jQuery integration gets effective, all of drupal js libs will probably get rewritten - this might be a good opportunity to have an "enhanced autocomplete" considered.
I'll try to find the best place to file a request about that.
Comment #16
marcoBauli commentedabout the error at #13:
sorry, my bad,easytouch hack do work (thx yched, your comment made me give another try!)! I pasted the whole code in node.module, but actually
function node_validate($node, $form = array()) {already exists there, so just skip the first 4 lines, and be sure that if you uncomment the array, that you remove the upper empty one.about additional node fields to display:
@yched: if i understood right, what you propose is easytouch solution (#5, second part of the comment: have checkboxes to select which fields to display in select box) on steroids.
So far so good, as far as is not overkill for the scope, but i am no coder so i'd better shut up ;-]
Comment #17
tema commentedyched (#15): I have my-own widget that displays one another node field in addition to node title. It helps to distinguish one node from another (with same title: it's a common place in my example), but I made it so brutal so I hesitate to represent it ;-)
I agree that Views module is the right thing to use instead of hard-coded database queries.
(#2): Another my project is a taxonomywidget that limits potential references by taxonomy terms with per-field settings. It helps to reduce a number of candidates and allows to implement more logic in use of nodereference field.
Now I'm updating it to use with my already published Add and Reference and Form restore modules: when You about to add a node from taxonomy-limited nodereference field (or edit already referenced) it must have altered taxonomy form with only allowed terms avaliable to select.
There's the trouble with 'tags' vocabularies: it needs for another settings to allow or not the creation of new tags from referenced nodes, if so - added tags must go in widget settings as allowed or we have an inconsistence.
Another trouble is a category.module. I don't like it but peoples do. Should I make it compatible?
Comment #18
yched commentedOK, I took a stab at the first point in my comment #15 : have additional node fields displayed, using views.module
It provides a way to solve the "ambiguous node selection" issue when using select widget, but only partially solves it when using the autocomplete widget (a different node with the same title can still get selected)
For this reason, and also because this thread begins to be cluttered, I'm submitting the patch in a new thread
Comment #19
yched commentedAs a first step on the autocomplete widget issue (see comment #10, second point), I submitted a patch for the core autocomplete widget to provide the underlying feature.
It is for drupal HEAD, so we need to :
- get the feature accepted and committed in HEAD, possibly before the code freeze
- get an agreement for a backport in 4.7, by pointing this to be a "serious usability issue" inc cck/nodereference (which I do think it is...)
In other words : please provide backup, support, review... over there if you feel this is needed :-)
Comment #20
RobRoy commentedI agree that we need a way to differentiate nodes with same titles for nodereference. I have a field that refers to blog posts and many of them have the same title. My field is a multiple autocomplete and I can select a blog titled "TEST" and one titled "test" hit save, and when I come back, only one of them is referenced. I do not feel this should go in another module, but rather be a patch to nodereference.module or the autocomplete widget. I did look at the above issue and saw it was "wont fix"ed. What is the current plan of action?
Comment #21
karens commentedFixed by recent commit to use views as nodereference selector.
Comment #22
yched commentedNot quite, actually.
The committed "views as noderef selector" patch allows to display additional info about potential ref nodes, so that the user can pick knowingly between multiple nodes with the same title.
But there remains an issue with the autocomplete widget : the UI now allows you to pick the precise node you want, but you still don't know for sure which one does get referred to in the db...
I tried to work on the idea of an extended autocomplete (core) feature, but this was not deemed a good idea (maybe rightly so...)
There's another possibility for a workaround - namely, have something like ' / #the_nid' appended at the end of the title when you select a node in the autocompletion list (I'm not fond of showing up nids in the UI, but that's the best I can come up with without having to ship a custom js widget almost mirroring the core autocomplete thing)
I have to admit that I kind of neglected this lately. I think I was waiting for the "views" patch to get in.
I'll get back to it ASAP.
Comment #23
karens commentedYched, there is another problem with autocompletes noted at http://drupal.org/node/83572. I'm having that problem myself and it's fairly critical -- once you select an item there is no way to go back to an empty option even when you have marked it not required. There is a patch submitted for that issue, but I don't know how that impacts what you're doing here. Can you look at that and see if we need that patch or not, or make sure that problem gets solved, too when you update the autocomplete feature?
Comment #24
yched commentedHere's a patch that corrects the 'random node gets selected when several nodes have the same title' bug. Well, almost.
It adds a ' [nid:nn]' pattern (with nn = the node id) at the end of the title that gets pasted in the textfield when the user picks a node in the autocompletion list. This, of course allows for unambiguous node identification on submit time. I tried to choose a pattern that is not likely to be part of an actual node title...
The pattern is also added in the pre-filled default-value on node edit form, so that you don't lose the info when you edit / submit without altering the field.
The user also has the possibility to type directly '[nid:nn]' (without any title), if he knows the nid of the node he wants.
Problem is when a title is entered and the autocompletion list doesn't have a chance to add the pattern, i.e. when JS is disabled, or when the user just manually typed the title he wants without picking from the autocompletion list.
For this case, this patch slightly enhances things :
- the current behaviour is to select ONE OF the nodes with that title, which might not even fit the definition of the 'referenceable nodes' for the field
- the behaviour with this patch is to select ONE OF the REFERENCEABLE nodes with this title. Plus you always have the ability to
Not ideal, I know. There could be several ways to handle this last tricky case (warning, 2nd step form...). I have some ideas and I'm open to discussion.
This patch also incidently fixes the issues http://drupal.org/node/83572 and http://drupal.org/node/93433.
Patch for 4.7
Comment #25
yched commentedAnd here is the patch for 5.0. It also includes the few fixes required to make noderef work in 5.0; the current CVS is broken since http://drupal.org/node/78825 got in.
Comment #26
karens commentedCommittted. Thanks yched!!
Comment #27
karens commentedComment #28
(not verified) commentedComment #29
MrTaco commentedthis isn't a problem if you create the relationship with the cck subform module
http://subform.googlecode.com/svn/trunk/subform_trunk.zip
(also provides incline CRUD operations on the child nodes right in the parent node page)