The Relation module has integration with Rules, primarily to allow creating new relations. It is also possible, but slightly awkward, to fetch and manipulate relation entities with Rules.

This documentation page assumes that you are already familiar with the basic concepts in Rules – in particular with lists.

Creating relations with Rules

To create a new relation in Rules, you must first have a list of entities that should be used as endpoints in the relation. The following steps will provide you with actions to create brand new relation in your rule:

  1. Add a new variable, of the type list of any entity items. (Note that lists of specific entity types won't work!)
  2. Use the action add an item to a list to add any of the entities available to Rules to the soon-to-be end point list.
  3. Repeat step 2 until you have the end points you want.
  4. Use the action create a new entity and select a relation of the type you want.
  5. In the entity creation configuration, select the entity list as endpoints.
  6. Done!

Possible stumbling block:

  • If you want to use the relation ID (for example to create links), you must use the action save entity – and also use the force immediate saving option. This will save the relation at the end of the currently executing rule, meaning that you will need a rule set (or a chain of rules) to be able to fetch the relation ID once saved.

View a screencast about creating relations with Rules

Fetching and manipulating relations with Rules

Using Rules you can also load existing relations, and perform actions on them or their end points. However, this is complicated by the fact that you usually need a good identifier for the relation you want to fetch – such as its relation ID or the entire list of endpoints.

Assuming you have one of these, you can load a relation using one of the load entity by ID or load entity by property action, fetching the entire relation entity into your rule.

Once that is done, you can access the end point objects as a list, either by looping through the list or by using data selection and selecting (say) the first end point directly.

Possible stumbling block:

  • A possible problem is that Rules may have issues recognizing the type(s) of entities stored within the list of endpoints – which may be necessary for making Rules access field data (or even, say, node titles). You need to use components for this and use the entity type condition in the component.

Comments

busla’s picture

Thanks to you all for the tut :-)

I´m trying to reproduce this with VBO but there are alot of options missing on the page where I choose "relation". I don´t get the relation type. I thought it was a panels problem but the same happens when I create a page with the VBO view and add it as a menu tab on a node.

I thought the problem was related to the relation entity itself since it was directional but I´ve created a new symmetrical one but still no go.

My understanding of Rules and VBO is that it treats it like a normal list.

I´m sure it´s a minor problem since I´m practically reproducing the same step as in Itangalo´s Relation + Rules screencast, except for the VBO part.

Although I´m having this problem I´m very happy that it´s not a Page Manager issue :-)

C-3PO’s picture

Hello guys!

Using Relation with Rules is a great experience! just have one question - how can a site builder (not a developer) actually delete relations using Rules? It is pretty easy to fetch endpoints of a relation and manipulate them further. But how do I fetch the relations that an entity is a part of? Could anyone please take a few minutes and explain this? That will be greatly appreciated!

miromarchi’s picture

To fetch relation by endpoint see issue Fetch Relation by Endpoints in rules.

Miro Marchi

timqallen’s picture

I've been working on this for a while now and I can't figure out how to get step 2 (add item to list) to actually select the node being viewed. What I'm trying to do is display a list of content in a block (via views) and create a relationship between the content in the block with the node being viewed. I've tried to use the rules link module and the VBO module. With both routes I'm getting stuck in how to add the correct nodes to the list - and I don't see what I think I need in the data selector list.

Thanks for ANY tips!