2

I know how to email using Rules when a field in a node is changed and add conditions for particular fields whether they were changed.

But I would like the email to contain the info which field(s) exactly were changed and from/to which value.

E.g., I have a content type with 20 fields. Then I create a node and populate all the fields. Now I want to change two fields in the node (e.g., "First Name" and "Age").

I'd be happy to receive the following email:

The following was changed in "Node name":

"First name" changed from "John" to "George".

"Age" changed from "35" to "76".

0

Some of the ingredients you'll need for a possible solution to make this work:

  • The Conditional Rules module.
  • Rules Actions "Add a variable" and "Set a data value".

Read on for more details ...

Here is a rule, in Rules export format, which is a prototype of what you're trying to get to work:

{ "rules_notify_admin_of_selected_field_updates" : {
    "LABEL" : "Notify admin of selected field updates",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "OR" : [
          { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
          { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_free_format_text" } }
        ]
      }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "Field changes applied to a node:" },
          "PROVIDE" : { "variable_added" : { "field_changes" : "Added variable" } }
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : {
                "data" : [ "node:field-optionlist" ],
                "value" : [ "node-unchanged:field-optionlist" ]
              }
            },
            "DO" : [
              { "data_set" : {
                  "data" : [ "field-changes" ],
                  "value" : "[field-changes:value] - Field Optionlist changed from \u0022[node-unchanged:field-optionlist]\u0022 to \u0022[node:field-optionlist]\u0022"
                }
              }
            ]
          }
        ]
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : {
                "data" : [ "node:field-free-format-text" ],
                "value" : [ "node-unchanged:field-free-format-text" ]
              }
            },
            "DO" : [
              { "data_set" : {
                  "data" : [ "field-changes" ],
                  "value" : "[field-changes:value] - Field Free Format Text changed from \u0022[node-unchanged:field-free-format-text]\u0022 to \u0022[node:field-free-format-text]\u0022"
                }
              }
            ]
          }
        ]
      },
      { "mail" : {
          "to" : [ "site:mail" ],
          "subject" : "Field changes in node with nid=[node:nid]",
          "message" : [ "field-changes" ],
          "language" : [ "" ]
        }
      }
    ]
  }
}

Some more details about the above rule:

  • It showcases updates to (only) 2 fields, i.e.:

    • a selection list field with machine name field_optionlist, and a few possible values.
    • a free format text field with machine name field_free_format_text.
  • The Rules Actions "Add a variable" is to create an appropriate variable.

  • The Conditional Rules module allows for including like "Rules Conditions" within the Rules Actions.
  • Using "Set a data value" we append what's appropriate to that variable.
  • In the end (last Rules Action), we make Rules send an eMail with an extremely basic eMail body: the content of the variable we populated in the previous Rules Actions.

Assuming that you enabled the Conditional Rules module, you should be able to import the rules example above in your own environment (provided you first defined 2 similar fields with identical machine names to make the import work, or after you first edit the field names to match your field names).

Homework:

  • Extend the prototype from 2 to 20 fields (= sames as except ...).
  • You may want to improve the formatting of the eMail body, eg add some relevant HTML tags in it to make it more readable. Maybe also its subject, etc.

Voilà, happy (Conditional) Rules-ing!

PS: Would be nice if you could use a Rules "loop" to iterate over all 20 fields in a single loop, similar to what is shown in the answer to "How to create a rule to send an email to a list of selected email addresses?". However I'm not aware of anything in Rules to do so.

  • Do you really have to add a condition and an action for every field on the node? I am on me phone right now, so I cannot really check, but wouldn't it be possible to create a loop over all fields on the node? – Neograph734 Feb 12 '17 at 19:11
  • @Neograph734 : I've been thinking about the very same thing (= which loop can I use to iterate over all fields). But I'm not aware of any such field. If yoy can think of something, please feel free to clone my rule and post an answer with an improved version, OK? Btw: if it was about "a multi value field", then of course you could do this in a loop to iterate over all its values (I'm sure I ever posted a sample rule about that). – Pierre.Vriens Feb 12 '17 at 19:17
  • You know more about rules than I do ;) – Neograph734 Feb 12 '17 at 19:22
  • @Neograph734 : I found my "multi value field" answer (cfr my PS at the end). And BTW: I don't know a lot about Rules, for sure way less then famous J.F(-with-no-K) from what used to be nodeone.se ... – Pierre.Vriens Feb 12 '17 at 19:41

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.