Hi, I'm wondering how it would be possible to find the nodereference field's field name for each instance in a nodereferrer field?

For example, I have a "person" node type, with a couple of different node references, and a node referrer field that catches all the nodereference fields, like this:

Nodereferences:
Rob > is friends with > Nat
Mary > is related to > Nat

Which results in this
Nat > is referenced by > [Rob, Mary]

Is there any way to find which type of node reference field is used in both cases (friends, relatives) from the nodereferrer data? Or do I have to load the referring nodes?

If the latter is the case, consider this a feature request, for the nodereference field name to be stored with the nodereferrer data.

Comments

naught101’s picture

Category: support » feature
Status: Active » Needs review
StatusFileSize
new448 bytes

This works fine for me, just adds another item to the value array.

andypost’s picture

Status: Needs review » Needs work

Very strange patch, need inline comment for code!

       while ($value = db_fetch_array($result)) {
+        // .................................storing ...what for?  
+        $value['referrer'] = $fieldname;
         // avoid duplicate referrers by using nid as key
         $values[$value['nid']] = $value;
       }

Also point where this stored value should be used if it stored...

EDIT: each code line with it's own comment.

naught101’s picture

andypost: the fifth line of the original request:
"Is there any way to find which type of node reference field is used in both cases (friends, relatives) from the nodereferrer data? Or do I have to load the referring nodes?"

This saves the nodeREFERENCE field name in the nodeREFERRER data in the node itself. It means that you can now find out what kind of reference is referring to the current node without having to load the referring node.

I currently use it in this: #212362: Display user/node relationships, and it works perfectly.

This patch isn't intended to increase the functionality of nodereferrer as such, but to make it easier to work with when creating other modules.

andypost’s picture

I'm open to get features but I need to be sure that they break nothing and well documented.
So glad to commit this change if you change your patch with by adding a comment about this line!

naught101’s picture

Status: Needs work » Needs review
StatusFileSize
new614 bytes

Well, I haven't tested this extensively, but I can't see how it would break much, since nothing relies on the values being added.

Here's the patch with an appropriate comment, didn't realise I put it after the comment in the previous one, sorry.

andypost’s picture

Status: Needs review » Fixed

Commited to 6 and HEAD

naught101’s picture

Status: Fixed » Needs review
StatusFileSize
new1.21 KB

Hrm... but I've just realised that when one node references another twice (with different fields) only the last fieldname will be recorded. This is due to the way noderefererrer reduces duplicates by overwriting previous reference with the same nid.

For my purposes, this is fixed, because I only use one nodereference field type per nodereferrer field. For others, this might be annoying, so I've created a new patch (against CVS head) that will still overwrite the nid, vid, and title, but add all the nodereference field names to a sub-array.

andypost’s picture

It seems more expensive then before

naught101’s picture

Yeah, but it's pretty minor, only two more string overwrites. It doesn't bother me, since it doesn't affect me, just seems like it might be worth going all the way...

andypost’s picture

$values[$value['nid']]['referrers'][] = $fieldname;

where is optimization?

naught101’s picture

I'm sorry, I don't understand the question?

andypost’s picture

Try to use

-        $values[$value['nid']]['referrers'][] = $fieldname;
+        $values[$value['nid']]['referrers'][$fieldname] = TRUE;

for example

I see no need to duplicate $fieldname many times

naught101’s picture

andypost, if you do that, you end up with the fieldnames as keys, instead of values in the array, no huge problem, but that doesn't make much sense to me. The array is a list of all the node reference fieldnames from Node A to Node B, so it makes sense to list them as values (only the fieldnames that are actually used run through the for loop).

You need to use $fieldname multiple times because one node can refererence one other node with two fields. For example:
"book A" > has Author > "Bob"
"book A" > has Publisher > "Bob"

kbellcpa’s picture

Where does this patch go in the nodereferrer module? Just paste it in the bottom?

andypost’s picture

When we get more responses about that nothing was broken

Also I think data should not be duplicateed

So maybe proceed with

-        $values[$value['nid']]['referrers'][] = $fieldname;
+        $values[$value['nid']]['referrers'][$fieldname] = $fieldname;

or use suggested in #12

kbellcpa’s picture

So paste in the bottom of the nodereferrer.module? I have never applied a patch before.

Also, I am having trouble installing graphviz nodereference graph and graphviz nodereference field. Both say that the node_reference is missing.

Do you know how to fix this?

naught101’s picture

kbellcpa: http://drupal.org/patch/apply
And please keep discussion of other modules to their own issue queue.

@andypost: I don't see how $values[$value['nid']]['referrers'][$fieldname] = $fieldname; is better/more optimised than $values[$value['nid']]['referrers'][] = $fieldname;, but I'm happy for that to be the solution if you are.

andypost’s picture

@naught101 By this way we get no duplicates in this array and could use array keys and values both

naught101’s picture

Good points. Works for me.

andypost’s picture

@naught101 can you roll a new patch?

naught101’s picture

StatusFileSize
new1.22 KB

here you go, sorry for the wait.

andypost’s picture

Status: Needs review » Fixed

Commited. http://drupal.org/cvs?commit=403968

naught101 thanks a lot for patience

naught101’s picture

Thank you :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Marko B’s picture

Is this added to any version? I have similar problem, have many reference fields in one node but on the back referrence list i only get this one node and i would want to have list of fields and their names, this is rather useful to have.