Drupal Relation module

April 4, 2014

So todays module to look into for a few hours has been ‘relation’. I have previously only heard of it from talks, and so wanted to delve a little into it, get it setup, and see what use cases I would grab it for. In the past, node/entity reference has always been my goto module, it is easy to setup, and more importantly, is pretty easy for the content admin user to use (it’s surprising how many people don’t consider the content admin user one of the most important people when it comes to the final product).

So, before we get started, the usual process of hitting d.o and seeing what’s there. The page is well structured, the module is used on a lot of sites, and appears to be very actively maintained (when I last viewed the page, there had been an update within the last 24 hours). But importantly, the docs for new users to the module have links to screencasts (and who doesn’t love those), worth noting they are relatively old, but good to get the concepts of it down.

The main three I would recommend are:

But this post isn’t just to reproduce that d.o page, it’s so I can share (and maybe rant) about my experience and any tips I find for future reference.

My setup

  • drupal7 default
  • admin_menu
  • views
  • views_ui
  • relation
  • relation_ui
  • relation add
  • relation select
  • relation dummy field
  • devel
  • devel_generate

Creating some quick relations and displaying them

Okay, modules are there, check your permissions, and add your first relation type using the admin page at structure > relations types > add. If you have seen the screencasts you already know this, but the label should be in the format of ‘is child of’ or ‘references’ so that it reads nicely such as:

node1 references node2

node2 is child of node1

The reason for this is not only to make it easier for us to understand, but there is also a nice human language display option later that we may want to use, so plan ahead.

The other options on this page should be read and understood (the screencasts will help here), but a quick bit of help:

Arity = “In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands the function or operation accepts.”

Next we just generate some quick content, and start connecting things. I was planning on connecting articles using ‘references’, articles were just generated using devel in the usual way. Then I edited my content-types to have the fields ‘reference add’ and ‘reference select’, I added both just so I could compare them, I would recommend you do this too at first, as each has a good use case in certain contexts (personally though, relation select is my favourite). You are now ready to edit some entities, and add your relations ... so what are you waiting for?

Once you have a couple of relations setup, you will notice it just lists them to the user, this is ugly, so set their display to hidden straight away. What we want is the much sexier ‘reference dummy field’. This adds a field which isn’t editable, and appears to purely be for display purposes. This is what allows us to read it in a much more human readable way, my local test now reads like:

“Ex Utrum Uxor references Causa Quidem Vereor, Abigo, Lenis Lobortis Meus Nisl, Minim Pneum Similis Sit”

Is that all it does?

Not exactly no, what we have here is the tip of the iceberg. It is acting in a very similar way here to how references work. Some gotchas to be aware of though are that you cannot edit endpoints once they are created, you would need to delete the reference completely and create a new relationship. This was planned, but if you need to get around this, that is just another reason to use ‘relationship filter’ as it allows you to remove/add endpoints (nice).

But, here are a few cool things it also does, several were harder, or impossible, with reference:

Full views integration - you can create views blocks for your pages showing all the references for that node with some contextual filters

migrate module - whilst I have not yet done a lot with the migrate module (this will be a future post) it does appear to have integration for this too, which will be handy when reworking sites, or even moving to D8 (depending on upgrade paths)

rules integration - you can trigger things depending on certain conditions, as you always do with rules

drush - I can also see some drush integration in there too. It doesn’t do much, but if you are testing some feature, it creates a load of dummy relations quickly to see if any random parts break. I might mention to Pete whether he wants to get this added to http://www.drushcommands.com for future reference too

What does it do that I should be aware of?

  1. For one it all works using entities. Where reference originally just had a field that added an ID in, this goes through an intermediary entity. Whilst this does add an extra SQL JOIN, it does open up some doors too
  2. One of the doors mentioned above, is that a relation is now fieldable, therefore you could create a relationship for a set of people and add more detail. If it’s for sub-teams you could add a link to a team website maybe (bad example I know, but first thought that came along).
  3. I have seen a few bugs crop up when people have changed config after aving created relations. If you do this, I strongly advise you test your content thoroughly, and look at the content > relations page to ensure things are as they should be.
  4. As mentioned above, as things are now entities, you have a nice place to view them as you would nodes. So they are listed and easy to manage. If this got out of control (for example you make 20+ articles a day, all with relations) then you could look at creating some admin tabs using views quite easily.
  5. As you can set relations to be bi-directional, you can edit from both ends of the relationship (not just on the owner) - this also makes views a LOT easier as ld articles can update nicely as new articles reference them

Why would I use it when reference does exactly what I need?

Generally, there is a line which could move, but it puts references as the tool if you want simple one way connections, and those connections should be editable once created. The simplicity of references can both help and hinder your projects, so just scope this out beforehand, looking at what they user really wants, and how you can help them do this.

And?

Nothing yet, but I may update this article if/when I work more with relations. It appears to be used quite a bit, so expect to come across it in a project or 2, so may share my thoughts on it then.