2

I want to add a rule (using the Rules module) for printing a message after changing a field in a field collection.

The field collection consists of the following three fields:

  1. Reference to Version (type: Entity reference)
  2. Related Entities (type: Term reference)
  3. Related Properties (type: Term reference)

What I am using for Rule (I have attached a print screen from the ruleenter image description here

EVENT: "Before saving content of type X"

CONDITIONS: Here I am using NOT Data comparison, but it is useless...

ACTION:"Show a message on the site"

How can I check if the data from a field collection has changed?

  • There is no answer for this issue please?? – Aleka Seliniotaki Jul 25 '16 at 9:30
0

Processing field collections is always fun, and even more fun to do so with the Rules module. My favorite sample to refer to (for actually doing so) is contained in part 1 of my answer to "How to iterate over all field collection items in an eMail body using Rules?".

Have a look at comment #2 of the issue about "Field Collection does not work with Rules' 'node-unchanged' option", which states:

... For example:

  1. Create a content type with a field collection that contains an integer field.
  2. Create an "After updating existing content" rule for that content type.
  3. Add a "Show a message on the site" action that prints the integer field attached to node:field-collection and node-unchanged:field-collection.

The printed value for node-unchanged is wrong. It shows the changed value.

So far the bad news, read on in comment nr 3 of that same issue, which is like so:

The reason that node-unchanged and node both show the same (new) field collection values is that the node stores a reference to the field collection. The field collection is a separate entity. Changes to it can be caught by the rules events for field collections. The "after updating an existing field collection item" event gives the correct unchanged and new values.

With this explanation in mind, have a look at this rule (in Rules export format):

{ "rules_verify_updates_to_field_collection_items" : {
    "LABEL" : "Verify updates to field collection items",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "field_collection" ],
    "ON" : { "field_collection_item_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "field-collection-item" ], "field" : "field_reviewers" } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "field-collection-item-unchanged:field-reviewers" ] },
          "ITEM" : { "previous_reviewer" : "Previous reviewer" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "NOT list_contains" : {
                      "list" : [ "field-collection-item:field-reviewers" ],
                      "item" : [ "previous-reviewer" ]
                    }
                  },
                  "DO" : [
                    { "drupal_message" : { "message" : "Reviewer [previous-reviewer:name] was removed from the list of reviewers in field collection item with id [field-collection-item:item-id]." } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "field-collection-item:field-reviewers" ] },
          "ITEM" : { "current_reviewer" : "Current reviewer" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "NOT list_contains" : {
                      "list" : [ "field-collection-item-unchanged:field-reviewers" ],
                      "item" : [ "current-reviewer" ]
                    }
                  },
                  "DO" : [
                    { "drupal_message" : { "message" : "Reviewer [current-reviewer:name] was added to the list of reviewers in field collection item with id [field-collection-item:item-id]." } }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

Some details about the above rule:

  • Rules Event: After updating an existing field collection item. Note that this is different from the Rules Event you're using!
  • Rules Condition: Entity has field, for a field with machine name field_reviewers, which is a multi value entity reference field (to users).
  • Rules Actions:
    • The 1st loop (related to field-collection-item-unchanged:field-reviewers), will discover all removed reviewers.
    • The 2nd loop (related to field-collection-item:field-reviewers), will discover all added reviewers.

Obviously, for this rule to work you'll also need the Conditional Rules module, though you could also rework the Rules Action to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module (for more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?").

Here is a demo of this rule (= some Drupal messages shown after I added/removed some user references in 2 field collection items (with id = 5 and 6):

Reviewer Hillary.Trump was removed from the list of reviewers in field collection item with id 5.
Reviewer UltraBob was added to the list of reviewers in field collection item with id 5.
Reviewer Donald.Clinton was removed from the list of reviewers in field collection item with id 6.
Reviewer Dries.Buytaert was added to the list of reviewers in field collection item with id 6.
Reviewer Pierre.Vriens was added to the list of reviewers in field collection item with id 6.

To finish: you're question may require a slightly different set of Rules Conditions, i.e. since you want to compare some field before the update (= unchanged) versus after the update. But if you're using the same Rules Event and Rule Condition (= Entity has field, for your field collection field), you should be able to perform such comparisons also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.