You are here

Drupal Relation Module Configuration Manual

Today I'll show you guys how to install the Relation module for Drupal 7. Believe it or not, it took me almost five years to fully figure out how to install what I think is one of the most confusing modules ever made lol.

So what exactly does Relation do? Let's say you have a film and its sequel:

* Film 1 is followed by film 2.
* Film 2 follows film 1.

The beauty of Relation is that once you create one relation (in either direction), it creates the other one for you automatically. It's this simple thing that this module does amazingly well.

If you want to look at a live example, take a look at the Connections page of a movie called Moonraker on IMDb. In this example, you can think of the module as basically the feature that allows any member of the site to create movie connections. ;)

But the main problem with this module is that it was built with administrators in mind, or at least this is what I've been told by a lot of people, including Steve Oliver, creator of the Relation Select module (speaking of which is another module I'm still trying to figure out, hehe). So basically, I've been wondering in vain for years how to enable users of a membership-based site to use this module while adding new content, because otherwise I always thought the module would be pretty much useless. In IMDb, this would be like only administrators being able to create movie connections. And if members can't create them, what's the point, right? And to make matters worse, the readme file itself has always been missing some critical information that would allow you to install the module in the way that I just described, which is how I think most people will use it for anyway.

So if you're one of those people who were never able to figure out how to install Relation in this way, you're in for a treat. Read on, as I will reveal a couple of secrets you need to know in order to get the module working in the way I believe it was meant to be:

#1. Download and install the Relation, Relation Endpoints Field & Relation UI modules (which are all part of the Relation module).

#2. Go to /admin/structure/relation and set up a new relation. For the purpose of today's example, let's check the "Directional" option, and just fill in "follows" for the "Label" field and "is followed by" for the "Reverse label" field. Now click the "Save" button.

At this point, you have a basic relation created.

#3. Here is the FIRST SECRET: Install the Relation Add module.

#4. Go to /admin/structure/types/manage/CONTENT_TYPE/fields and add a new "Relation add" field. For this blog post, we'll call the field "Connections". It is this field that will allow users other than administrators of your site to create a relation while adding new content. And when creating the field, make sure to choose both "Follows" & "Follows (reverse)" for the "Relation types" option and whatever content type you'll be using with the module for the "Target bundles" option.

So go ahead and create a couple of new nodes of whatever content type you want to use the module on. For the purpose of today's example, let's say we have two nodes called "Film 1" & "Film 2". When creating the second film, make sure to create the relation "Follows" (film 2 follows film 1).

At this point, you'll realize that the module at least works. But the problem now is that the display looks weird as hell. Here are what the two film pages look like in our example:

#I. Film 1:

Connections:
endpoints
* Film 2

#II. Film 2:

Connections:
endpoints
* Film 1

#5. Here is the SECOND SECRET: Install the Relation Dummy Field module (which is part of the Relation module).

#6. Go to /admin/structure/types/manage/CONTENT_TYPE/display and select "Natural language" as format. It is this that will change the display of the relation to match what you created in step #2 above. Here are what the two film pages look like now in our example:

#I. Film 1:

Connections:
Film 1 is followed by Film 2

#II. Film 2:

Connections:
Film 2 follows Film 1

Notice that the pages finally look similar to what you saw above in the Connections page of Moonraker on IMDb. Finally! :)

UPDATE (September 17, 2016): Now let's take a look at a minor limitation of Relation:

Let's say we have a film trilogy. Here's how the connections will look like:

#I. Film 1:

Connections:
Film 1 is followed by Film 2
Film 1 is followed by Film 3

#II. Film 2:

Connections:
Film 2 follows Film 1
Film 2 is followed by Film 3

#III. Film 3:

Connections:
Film 3 follows Film 1
Film 3 follows Film 2

But here's how I think the connections should look like instead:

#I. Film 1:

Connections:

Is followed by:
Film 2
Film 3

#II. Film 2:

Connections:

Follows:
Film 1

Is followed by:
Film 3

#III. Film 3:

Connections:

Follows:
Film 1
Film 2

It might seem minor, but imagine what it would look like for a movie like Moonraker. Per my reply to an old issue (#10), I hope somebody eventually fixes this annoying problem.

Now let's take a look at a couple of advanced options when setting up #2 above:

#7. Transitive: It's an option that actually does NOT work, and is related to what I just described above.

For example, if you create a couple of relations so that "Film 2 follows Film 1" & "Film 3 follows Film 2", this option would theoretically create another relation automatically so that "Film 3 follows Film 1". But the problem is that this option has never been implemented, and is briefly mentioned within the module's online tutorial page that "it is merely an information flag to be used by other modules". I take it that this probably means that we'll have to wait until a sympathetic developer creates a new module (called "Relation Transitive"?) that makes this amazing feature work. I sincerely believe this is a major limitation of this module, and I hope somebody eventually fixes it.

UPDATE (September 22, 2016): It looks like I might have to do some research into Bundle Inherit, which might actually be able to provide a simple transitive solution. One user also claims that Rules will work as well.

#8. Unique: It's an option that prevents you from creating duplicate relations. For example, try creating the same relation twice in #4 above when creating the second film ("film 2 follows film 1"). This option will automatically prevent you from doing this. There's also a Relation Unique module that looks like it might extend this feature (but I'm not exactly sure what at this point).

And last but not least, let's look at a couple of alternative solutions if this module didn't exist:

#A. Entity Reference & Corresponding Entity References

It is worth noting here that Corresponding Entity References is VERY unstable at the moment. You'll get error messages everywhere. :(

#B. Entity Reference & Views

There are at least three problems with both options:

#a. First, you have to choose Entity Reference on one side, and let Corresponding Entity References or Views choose the other side. Remember what I said above about Relation creating one relation (in either direction), and it creating the other one for you automatically? ;)

#b. Second, you can't prevent your members from wreaking havoc by creating duplicate relations.

UPDATE (September 22, 2016): It looks like the Field Validation module can actually prevent your members from creating duplicate relations, but I get an error message that's been active for the past few years. The module does look promising though, and I'll try this option again after the bug gets squashed.

#c. Third, you also won't be able to implement the transitive option that will probably be available for Relation in the hopefully not-too-distant future.

#d. Fourth, only Relation allows you to add fields to any relation. For example, you can add a "Notes" field explaining the relation. You can see plenty of these in the Connections page for Moonraker. However, it is worth mentioning here that I believe you might be able to get away with this problem for the alternative solutions by using Paragraphs (or Field Collection) and just adding whatever additional fields you need. I haven't actually tried it myself though, so don't quote me on this yet. :P

It is also worth mentioning that using Entity Reference & Views will enable you to solve the minor limitation of Relation that I described above. It is the one advantage that using this alternative solution currently has over using Relation (I haven't tried this with Entity Reference & Corresponding Entity References though).

So I hope this explains the raison d'être of Relation a bit more clearly. It's a great module that does one thing really, really well.

And that's it. I hope this helps some people start using this awesome module. :)

You are here