1

I use the Group module. I create a flag to mark groups. I wish that when a group is marked, an email is sent to all members of that group. How to do this ?

I created the following rule, but I find nothing in "conditions" in relation to the groups :

{ "rules_email_groupe" : {
    "LABEL" : "Email groupe",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "flag" ],
    "ON" : { "flag_flagged_achats_groupes" : [] },
    "DO" : [
      { "mail" : {
          "to" : "[email protected]",
          "subject" : "Invitation \u00e0 la vente group\u00e9e de [flagged-group:title]",
          "message" : "Bonjour,\r\n\r\n\r\nle groupe [flagged-group:title], vous invite \u00e0 sa vente group\u00e9e.\r\nElle commencera [flagging:field-flag-date:value] et se terminera [flagging:field-flag-date:value2].\r\n\r\n\r\nCordialement,\r\nL\u0027\u00e9quipe [site:name]\r\n[site:url]\r\n\r\nPour rester inform\u00e9 des nouveaut\u00e9s et b\u00e9n\u00e9ficier de nos offres promotionnelles, suivez-nous sur :\r\nhttps:\/\/www.facebook.com\/S1biosecom\r\nhttps:\/\/twitter.com\/S1biosecom\r\nhttps:\/\/plus.google.com\/+S1biosecom\r\nhttps:\/\/www.pinterest.com\/S1biosecom\r\n\r\nPS : ceci est un mail automatique, merci de ne pas y r\u00e9pondre.",
          "from" : "[site:mail]",
          "language" : [ "flagging-user:language" ]
        }
      }
    ]
  }
}

Any suggestions?

Some more details about my setup:

  • An overview of the Flags I've defined:

    enter image description here

  • More info about some of my fields (I also use Display Suite):

    enter image description here

  • The fields I defined for my Group Type "Entreprise":

    enter image description here

Update: By using the Rules Component included in the answer to "How to pass a Flag field's value to a Rules Component triggered by the flag?" I'm getting closer, but I still have to find out what to do with those 2 date variables I've to pass to that Rules Component, as shown here:

enter image description here

2
  • Please QA-review my edited of your question (I only tried to explain the screenprints you added). Feel free to rework if needed, or simply perform a rollback to the prior version if you don't like my edits at all, ok? Nov 3, 2016 at 11:25
  • 1
    @Pierre.Vriens Thank you this is exactly the question
    – user54067
    Nov 3, 2016 at 20:03

1 Answer 1

0

Looking at the rule you included, it seems that your Rules Event is fine. But obviously your Rules Action (to send an eMail) is to only send it to 1 (hardcoded) eMail ID. Read on for more details about how you can get this to work ...

Step 1:

Create a flag (similar to yours) to flag a group. In my case I named it (surprise ...) "Flag a group", and with machine name "flag_a_group" (just replace in what follows this machine name of that flag with your own flag's machine name).

Step 2:

Create a Rules Component "Send eMail to all members of a Group", which you can do like so:

  • The type of this component is an "Action Set" (no need for any conditions).
  • Action 1 is to build a list of all the members of the flagged group. It uses "Fetch entity by property", with these additional specs:
    • Entity type value = Group Membership.
    • Property value = Group.
    • Value of the data selector = Group.
    • limit result count = blank (we do not want to limit the nr of results).
    • Provided variables = "Group members" (variable label) and "group_members" (variable name).
  • Action 2 is to iterate (= loop) over each of the members of the flagged group. It uses "Add a (rules) loop", with these additional specs:
    • The list to loop over = group-members
    • Current list item = "Member of group" (variable label) and "member_of_group" (variable name).
    • Sub-action (i) of this Action 2 is to just "Show a message on the site" (while QA-testing this Rules Component), with message type = Status and with a message something like "Group member named "[member-of-group:user:name]" and eMail "[member-of-group:user:mail]" will receive an eMail because of flagging this "[group:group-type]" group titled "[group:title]" and id "[group:gid]" (note the various tokens which might be interesting).
    • Sub-action (ii) of this Action 2 is to actually send an eMail to the "member_of_group" being processed in the loop, using various tokens similar to those in Sub-action (i).

Here is how the Rules Component looks like (in Rules export format):

{ "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" } },
    "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" } },
            { "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\nGreetings,\r\n\r\nPS: contact the site admin by eMail (via [site:mail]) for more details if needed",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}

