Setting a view relationship around the content author

Events happening in the community are now at Drupal community events on www.drupal.org.
brentonj's picture

For my d6 site, I was wanting to display two different types of nodes both of which are created by users. Not wanting to show my stupidity I searched and searched but couldn't find anything and had previously writter a dynamic filter code for d5 which struck me as messy. Finally, I wrote something for myself that can enable it as a relationship in my code. Thought I'd share it in case other people needed the same code. If there is a simpler way, I'd love to know it but to me this is incredibly simple and shows the power of views2.

function user_relation_views_data(){
    $data=array();
      $data['node']['uid']['relationship'] = array(
        'base' => 'node',
        'base field' => 'uid',
        'field' => 'uid',
        'group' => 'node',
        'title' => 'Author Relationship',
        'help' => 'Relate nodes by the author',
        'handler' => 'views_handler_relationship',
        'label' => t('UserID'),
      );
 
    return $data;
  }

Comments

where do you put this code?

tony584's picture

brenton,
where do you put this code to make it work? i need this feature badly. thanks for writing it. cheers,
tony

Location of code

bjj's picture

Hi Tony,

Sorry for the delay in responding, I've been locked out of my account after they merged drupal.org and groups.drupal.org - finally gave up and added a new account.

Just put it in a custom module, as nick suggested. Only trick is to ensure you respond to hook_api() otherwise it won't search your file for the hook. Let me know on this account if you need any help.

Brenton

Sorry

stickgrinder's picture

Sorry, I'm really a noob in extending views. Could you give me more details about this? What do you mean with "ensure you respond to hook_api()"? I can't understand where in my module I have to put the code... MODULENAME.views.inc? .module in hook_api? Is it a hook_views_data() by itself?

Many thanks.

Have you got a module? If

bjj's picture

Have you got a module? If not, I created a file called user_relation.module and put in user_relation_views_data() as shown. It is that simple. The hooks api I was sure you needed but checking back on the functionality it seems it wasn't needed, whether or not you include it you can test by trial and error. The documentation for this is here: http://views-help.doc.logrus.com/help/views/api

Let me know if you need more help.

function user_relation_api(){
return array(
  'api' => 2,
  'path' => drupal_get_path('module', 'user_relation'),
);
}

Hi, bjj! Many thanks for your

stickgrinder's picture

Hi, bjj! Many thanks for your reply. I've got a module (I called it views_author_relationship, for sake of clarity :P) and I've put the above implementation of hook_views_data in it but nothing happened. After module activation I edit my view, add a relationship and check for this new type of relationship to show up, but I can't find it. :(

I'm now trying a more complex way, cloning and modifying another relationship handler (the one from content_profile, I find it pretty clear to read). It could be an overkill solution to implement a whole new handler by myself but I can't get your "simple" one to work... or I really miss something O_O.
That could be possibile, since I'm permanently in a daze! ^_^'

I'll try your solution, registering the handler with hook_views_api and let you know if this works. Many thanks.

Just to inform you that my

stickgrinder's picture

Just to inform you that my "stolen" method works. Didn't try your one but I will as soon as I can, since I began to understand how Views works and coding a whole new handler to manage this out is total overkill. Even if, at least with custom handler I could select a target content type... that's not bad at all! :)

It's a sin I cannot attach it here. Anycase, I think I'll contribute a simple module for this. I know it's very easy to do that but it's also very strange not having this already available in views itself! I think many non programmer users could take advantage from this. I hope, at least.

Many thanks again for pointing me in the right direction!

hey there, happen to have

arski's picture

hey there, happen to have opened a module that does this? :) or why doesn't anyone post a feature request in Views itself? :o

Got it

stickgrinder's picture

I there everybody, I checked in my past projects and found the custom module to do this. I never applied for a CSV account since I lack the time, but I will.
In the meantime, drop me a PM if you want it sent by mail! :)

Bye!

Nice, I threw this in a

Nick Robillard's picture

Nice, I threw this in a custom module. Works like a dandy.

module on github

kevindees's picture

Hey, i too was to lazy to register for a module but I do have it on my github account for those who may need it.

https://github.com/kevindees/author_relationship

note: that i have not tested the module to the fullest extent.

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: