2

I used the Rules module to create a rule with an event "After update existing content". This rule sends an email to the editing user when that user makes an edit to one of the multiple custom fields in a node. I would like to include only the custom field that he/she changed in that email.

1

Part 1: Processing a single field

Below is a sample of a rule (in Rules export format) that will send an eMail similar to what is asked in the question:

{ "rules_notify_author_of_selected_field_updates" : {
    "LABEL" : "Notify author of selected field updates",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "NOT data_is" : {
          "data" : [ "node-unchanged:field-optionlist" ],
          "value" : [ "node:field-optionlist" ]
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[site:current-user:mail]",
          "subject" : "Notification about a node with a special field value",
          "message" : "Go checkout the node with nid [node:nid] and title [node:title]: it has a field with a value (which is [node:field-optionlist]) you needed to be informed about ..."
        }
      }
    ]
  }
}

Some more details to further explain the above above sample:

  • The Rules event I'm using is "After updating existing content".
  • In my case I have a selection list field with machine name field_optionlist, and a few possible values.
  • The Rules Condition checks if the "The selected value changed".
  • If the Rules Condition is true, then (as Rules Action), an eMail will be triggered to the editing user.

Change the field name of field_optionlist to match the field name of your content type, and adapt the actual eMail details (subject, etc) to fit your needs. Then import it in your own site (using the Rules UI). That's it.

Part 2: Processing a multiple fields

Option 1 - a SEPARATE rule for processing each field

If you have multiple fields, create a similar rule for each of them, so that it will send 1 single eMail for each field update.

Option 2 - a SINGLE rule for processing all fields together

If you have like a dozen fields, using Option 1 implies .... a dozen rules (fairly straight forward rules, but a bit of a maintenance challenge). To consolidate them all in a single rule, you should consider (both of) these refinements:

  • a Rules Component (which will replace the Rules Action as in the prior rule example), and which you'll be able to reuse within the Rules Action later on by assigning appropriate values to its variables (as further detailed below).
  • the Conditional Rules module which will allow for conditions to be part of a Rules Action.

Read on for more details on this refinements.

Rules Component

Create a Rules Component that will (simply) produce an eMail, using these Parameters:

  • User to be notified with machine name user_to_notify.
  • Updated Field Label with machine name updated_field_label.
  • Updated Field Value (old) with machine name updated_field_value_old.
  • Updated Field Value (new) with machine name updated_field_value_new.

Here is an example (in Rules export format) of such Rules component (I trust the eMail subject, content, etc is clear from just reading the source ...):

{ "rules_email_field_updates" : {
    "LABEL" : "eMail field updates",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "user_to_notify" : { "label" : "User to be notified", "type" : "user" },
      "updated_field_label" : { "label" : "Updated Field Label", "type" : "text" },
      "updated_field_value_old" : { "label" : "Updated Field Value (old)", "type" : "text" },
      "updated_field_value_new" : { "label" : "Updated Field Value (new)", "type" : "text" }
    },
    "ACTION SET" : [
      { "mail" : {
          "to" : [ "user-to-notify:mail" ],
          "subject" : "Notification about an updated field",
          "message" : "Please be aware that the field labeled \u0022[updated-field-label:value]\u0022 has been updated from \u0022[updated-field-value-old:value]\u0022 to \u0022[updated-field-value-new:value]\u0022.",
          "from" : "[site:mail]",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Conditional Rules

On top of the field_optionlist field from above, let's assume we add a 2nd field, i.e. a free format text field with machine name field_free_format_text. Assuming that you enabled the Conditional Rules module, you should be able to import the rules example below (in Rules export format) 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):

{ "rules_notify_editing_user_of_selected_field_updates" : {
    "LABEL" : "Notify editing user of selected field updates",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "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" : [
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : {
                "data" : [ "node:field-optionlist" ],
                "value" : [ "node-unchanged:field-optionlist" ]
              }
            },
            "DO" : [
              { "component_rules_email_field_updates" : {
                  "user_to_notify" : [ "site:current-user" ],
                  "updated_field_label" : "Option List",
                  "updated_field_value_old" : [ "node-unchanged:field-optionlist" ],
                  "updated_field_value_new" : [ "node:field-optionlist" ]
                }
              }
            ]
          }
        ]
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : {
                "data" : [ "node:field-free-format-text" ],
                "value" : [ "node-unchanged:field-free-format-text" ]
              }
            },
            "DO" : [
              { "component_rules_email_field_updates" : {
                  "user_to_notify" : [ "site:current-user" ],
                  "updated_field_label" : "Free Format Text",
                  "updated_field_value_old" : [ "node-unchanged:field-free-format-text" ],
                  "updated_field_value_new" : [ "node:field-free-format-text" ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

If you're familiar with Rules, the above Rules example should be self explaining. If you're not, here are a few more details to explain the rule:

  • Rules Event: After updating existing content.
  • Rules Conditions: Check if we're processing content that has either of our 2 fields.
  • Rules Actions:
    • If the value of field_optionlist changed, then invoke the Rules Component, with parameters related to this field.
    • If the value of field_free_format_text changed, then invoke the Rules Component, with parameters related to this field.

Voilà, happy (Conditional) Rules-ing!

  • 1
    Thanks, this is a good idea. I thought I'd have to resort to PHP. I have 18 different fields, so I'd obviously like to avoid 18 rules. Could you expand on how I could use Conditional Rules to do this? Is there a way to only have one email action by putting the changed field in a variable? – Tenz Jun 29 '16 at 0:46
  • @Tenz: see my expanded answer (= Part 2 that I added) ... Good enough? – Pierre.Vriens Jun 30 '16 at 17:25
  • this looks fantastic! Thanks a lot for the detailed walkthrough. – Tenz Jul 1 '16 at 12:29
  • @Tenz now that you are able to, you can vote up this answer. – No Sssweat Jul 5 '16 at 9:06

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.