If you have the Rules UI enabled you can simply import the above rule in your own site, no need to tweak anything in it, though you may want to tune the Subject and Message content of the actual eMail as you want (just remember the various tokens you may want to use).

Step 3:

Create a rule "Send a group notification by flagging a group", which you can do like so:

  • Rules Event: When a group is flagged with the flag from Step 1.
  • Rules Conditions: none (no need for it).
  • Rules Action: perform the Rules Component from Step 2.

Here is how the rule looks like (in Rules export format, disabled as shown here):

{ "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" : [] },
    "DO" : [
      { "component_rules_send_email_to_all_members_of_a_group" : { "group" : [ "flagged-group" ] } }
    ]
  }
}

If you have the Rules UI enabled you can simply import the above rule in your own site, after you adapt the flag's machine name to fit yours. Don't forget to enable the rule also.

Step 4:

Add any additional parameters to the Rules Component to pass any relevant extra data from the rule (from Step 3) to the Rules Component (from Step 2). E.g. that date field you added to the flag to be included in the email also (as in your question, like "Elle commencera [flagging:field-flag-date:value] et se terminera [flagging:field-flag-date:value2].".

For more details about this, refer to the answer to "How to pass a Flag field's value to a Rules Component triggered by the flag?". Some more details about how to use that answer in the context of this question also:

  • looking at the message of the eMail in your rule here, I see 2 tokens that look like dates: [flagging:field-flag-date:value] and [flagging:field-flag-date:value2]. But your screenprints do not show how this "field-flag-date" is defined.
  • My best guess is that you have 1 field with 2 values, no?. If so, you only need 1 Rules Condition to check "Entity has field", which I think will be "flagging:field_flag_date". And in the Rules Action you should then be able to specify (drill down) to both [flagging:field-flag-date:value] and [flagging:field-flag-date:value2].

Voilà

So here you go: only 3 (robust) modules (Flag, Rules and Group) is what's needed to be able to trigger an eMail of all members of a Group that is flagged by an authorized user (with zero custom code).

True, the actual eMail is hardcoded in the Rules Component (would be nice to transform that into something with 2 extra Flag "fields", to specify during the flagging what the actual Subject and message content of the eMail should be).

VBO alternative

Another solution could be to use a combination of Rules and VBO. That would have the advantage that you get all the power of Views available to filter which group members you want to be eMailed ...

6
  • Thank you, it works. However it is possible to retrieve the date field that I added to the flag to include in the email. As my question ? "Elle commencera [flagging:field-flag-date:value] et se terminera [flagging:field-flag-date:value2]."
    – user54067
    Nov 1, 2016 at 11:14
  • @Mathieu : merci for the accept. Check my edited answer (Step 4 that I added) to also address your previous comment ("solution to the problem, changes the problem", right?). Disclosure: I haven't QA-tested that, but as per standard Rules facilities that MUST work. Nov 1, 2016 at 11:53
  • Thank you, but I do not understand what I have to add in step 2. I know the rules, but I find the components.
    – user54067
    Nov 2, 2016 at 7:53
  • @Mathieu : your prior comment is a bit hard to understand (is it incomplete?). Also, how about posting that as a new question, with a link (reference) to this answer if you want? And a question that is something like "How to pass additional parameters to a Rules Component from a Rule that invokes it" (+ in the question details you explain that you want to apss some flagging-data from the Rules Event). What do you think? Nov 2, 2016 at 8:11
  • I do not control the components and I do not understand how to add "Elle commencera [flagging:field-flag-date:value] et se terminera [flagging:field-flag-date:value2]."
    – user54067
    Nov 2, 2016 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.