I'm going to explain this as best I can...
I need a list of backreferencing nids (nodes that point to this node), but without any of the frills (views, themes, or any of that crap).
The page that I need the nids on isn't the referenced node, it's a different page altogether. I loaded the parent node with node_load, but I found that my $node->field_backref_XXXXXXXX was empty.
Is there some function I can call (or create) that will just give me an array of nids that reference a particular node? I can't change my Backreference View for this at all, because it's already in use. This feature is in addition to that one, so the view can't be modified at all. I already took a look at the other support request that dealt with this issue, but the solution was to edit the theme for the View or something like that, and I'll be honest, I didn't completely understand it.
Any help would be greatly appreciated. I know beggars can't be choosers, but the simpler the solution is, the better. ;)
Comments
Comment #1
markus_petrux commentedUse Panels, and then you can add as many stuff as you need to the node view page. You can add the same field several times, each one with a different formatter. If you implement additional formatters, you can render the back references list as you wish.
Search for CCK Formatters module to see an example on how to extend existing fields with additional formatters. See the formatters implemented in Node Relationships module to see which information is available and how to use it. You have 2 examples here. One renders a view, the other performs a query to compute a number.
Obviously, you need custom code to do it.
Comment #2
SpikeX commentedBut I don't want to modify the node view itself - all of this is happening on a different page entirely, and I'm just loading the reference node via node_load manually. So the only data I have available is a $node object that was returned from node_load. No CCK data, no Views, none of that, just whatever comes out of node_load.
I've gotten pretty good at hacking module code over the years (my site is very niche - I have a lot of strange needs that I end up putting together myself), so if there's no easy way to get a list of nid's, where in the code would you suggest I look that I could maybe get a list of nids manually?
Comment #3
newtonpage commentedI had a similar requirement that had 2 cases: 1) nodes that could be referenced by more than one node and 2) nodes that could be referenced by only one node. It turned out the cases were similar but that the many-to-one case was more complex. As with you, all I had was the currently-viewed node object.
Note first, however, that in my case, I have "permanent" nodes and "calendar-based" nodes. A "permanent" node may be referenced by one or more "calendar-based" nodes but "permanent" nodes cannot reference anything - - but can only be "pointed to" by "calendar-based" nodes. Further, "calendar-based" nodes cannot point to other "calendar-based" nodes, only to permanent nodes. Again, these are my own requirements that may not apply in all cases - - but I believe that the following code may be easily applied to "bi-directional" cases.
What I did was this:
1) created a node reference cck field where a node may select from an auto-complete only nodes that are "reference-able" by that node. This element is only exposed when a user is creating (or editing) a "calendar-based" node. (This is done with jQuery "hide-show").
2) create a view tied to this node reference cck field that selects only nodes that can be pointed to.
Note that this a fairly simple view to create but, if needed, I can share that structure.
The code in the node.tpl is as follows:
This produces an array with $i elements where the ith key is the node title and the value is that node's nid. Thus, the ith linked node pair may be referenced:
I then use a for loop to list these in the appropriate place:
I then do various other things with this info but net-net, produce a UI element wherein the user may 1) view the linked nodes; 2) click on a link to visit any of the linked nodes.
This may or may help you and there are probably more Drupal-ish ways to do this, but this works for me.
I do not think this is appropriately "rollable" in a patch but is offered as one possible solution to what you need to do.
Comment #4
markus_petrux commented@SpikeX: Ah, so this is custom code that generates or executes somewhere else than node view. Sorry, I did not understand this.
Well, then you can use functions content_format() or content_view_field(), and then you can print the same field using different formatters as many times as you may need. These are functions located in content.module, part of CCK, and are documented in the code itself (doxygen). Examples to use them can also be found scanning the code in CCK.
I'm afraid all this stuff is far beyond the scope of the Node Relationships module. It requires custom solution, which is something I do not cover here. You may need to ask in the development forums.
Comment #5
SpikeX commentedI can't just print out or use the data from the CCK field, though (the backreference field that your module auto-generates), because it's empty.
What function in your module could I call that would populate my custom $node object's fields with the backreference data (the $field_backref_xxxxxxxx)? (I hope that made sense...)
Comment #6
markus_petrux commentedThe field is empty because there's nothing stored in the database. It exists only when rendering.
To see an example on how to use content_view_field() look at function content_content_field_content_type_render() located in includes/panels/content_types/content_field.inc
To see an example on how to use content_format() look at the render() methods of CCK field handlers for Views, located in includes/views/handlers folder, files content_handler_field.inc and content_handler_field_multiple.inc
Comment #7
newtonpage commentedI am not the maintainer here but I will try again to help.
You are certainly correct that, in general, CCK fields are available for output in nodes via the node object - - and correct to try some variation of:
$node->CCK-BACKREFERENCE-NAME[SOMESUFFIX];
But this will not work since, as you say, this field is empty. In many other ways, I think, this is an exception to the CCK behavior. While the backreferences are not present in the node object, you can certainly add them, if you wish.
As I understand it, you are located on some node and want to know this node's backreferences.
Again, I assume you know the nid of the node of interest - - that is, the node for which you want the backreferences.
While this may not suit your needs, I suspect it might. This is essentially a re-statement of my earlier post but less verbose and hopefully more explanatory.
I found that the best way to get the references was to access the table that holds them - - since this is CCK, this is stored in content_type_YOUR-CONTENT-TYPE. So, you need to access the field in this table since it holds the nids for the backreferences. From these nid's, you can, as you know, get any info you need.
So, you can either make a view that generates these nid's or you can simply do a DB query from the relevant node.tpl (or a module or wherever you wish).
I accomplished this as follows - - note that 1) I did this in a node.tpl script and 2) I already had the nid I needed.
So, in SUDO, assuming that the node of interest may be linked to more than one node and assuming you would like the return in an array rather than an object (that was my preference here):
- get the nid of the node for which you need the backreferences
- init the sql query
- init an array variable
- init a counter variable for use in the while loop in formatting the array
- invoke the (convenient) Drupal DB function(s)
- pass the result to a while loop to retreive the individual results you want from each node (title and nid, for example)
- output the results as you wish by printing the array elements
In code:
I apologize if this seems simplistic, but I hope this helps.
Comment #9
geerlingguy commented#7 was immensely helpful to me in making a custom module to grab backreferences to filefields in the referencing nodes. Thank you!