How to fire a Rule after adding a new file in multi-value field? Working with Drupal 7.56, Rules 2.10, Conditional rules 1.0-beta2 and File Entity 2.4

I can't fire a rule when I add a new image in a multi-value Image field (related to File Entity for inline fields).

I need to act when a new file is added and also when an existing file is updated (each image has inline fields for caption and details) but I can't find the right way to build my Rule?

At the end, I need to fire a rule when a new image (file) is added to send data (fid, url, inline fields values) to an external database and I need another rule when I updated the inline fields of an image (file) to update the external database.

Does anybody has a tip here?

Already tested : Event "Before saving content", Condition "Data comparison" between updated and unchanged node (NOT equals returns always true...), module File Field Rules...

Related :

Using Drupal 7 Rules, how to react to a file upload and change a node field?

Writing custom PHP code in Rules to check if a file field has a new value

Here is my last try (a little bit modified for clarity) with a simple message on site to check if the rule fires :

{ "rules_image_insert" : {
    "LABEL" : "image insert",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional" ],
    "ON" : { "node_update--article" : { "bundle" : "article" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_pictures" } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-pictures" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "entity_fetch" : {
                "USING" : { "type" : "file", "id" : [ "list-item:file:fid" ] },
                "PROVIDE" : { "entity_fetched" : { "picture" : "picture" } }
              }
            },
            { "CONDITIONAL" : [
                {
                  "IF" : { "entity_is_new" : { "entity" : [ "picture" ] } },
                  "DO" : [
                    { "drupal_message" : {
                        "message" : "NEW picture!!",
                        "type" : "warning"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
To process a multi-value field with Rules,  you should use a completed different approach, which is related to the Rules Condition "List contains item" (and/or its negations).

Have a look at this rules example (in Rules export format), which processes a multi-value field also, though in my case it is a list of eMail Ids (replace "eMail ID" by "picture" to match your case):

{ "rules_verify_updates_to_selected_email_ids" : {
    "LABEL" : "Verify updates to selected eMail IDs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules_conditional", "rules" ],
    "ON" : { "node_update--article" : { "bundle" : "article" } },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node-unchanged:field-email-addresses" ] },
          "ITEM" : { "previous_email_id" : "Previous eMail ID" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "NOT list_contains" : {
                      "list" : [ "node:field-email-addresses" ],
                      "item" : [ "previous-email-id" ]
                    }
                  },
                  "DO" : [
                    { "drupal_message" : { "message" : "Value [previous-email-id:value] was removed from the multi value field." } }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-email-addresses" ] },
          "ITEM" : { "current_email_id" : "Current eMail ID" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "NOT list_contains" : {
                      "list" : [ "node-unchanged:field-email-addresses" ],
                      "item" : [ "current-email-id" ]
                    }
                  },
                  "DO" : [
                    { "drupal_message" : { "message" : "Value [current-email-id:value] was added to the multi value field." } }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}


If you just replace the machine name "field-email-addresses" by "field-pictures" (prior to import), you should be able to experiment with this rule in your own site to see it at work.

Here is a sample of the messages I get when I update an article, and while doing so I add 1 more eMail ID and at the same time I remove an existing eMail Id:

Value user3@example.com was removed from the multi value field.
Value user2@example.com was added to the multi value field.


So the clue to make this work is:


first iterate over all field values of your field of the unchanged node (to find the removed values).
then also iterate over all field values of your field of the changed node (to find the inserted values).


Obviously, this does note explain how to find the "updated" values. But think about my sample scenario: another way to look at it is that I updated value user3@example.com to user2@example.com. Not sure if it is a complete answer to your question, but admit it is already pretty close, no?

Note: As an alternative to using the Conditional Rules module (as you also seem to be doing already), you can also rework the relevant part of my Rules example into performing equivalent Rules Components. For more details on using Rules Components instead of Conditional Rules, refer to the answer to "What&#39;s the alternative to using the Conditional Rules module?".