Patch to add nodereferrer relationship to Views
chipway-drupal - February 16, 2009 - 11:52
| Project: | NodeReferrer |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | scroogie |
| Status: | closed |
Description
Hi,
This module is great but i can't use a nodereferrer field in views relationship.
Can you add this please ?
Thank you very much.

#1
views integration and d6 version #259837: Nodereferrer Drupal 6 patches + zip + Views integration
#2
still not working with relationships with the nodereferrer 6.x-1.x-dev (March 5, 2009 - 14:15 build)
#3
What is views relationships?
#4
Relationships allow you to declare JOIN's in Views queries. For example the file field module adds a JOIN to the files table on the fid column, so you can display fields from the files table in the view. For nodereferrer this could be interesting to realize hierarchies.
Here is a link to the views documentation: http://views.doc.logrus.com/classviews__handler__relationship.html
It's not clear to me how CCK handles this, but I think you need to add a 'views data' op in hook_field_settings.
Filefield does the following in there:
<?php
$data = content_views_field_views_data($field);
$db_info = content_database_info($field);
$table_alias = content_views_tablename($field);
// Add a relationship for related file.
$data[$table_alias][$field['field_name'] .'_fid']['relationship'] = array(
'base' => 'files',
'field' => $db_info['columns']['fid']['column'],
'handler' => 'content_handler_relationship',
'label' => t($field['widget']['label']),
'content_field_name' => $field['field_name'],
);
return $data;
?>
*update*
I found the place where nodereference does this. It is in field_settings as well:
<?php
case 'views data':
$data = content_views_field_views_data($field);
$db_info = content_database_info($field);
$table_alias = content_views_tablename($field);
// Filter: swap the handler to the 'in' operator.
$data[$table_alias][$field['field_name'] .'_nid']['filter']['handler'] = 'content_handler_filter_many_to_one';
// Argument: use node.title for summaries.
$data["node_$table_alias"]['table']['join']['node'] = array(
'table' => 'node',
'field' => 'nid',
'left_table' => $table_alias,
'left_field' => $field['field_name'] .'_nid',
);
$data[$table_alias][$field['field_name'] .'_nid']['argument']['name table'] = "node_$table_alias";
$data[$table_alias][$field['field_name'] .'_nid']['argument']['name field'] = 'title';
// Relationship: add a relationship for related node.
$data[$table_alias][$field['field_name'] .'_nid']['relationship'] = array(
'base' => 'node',
'field' => $db_info['columns']['nid']['column'],
'handler' => 'content_handler_relationship',
'label' => t($field['widget']['label']),
'content_field_name' => $field['field_name'],
);
return $data;
?>
*update*
I'm stupid. I just realized that there is nothing in the database to JOIN against. No idea how to do that.
#5
Of course there is data. It's the same as for nodereference. We just need to use the other database column (nid instead of $field['field_name'].'_nid' as the key. I didn't find out how to do that yet, but I'm still browsing the Views sourcecode.
#6
Seems like this would be a very good implementation;
Bi-directional Node Reference
#7
#6 looks promising so every referrer can to now all referrer's fields so can query but anyway it makes query more complicated and slow (a lot of JOINs)
So using views can make your custom implication (only developer knows his references between nodes)
#8
Hello!
I have a question on that issue, prior to releasing the 6 version -and many thanks for that!- there was an 'unofficial' views relationship handler for which I sponsored its development and was implemented in the file "nodereferrer_view_handler_relationship_referers.inc" in the views folder.
Now that file does not exist anymore, is there a reason why views relationships have been removed? My guess is it stopped working, so I 'm going to attach it here for future reference in case someone wants to tackle with updating it.
/\
I am stuck with that old version where views relationships still work and am trying to contact the initial developer who wrote the relationship code so that he can take another look at it.
#9
@christopher_skauss thanks for your work but can you provide a patch for current -dev lets make it better
#10
It's a seperate file, not modified existing files, so you can just add it to the cvs.
#11
@andypost thanks for the prompt reply! It is indeed a separate file, there isn't anything to patch. But the problem is it does not work, at least on my setup. It used to work, with the initial 6 version of node referrer. My guess is there is not much to change, I don't know how much node referrer has changed since then though.
I am trying to find some time to look deeper into it, however I thought I should post it here so that people interested in Views Relationship can take a look too and perhaps suggest any fixes.
#12
I think it should actually work. Did you add a nodereferrer_views_api() and nodereferrer_views_handlers() function to the module? This is needed in Views 2 final. Perhaps your version was for an earlier API.
#13
Tried that, still does not work. I re-added the entry for the relationship handler in nodereferrer_views_handlers() but could not get the handler to appear in the Views UI. Haven't given it much thought though, just a quick one to see if it's easy to fix. And please let me clarify, I am not the original developer of the relationship handler, I paid some other guy to do it, and have asked him a week ago to help out with updating it.
#14
I couldn't get it to run either. I'm a bit confused right now. It worked in a prior version? Do you have the complete version of the module with running relationships? Perhaps I can find the problem then.
#15
yes,it did work in the original 6 version plus some patches, I am attaching it with this post. You can find the story around these patches here: http://drupal.org/node/259837
btw, the version I am attaching might have functional relationships, but it throws sql errors on node creation, a bug that has been fixed in newer versions of node referrer afaik.
#16
Ah, now I get it. Instead of using the 'views data' op in field_settings it uses the nodereferrer_views_data_alter() hook. I think it's essentially the same. Don't know which one is the 'normal' way, although most other modules seem to use the 'views data' op.
Attached is a patch that adds this original implementation, which works. But let me say that I don't know how good this implementation really is.
#17
Thanks scroogie ++1, works Great! Patch applies smoothly, Views Relationships work as expected.
If we get more people to test it I think it can be committed.
#18
I recommend to make issue title is more informative so more people pay there attention to it
Right now I have no ability to test patch but glad to commit if it useful
#19
Hello.
Im a Drupal nooby and this is the first time I have ever used the patch system.
I used the cygwin program to use this patch, it seemed to go alright (no error messages), but now whenever I go on a page that invloves using views I get an error message.
Fatal error: require_once() [function.require]: Failed opening required './modules/nodereferrer/views/nodereferrer.views.inc' (include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\drup\modules\views\views.module on line 524
The file is still there however....
Sorry if I am being stupid.
Thnks...
#20
AudioUnderGround: This seems to be a configuration error, or the patch failed to apply. Do you have any .rej files in that folder? From which folder did you apply the patch?
#21
There are no .rej files in that folder.
I put the patch file in the views folder of the nodereferrer module folder.
I then cd to the nodereferrer views folder and applied the patch from there.
#22
Can you try another module that adds a views include file, e.g. filefield if you get the same error with them?
#23
Can you run this code somewhere within your site and give us its output?
drupal_get_path('module', 'nodereferrer').'/views';
As far as I can see you put your modules in the root module folder, not sites/all/modules, which is somewhat non-standard, so let's see if that's the culprit.
#24
Oh, I didnt know it was non-standard...cheers for letting me know that chris :p
Should putting the modules in there, then running the update work?
#25
Haven't tried it myself but a search showed that that's the right way, move them and run update.php
Btw you should not move core modules, only the ones you added afterwards. If you want to be on the safe side you may try taking a db backup before doing that.
#26
Ive tried moving an patching....no luck...
Ive tried with a filefield patch...and it ends up doing the exact same thing...
#27
I had the same trouble. The problem seems to have to do with file permissions on Windows using Cygwin. I can't really explain it but I've done a little research and this is what I've found:
- If you run "ls -l" in the directory with the patched files you'll find the files that the patch has touched DON'T have a "+" at the end of the permissions listing while the untouched files DO. My understanding is that the "+" indicates that the file has some sort of complex ACL permissions granted it.
- There got to be a better way to fix this (hopefully a way to fix it via Cygwin), but what I've done is to simply copy the files using windows explorer. I copy the files that have been patched to a new name, delete the originals, then rename my copy back to the correct name. This lets Windows itself write the files and it then seems to write them with the "correct" permissions (whatever they are). Re-running "ls -l" on the directory confirms the permissions have been changed.
This is kinda janky, I know, but hopefully it gets you back up and running.
#28
Thanks Azinck, works a treat.
As does the patch...wohay!
Thanks to everyone who contributed to the module and patch, and cheers for all the help :D
Just a shame I have a Computing Funamentals to revise for the next few days....just my luck...
#29
So guys, PLEASE tell in this patch WHAT you tried and IF IT WORKED. That is the sense of a review! Otherwise the developer cannot know if this is ready or not. Thanks.
#30
Arr ok sorry :)
Well, after sorting out the patching process (as directed by Azinck) I enabled the addon and proceded to modify one of my content types.
I have a content type 'bands' which has all the usual stuff, name, members etc.
I also have a content type 'Albums' which has album name image, tracks etc.
After enabling this addon, I created 2 new fields for the type Album. One, a usual node reference field pointing to the 'bands' name field. This is called
field_album_band_name
The second was a node refererr. Refering node type was set to 'bands' and referring fields my 'Album' node referrence (field_album_band_name).
Save this.
Went to my Band content type and added a node referrers field. Referring node type is 'Albums' and referring fields is
field_album_band_name.
Save this.
Go to my 'band' view. Set up a 'Node: referrers' relationship pointing to
field_album_band_name
Because this is a 'band' view, it displays the band name and other details. I then have a section for teaser style nodes of other types, for example....band discography.
So i add fields from the content type 'Album' giving them the relationship I just made.
Save this.
Now I just create a band. Put the name, members etc. No albums though because the node referrers field does not show in create forms...which is what i needed.
I procced to create a album, and as well as adding name image etc, I also choose the band name from a dropdown list.
Save this.
When I look at a perticular band, i have the band details and the teaser style discography.
Works perfect :)
Sorry if this is hard to read through, I was writing in a rush.
Thanks again.
#31
Example looks a strange to me because:
Band - title, body, node_referrer (to album type)
Album - title, body, node_reference (to band)
create band, then create its albums each of albums should point to band
than when you visit band a list of albums would be shown automatically
when you visit album you see a reference to it's band
So I still cant understand what this patch for...
#32
It's to do the things you are describing in views. Let's say you want a list of Bands, but only their names, with the title to all of their albums. Some kind of directory.
Or you want to show only Bands, that have albums.
#33
I talk about that for case described by AudioUnderGround there's no need of view
@scroogie But your patch make some views modifications, please describe - seems like adding relation for field, but it's possible to mek with referring to some view which calculates all you need
#34
One of my use cases for a socnet I am doing right now:
I have photoalbums and photos. Photos have a node reference field that points to photoalbums. I want to create a view and embed it in the photoalbum nodes, that show what photos point to the current photoalbum. See attached view for my implementation.
#35
andypost: you mean embedding a view to the content type? that doesn't solve it if you have different places where your content is displayed. The Views and Relationship way is just more flexible.
#36
I think I do need views for my site. While a normal node view does show everything automatically, I will need to pick and choose items for display, i need the arguments, themability etc.
#37
subscribing
#38
if you guys tested this patch and it works ok, please let the maintainer know so that it can be commited.
#39
I applied the patch in #16 against the latest dev. Applies cleanly. I've only tested it in a simple circumstance (one content type referencing one other content type) but it worked perfectly for my purposes.
#40
There's a typo, though: "Referers" should be "Referrers".
Actually, I'd vote to change "Referrers Nodes" to "Referencing Nodes". I think it's a slightly more obvious description of the relationship.
#41
So I see 2 positive comments but we should clean strings from #40
I'm going to review this patch next week + some notice fixes
#42
The #16 patch works OK, unless you need to base the relationship off of an existing relationship. In the attached screenshot, you can see that I based the "Node: Referrers" relationship on the "Courses" relationship. You can see that the title comes out incorrect at that point. It appears to be pulling in the nid incorrectly in this situation.
#43
Suppose after commiting this patch I can make a release version of this module/
So going to test it now.
And Im still waiting for comments and patches...
#44
attheshow: Can you explain your situation better? I don't understand it. What is Course? Is it a node reference field? Does the group refer to the course or the other way around? Are you sure you set the right field in the relationship options? It seems to me that you are using the reference field of the objective.
#45
So let's test this patch against -dev
Please use http://drupal.org/node/106862/cvs-instructions/DRUPAL-6--1
#46
Example as #34
@christopher_skauss check this screen, please
#47
Patch commited to HEAD so after a time it appears it -dev
#48
scroogie,
My "Course Objective" content type has a node reference field ("Course") that lets the user indicate which course the objective goes with. I have another content type, "Course Grouping", that has a similar node reference field in which the grouping is associated with a course. Here's my attempt to indicate which content type references another in my screenshot from comment #42:
Course Grouping -> Course <- Course Objective
#49
attheshow: And did you really choose the fieldname from the 'Course Grouping' content type for the second node-referrer relationship?
#50
scroogie: The node-referrer relationship is based off of the first relationship (the course associated with the objective). I'm not sure if this answers your question or not. I don't think it's really clicking, what you're asking me. Sorry.
I will say that in the end, I ended up not using two relationships in this manner. I switched it to where my base content type was the course, and then I set up two Node Referrer relationships (one to course objective and one to course grouping). It would have been nice to chain the relationships together, but I just couldn't get it to work for me.
#51
Tested latest dev and it works great.
@andypost #46 is exactly what I have in my setup, and it works like a treat. Thanks for the update!
#52
@christopher_skauss Can you help to improve documentation here, just some words about #431318: Much requested documentation
So waiting other reviews to roll out RC version
#53
@andypost Ready! Glad to help!
#54
Hiya...how close is this patch to being integrated into the module? cheers.
#55
@AudioUnderGround: The patch is already included in the latest dev version of the module.
P.S. It's working well for me.
#56
Marking as RTBC.
#57
Commited to 6-dev, thanx to all
#58
Automatically closed -- issue fixed for 2 weeks with no activity.