Assume a flag (created using the Flag module) which triggers a rule, using Rules Event "When something is flagged" (with this flag). And assume that this flag has 2 fields defined for it also, i.e.:

  • "Date from", with machine name field_flag_a_group_date_from.
  • "Date to", with machine name field_flag_a_group_date_to.

When such rule is triggered, assume it performs a Rules Component, which contains a Rules Action to "Send an eMail".

Question: How to pass the values of those Flag fields to that Rules Component, so that they can be used in the message body of that eMail?

up vote 2 down vote accepted

Step 1 - Add parameters to the Rules Component

Make the "Date from" and "Date to" available as extra parameters to the Rules Component. Here is an example of such Rules Component (in Rules Export format, if you have the Rules UI enabled you can import it in your own site).

{ "rules_send_email_to_all_members_of_a_group" : {
    "LABEL" : "Send eMail to all members of a Group",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "TAGS" : [ "group" ],
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "group" : { "label" : "Group", "type" : "group" },
      "flag_from_date" : { "label" : "Flag from date", "type" : "date" },
      "flag_to_date" : { "label" : "Flag to date", "type" : "date" }
    },
    "ACTION SET" : [
      { "entity_query" : {
          "USING" : {
            "type" : "group_membership",
            "property" : "group",
            "value" : [ "group" ]
          },
          "PROVIDE" : { "entity_fetched" : { "group_members" : "Group members" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "group-members" ] },
          "ITEM" : { "member_of_group" : "Member of group" },
          "DO" : [
            { "drupal_message" : { "message" : "Group member named \u0022[member-of-group:user:name]\u0022 and eMail \u0022[member-of-group:user:mail]\u0022 will receive an eMail because of flagging this \u0022[group:group-type]\u0022 group titled \u0022[group:title]\u0022 and id \u0022[group:gid]\u0022 (with Date from = [flag-from-date:value] and Date to = [flag-to-date:value])." } },
            { "mail" : {
                "to" : "[member-of-group:user:mail]",
                "subject" : "eMail notification to group [group:title]",
                "message" : "Hello [member-of-group:user:name],\r\n\r\nyou are a member of the group [group:title] (which is of type [group:group-type]).\r\n\r\nYou are receiving this eMail since user [site:current-user] just flagged this group.\r\n\r\nFYI: the \u0022From date\u0022 (from the flag) is [flag-from-date:value] and the \u0022To date\u0022 (from the flag) is [flag-to-date:value] ...\r\n\r\nGreetings,\r\n\r\nPS: contact the site admin by eMail (via [site:mail]) for more details if needed",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}

The above Rules Component uses these 2 parameters (together with some other parameter "Group"):

  • "Flag from date" (label), with machine name flag_from_date, and data type "Date".
  • "Flag to date" (label), with machine name flag_to_date, and data type "Date".

Note how both parameters are shown within "USES VARIABLES" (that's where they are 'defined'). But also note how within the message body of the eMail both parameters are used (referenced), with any "_" in the machine name replaced by "-".

Step 2 - Make the Flag fields available in the Rule

Before you can use the "tokens" related to the Flag fields in a Rules Action (as in Step 3 below), the Rule needs to be made aware about those Flag fields. To do so, simply add those 2 Rules Conditions in the rule that is triggered by Rules Event "When something is flagged" (with this flag):

  • Entity has field, whereas the Data selector for the entity is "flagging", and the value of the field to check for is field_flag_a_group_date_from.
  • Entity has field, whereas the Data selector for the entity is "flagging", and the value of the field to check for is field_flag_a_group_date_to.

Step 3 - Pass the Flag fields values from the Rule to the Rules Component

After you completed Step 1, Rules "knows" that for any Rules Action that performs the Rules Component, it is required that a value is specified for those 2 parameters that the Rules Component needs (expects).

So if you add a Rules Action like "perform the Rules Component" (from Step 1) to your Rule (not Rules Component!), you will be prompted to specify a Data Selector for each of the 2 parameters (labelled "Flag from date" and "Flag to date"). Here is the trick to complete the rule (puzzle?): if you first completed Step 2 (and only then!), you will be able to:

  • use flagging:field-flag-a-group-date-from as the Data selector for the "Flag from date".
  • use flagging:field-flag-a-group-date-to as the Data selector for the "Flag to date".

Here is an example of such rule, which passes the values of those Flag fields to the Rules Component that is performed in the Rules Action (in Rules Export format):

{ "rules_send_a_group_notification_by_flagging_a_group" : {
    "LABEL" : "Send a group notification by flagging a group",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "flag" ],
    "ON" : { "flag_flagged_flag_a_group" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "flagging" ], "field" : "field_flag_a_group_date_from" } },
      { "entity_has_field" : { "entity" : [ "flagging" ], "field" : "field_flag_a_group_date_to" } }
    ],
    "DO" : [
      { "component_rules_send_email_to_all_members_of_a_group" : {
          "group" : [ "flagged-group" ],
          "flag_from_date" : [ "flagging:field-flag-a-group-date-from" ],
          "flag_to_date" : [ "flagging:field-flag-a-group-date-to" ]
        }
      }
    ]
  }
}

If you have the Rules UI enabled you can import this rule in your own site (after you adapted the machine names of the flag fields to fit yours).

Note the 2 Rules Conditions that were added in the above rule (as detailed in Step 2). And how those fields are then also used within the Rules Action that performs the Rules Component (from Step 1).

  • @Mathieu : Merci for your feedback! Question suivant peut-ëtre? – Pierre.Vriens Nov 3 '16 at 11:26

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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