Views integration with the relation module

By:

on

January 24, 2012

introduction

Relations is an awesome new module for Drupal 7. It essentially allows you to link any entities together and which are themselves fieldable. I can think of limitless ways in which this can be super useful. However, the topic of this blog posting is not to heap praise on relations and explain in detail what they can be used for; I'll let you discover that yourselves. The intention is rather to give a practical example of how to create views of separate entity types tied together with relations. Something that I've had to play around with for hours to figure out. Perhaps this will save you some time.

Note: ​At the time of writing this the functionality I'm discussing only exists in the dev release of relation. Beta3 does contain this. If people from the future are reading this blog the functionality was commited Jan 21st by chx.

​An Example relation

Just read about this example on the d.o. issue queue where someone needed help with the views integration. So, lets say we create a relation that defines a relationship between a user and nodes called 'follow'. So that is to say a user can select particular nodes they deem interesting and choose to follow them.

Note: ​This could be accomplished other ways as well such as using the flags module, but we'll ignore that for now and consider that a relation can probably give us more functionality. 

We add a field to the 'follow' relation called 'category' to categorize the nodes which the user is following.

So now what we have is a directional relation user -> node ​with the single field 'category'.

Views you can create

Right off the bat I will tell you that you cannot come at the relation from its reverse end in views. So if you have a directional relation such as user > node (haven't checked out symmetrical yet) you cannot create a content(node) based view and select the relationship for the reverse of the relation: node > user. It doesn't exist in the current implementation. Knowing this will save you much time. The only relation defined views relationships you will see for a particular entity type (views base) will be the relations that originate with that entity type.

Using our example relation above if I create a user based view then I will see the views relationships:

User: Relation: follow (user -> relation)

User: Relation: follow (User < -- > node)

Or something along those lines; it might be the actual content type instead of node; inconsequential. However, like I mentioned above you will not see any reference to the relation if you created a content based view. This isn't to say that you cannot create a view that lists the information from the perspective of the reverse direction. By using the user -> node ​views relationship you can access all the fields from the node and don't need to include any user fields at all if you don't want to. That should be pretty straight forward if you try it out.

​What isn't obvious is how to also include fields from the user, relation and node in one view. This is where the user -> relation relationship comes in handy. You'll want to set the following settings to hook it up properly and avoid duplicate rows: (base = source, required = checked). That provides you with the fields from the relation as well as the user. Now we can access our 'category' field from the relation. To get the node's fields in there as well we take a look in the views relationships section again and there will now be a section called 'relation'. In that section we will find the relationship Relation: Relation: Follow (relation -> node). Choose the settings (base = target, required = checked, relationship = follow) to ensure its hooked up properly and we avoid duplicates.

Blam!  You're now a relations - views expert.

 

Huge thanks to @chx for the views work and props to all the maintainers of the relation module. Its pretty slick.

About The Author

Jesse is an extremely enthusiastic and confident developer with extensive experience in the design, development and launch of Drupal websites. Jesse is a father and a husband, has a degree in Engineering Physics, has built a robot that solves the rubiks cube, founded vegetablepatch.ca, a local organization aimed at sustainable organic farming within the city of Ottawa, and is most definitely a moose-man of the forest... quite possibly the moosiest.