Updating entity referenced users from rules (code snippet)

welly's picture

Hey all,

Just wanted to share some rules I built as I've been racking my brain all afternoon on how to do this but eventually figured it out using Drupal 7 Rules 2 with a Rules Component. Might be useful to some of you, might not be but here it is anyway!

I have an Event node which contains an Entity Reference to a user. The user has a "credits" field. I needed to remove a credit from the user's account every time they attended an event. So I created a rule that uses a "Before saving content" event and a "Content is of type" condition.

I then created a loop to loop through the referenced attendees of a particular event node and then created an action to set the value of the user's credits to the users credit's -1 using the "Set a data value" action element. This was working fine, however I found that when I wanted to make any update to the event that a user was a member of, it was reducing their credits.

So I needed to have a condition in the loop that essentially checked if the user previously existed. You can't add conditions directly to rules loops and so I had to create a component that would both check if the user previously existed AND would run the action to remove a credit from the user. This worked exactly how I wanted to and so thought I would share with you guys in case you needed to do something similar or wanted an understanding of how the new Rules 2 loops and components work.

Note that I used the "unchanged" node instance in the rule as otherwise when you update a node with user references, it'll use the node with the newly added users as opposed to the node prior to you adding user references.

Feel free to ask any questions if you have any!

Rule

{ "rules_test" : {
    "LABEL" : "Remove attendee credit",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_presave" ],
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "event" : "event" } } } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-event-attendees" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "component_rules_attendee" : { "attendee" : [ "list-item" ], "event" : [ "node-unchanged" ] } }
          ]
        }
      }
    ]
  }
}

Component

{ "rules_attendee" : {
    "LABEL" : "attendee",
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "attendee" : { "label" : "attendee", "type" : "user" },
      "event" : { "label" : "event", "type" : "node" }
    },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "event" ], "type" : { "value" : { "event" : "event" } } } },
      { "NOT list_contains" : { "list" : [ "event:field-event-attendees" ], "item" : [ "attendee" ] } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "user: [attendee:name] updated" } },
      { "data_set" : {
          "data" : [ "attendee:field-user-credits" ],
          "value" : {
            "select" : "attendee:field-user-credits",
            "num_offset" : { "value" : "-1" }
          }
        }
      }
    ]
  }
}

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: