1

I tried to concisely explain what I am looking to accomplish in the subject, but will go into more details here. I think I'm really close but my Google-fu is failing me.

My question: How can I use the role reference field coming through field_responsible_team in a dynamic fashion opposed to the static fashion I've solved for currently?

Current Setup:

Content Type: Question

-- Taxonomy: Category (attached to Question content type)

---- Taxonomy Field: Responsible Team (user role) / (attached to Category taxonomy)

Example Question node:

Node Type: Question

Category: Training

Responsible Team: Learning & Development

    { "rules_XYZ" : {
    "LABEL" : "XYZ",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--question" : { "bundle" : "question" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_category" } },
      { "AND" : [] },
      { "entity_has_field" : {
          "entity" : [ "node:field-category" ],
          "field" : "field_responsible_team"
        }
      }
    ],
    "DO" : [
      { "mail_to_users_of_role" : {
          "roles" : { "value" : { "4" : "4" } },
          "subject" : "XYZ [node:workbench-access-sections]",
          "message" : "XYZ"
        }
      }
    ]
  }
}

I got excited when this worked, but realized I had limited this to just users of role 4.

Preferred solution: Dynamically pull in the role from the new node created and use that for the role selection in the "mail_to_users_of_role" function. I would've thought I could've used the "node:field-category:field-responsible-team" data selector, but it's telling me that "The data type of the configured argument does not match the parameter's roles requirement." I'm using the Role Reference module.

Secondary solution (solved - see below): Create one rule for each of the roles and specify the categories within the rule set (not ideal).

{ "rules_XYZ" : {
"LABEL" : "XYZ",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "node_insert--question" : { "bundle" : "question" } },
"IF" : [
  { "entity_has_field" : {
      "entity" : [ "node:field-category" ],
      "field" : "field_responsible_team"
    }
  },
  { "AND" : [] },
  { "data_is" : {
      "data" : [ "node:field-category:field-responsible-team" ],
      "value" : "4"
    }
  }
],
"DO" : [
  { "mail_to_users_of_role" : {
      "roles" : { "value" : { "4" : "4" } },
      "subject" : "XYZ",
      "message" : "XYZ"
    }
   }
  ]
 }
}
2
  • I reopened this, and think I understand it, but this it could use a little more editing to make it useful for future readers.
    – mpdonadio
    Commented Mar 17, 2018 at 18:55
  • @mpdonadio merci for reopen! Glad to see I am not the only one anymore who understands this question. BTW, Have you seen the comment from OPer from last Wed at 10:45 below my answer (now moved to chat)? It confirms my answer did work. So maybe first read my answer to get more details about the question? PS: the question title is a perfect resume about the question I think. Commented Mar 18, 2018 at 7:09

1 Answer 1

1

Preface: there is no need for the "AND" in your Rules Conditions (that's the default). Moreover, that "AND" applies to all Rules Conditions following the "AND" (but you only have 1 Rules Condition that follows it). Therefor, start by simplifying your Rules to get rid of the "AND". Refer to my answer to "How to make an OR statement in rules?" for more details about all this.

What you could do for sure is to rework your 2nd Rule (for role "4"), into a rule that performs a (new) Rules Component. To start, at least perform these changes:

  • Create a Rules Component which uses a parameter named (say) "Selected term". Use a data type of "Taxonomy term".
  • In your original Rule:
    • Replace the Rules Action (to send an eMail) by a Rules Action to just execute the (new) Rules Component. While doing so, pass node:field-category as the value of "Selected term".
    • Remove the last Rules Condition (with the hardcoded role "4").

The above changes will be a good start, but still it won't be sufficient. The remaining challenge (in the rules Component) will be related to the "Data selector" you need to specify for the Rules Action "Send mail to all users of a role" (= the replacement for your hardcoded "4"). That challenge is because the rules Component parameter "Taxonomy term" is a single value, while for the roles to be emailed you need a list (repeat: list!), as indicated also below the roles label, where it says "Select the roles whose users should receive the mail." (note: "roles", not "role"!).

Too bad, if we don't have a list, we'll massage the single value (Taxonomy term) into a "list". What follows is an example of a Rule and related Rules Component, with which I could make it work in my own setup:

Main rule

Here is my sample rule (in which I'm using 2 Rules Events, just to simplify testing and so that the logic is also triggered when updating content):

{ "rules_notify_users_of_selected_role_about_new_content" : {
    "LABEL" : "Notify users of selected role about new content",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert" : [], "node_update" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "article" : "article" } } } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_demo_tags" } }
    ],
    "DO" : [
      { "component_rules_do_something_with_selected_term" : { "selected_term" : [ "node:field-demo-tags" ] } }
    ]
  }
}

Note that I'm using a field like field-demo-tags (instead of field_category as in your case). I'm NOT using the Role Reference module, no need for it. The Rules Action is to only perform a Rules Component, in which the real magic will happen (see below).

Rules component

Here is my sample Rules Component:

{ "rules_do_something_with_selected_term" : {
    "LABEL" : "Do something with selected term",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "selected_term" : { "label" : "Selected term", "type" : "taxonomy_term" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "selected-term" ], "field" : "field_related_role" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Field \u0022field_related_role\u0022  (for Related role ID) is [selected-term:field-related-role]." } },
      { "variable_add" : {
          "USING" : { "type" : "list\u003Cinteger\u003E", "value" : [ "" ] },
          "PROVIDE" : { "variable_added" : { "list_of_roles" : "List of roles" } }
        }
      },
      { "list_add" : {
          "list" : [ "list-of-roles" ],
          "item" : [ "selected-term:field-related-role" ],
          "unique" : "1"
        }
      },
      { "mail_to_users_of_role" : {
          "roles" : [ "list-of-roles" ],
          "subject" : "Some eMail subject (delivered to all users with role id = [selected-term:field-related-role])",
          "message" : "Some message content related to the term named [selected-term:name] (delivered to all users with role id = [selected-term:field-related-role])"
        }
      }
    ]
  }
}

The field field_related_role is an extra field I added to my taxonomy term I'm using (single value, data type integer), instead of field_responsible_team as in your case.

Some more details about this Rules Component:

  • Rules Conditions:
    • Check the type of node (in my case "article", adapt to fit your needs).
    • Check that the entity (node being created or updated) has a field field_demo_tags (the term I'm using).
  • Rules Action:
    • Show a message on the site (just for QA-testing purposes, omit if not needed anymore).
    • Add a variable, i.e. a "list of integers", which will become the "List of roles" we'll need in a subsequent Rules Action.
    • Add to this list, the value of selected-term:field-related-role (part of the Rules Component parameter).
    • Send mail to all users of a role, whereas we use the newly created "List of roles" to get it all to work (adapt eMail subject, message, etc to fit your needs).

The above works like a charm, at least in my case/setup: I have 3 users with a role related to a taxonomy term I used while updating an article, and each of those 3 users did receive an eMail as in my last Rules Action.

Voilà ...

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Commented Mar 13, 2018 at 13:44

Your Answer

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

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