I have been trying to set up a Drupal View of a a 3-way relationship in CiviCRM.

The view I wanted was that showed a list of contacts that are provided services from the current user's employer, as below

(current user) contact a => (is employed by) => organisation => (is a provider of) => contact c

I created a view of CiviCRM Contacts, and could not see a way of getting this relationship to display what I wanted without building a custom module, which is explained below.

2 Answers 2


You can do this with a custom module and using the preprocess views hook to pass in the contextual filter. The filters are passed into the $args[] variable.

You also may need to enable multiple values in a contextual filter, if you have more than one relationship from Contact A to Contact B, in this case this is multiple employers. This is under Contextual Filters > Advanced > Allow multiple values.

You do not add % to the path as you would normally do with a contextual filter as we are programmatically inserting it.

Create a custom Drupal module with the following code

* @implements hook_views_pre_view().
function HOOK_views_pre_view(&$view, &$display_id, &$args){

if($view->name == "my_view") {
      // Check that we are altering the correct view based on and $view->name
      require_once 'api/api.php';
      //get the matching civi id for the current user
      //This is using Civicrm version 4.4.6

      global $user;
      //first get the current user's Civicrm record
      $params = array(
         'version' => 3,
         'sequential' => 1,
         'uf_id' => $user->uid,
      /*The contact ID is in $result['values'][0]['contact_id']*/
      $result = civicrm_api('UFMatch', 'get', $params);

      $params = array(
          'version' => 3,
          'sequential' => 1,
          'relationship_type_id' => 5, /*get your relationship ID from /civicrm/admin*/
          'contact_id_a' => $result['values'][0]['contact_id'],
      $result = civicrm_api('Relationship', 'get', $params);

      //temp stores the string which will be passed to the $args[] array
      $temp = "";

      //loop through the a => b relationships, and append all the ids of contact b
      foreach ($result[values] as $key => $value ) {
         if ($key > 0)
            $temp .= "+";
         $temp .= $value['contact_id_b'];
      // populate the $args contextual filters array with the ids.
      $args[] = $temp;

You can do this without a custom module. In Views Relationships your first add a Views Relationship for the CiviCRM Relationship, and then add the Contact at 'the other end' of the Relationship. So for each Civi Relationship you will create 2 Views Relationship. Hence to daisy chain A to B to C, you will have 4 Views Relationships

Not easy to explain especially when both Views and CiviCRM are using the term Relationship.

Add Views Relationship 1 (VR1) of type "Connects a contact (as contact A) to a relationship." (eg employer of) Add Views Relationship 2 (VR2) of type "The contact B" use select VR1 as the 'Relationship'

So you have now connected person X to Org Y

Add Views Relationship 3 (VR3) of type "Connects a contact (as contact B) to a relationship." (eg Provider of) - select VR2 as the 'Relationship' Add Views Relationship 4 (RV4) of type "The contact A" select VR3 as the 'Relationship'

So now you should have connected person X to Org Y and then Org Y to person Z.

For creating RV3, you need to work out whether to use "starting from A" or B as it will depend on how you created your "is a provider of" Relationship. Clear? Not that easy to explain ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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