5

I am trying to write a relationship handler for Views 3 in Drupal 7, but am failing even to begin.

Basically, I have a bunch of nodes of various types all with the same field attached to them. This field is a reference field linking off to a Civicrm contact - but that's not important, as essentially these fields just contain integers.

Now, let's say I have a node of content type 'story', and its reference field contains the integer, oh, let's say 55. In addition, there are nodes of content type 'news' which also contain the reference ID 55.

I want to build a block view, that sits on the story page, that reads in the node's nid as its argument, and then through a relationship it discovers all other nodes that contain the same reference integer as itself.

(In my head, the sql looks something like: SELECT n.nid FROM node n INNER JOIN node n2 ON n.reference = n2.reference [Assuming, for the sake of simplicity, the field data is kept in the node table]).

How do I create a Views handler that caters for this relationship? The only documentation I can find is the views api, which is for version 2 and not 3, and besides is utterly unhelpful. Reading various module's views handlers is not helping either, and views tutorials on the web are almost non-existent.

9

Are you really sure you need a relationship handler for this?

In general the default relationship handler is already quite flexible because it uses the default join class which is really powerful.

There are some use cases for relationship handlers, for example the relation module, but this module does some vodoo stuff.

So back to the problem. It might be enought if you define 'relationship' with the right field and base field.

PS: There is a big reason why there is no documentation about api=3 because the difference to api=2 isn't big at all.

Here is an example how to define a relationship.

  $data['node_revision']['vid'] = array(
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'node',
      'base field' => 'nid',
      'title' => t('Content'),
      'label' => t('Get the actual content from a content revision.'),
    ),
  );
  • Thanks for this comment! This helped me write a views contextual filter for another project, and hopefully will allow me to return to writing this relationship. I will post the code for that when it is completed. – Torrance Jun 1 '11 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.