1

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"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
  • It depends a bit on what you ultimately want to do if a file is changed. Do you need to act on the file, or on the node? Otherwise it might be easier to configure a rule that acts on the creation or update of a new file entity rather than acting on a node update event? – Neograph734 Sep 25 '17 at 14:40
  • Actually I need to gather all file data and send them to an external database (id, url, inline fields values...), so I just need to know if a new file has been added to a node (INSERT) OR if an existing file has been updated (UPDATE). – Toki Sep 25 '17 at 14:42
  • Since no File events exist by default, I applied this patch (drupal.org/files/issues/file_events-826986-22.patch) to Rules but I did not seem to work either... But maybe Show message on site After saving new file is not a good test since the time to see the message is too short (I got no Rules log errors...)... – Toki Sep 25 '17 at 15:59
  • Ok, maybe I got something. The patch mentioned in the previous comment is working. But I have not noticed that the file was saved immediately. So if I add a new image in the field, the rule fires as soon as the image is uploaded and the message appears when I am still on the edition form. – Toki Sep 25 '17 at 16:08
  • You could also try the entity rules module. It works slightly different, but I think that works for every entity type. – Neograph734 Sep 25 '17 at 16:12
1

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:

  1. first iterate over all field values of your field of the unchanged node (to find the removed values).
  2. 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's the alternative to using the Conditional Rules module?".

  • "Merci" Pierre for your answer. I will try this later but do you think it will work with a File field? – Toki Sep 25 '17 at 16:56
  • Actually my first try failed but it was a long day, let me try again tomorrow with a fresh mind. Keep you informed. – Toki Sep 25 '17 at 17:06
  • Yes, I imported your rule and modified it to fit my field. My question is about a multi-value File field and I am afraid it is a bit trickier than a multi-value Text field. Ultimately, I need all file data (fid, url, inline fields values...) so I need to build the right rule to react on new data or updated data for files. That is why I also tried the patch with File events (see comments above). I have tried your scenario on a fresh content type and unfortunately, when I edit a node, adding a new image, messages are wrong : 1 content with 2 images, I get 3 messages (remove, add, add). Any idea? – Toki Sep 26 '17 at 7:15
  • I understand the misunderstanding but contrib modules involved are listed at the top of the question and the title specifically mentions "file". Your answer, even if it works for text field on your side, is not relevant for my concern. Sorry for that ;) – Toki Sep 26 '17 at 13:27
0

Here is my own solution to fire a Rule when adding new file through Image field (related to File entity module).

Actually, the main idea is to patch Rules with File events (patch here) so you may trigger actions on Saving a new file or Updating an existing one (if you have skills on coding Rules events in a custom module, maybe this patch is not necessary). Be aware that the File is saved as an entity as soon as it is uploaded through your content type form.

So, in my case, I also needed to act on inline fields (alternative to field collection or another content type to add captions or copyrights on Image) and I have to create a second rule, based on the "host node" updating (the node using the new file) to fetch the file by ID and then gather all data from File inline fields for my purpose (using tokens).

Besides, be careful about the File bundle to avoid firing the rule for any kind of file (document, audio, etc) if you just need to act when an image is added. So you may have to add a condition on the first rule (Entity is of bundle, here File is of bundle image).

Finally, if you need to fire the Rule only when an image is added for a specific content type, I added a last condition to the first rule (when saving new file) : the file path contains the name of the folder where your image is going to be stored. With this condition, the Rule will fire only if the new image has been stored in the right folder, specifically designed for the Image field of your specific Content type.

Hope this would help someone.

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.