I am attempting to send an email to a field selected value using the Rules module. The user selects a faculty member and saves the node. The rule should fire off and email based on the field's key. I have the field set up as:

john@email.com|Doe, John

I am attempting to send to the key using [node:field-advisor-:key] as a replacement pattern. But I think it's still trying to send to the label.

I am still very new to Drupal so my vocabulary and skills are relatively limited.

Here is the exported rule:

  { "rules_email_to_adviser" : {
  "LABEL" : "Email to Adviser",
  "PLUGIN" : "reaction rule",
  "OWNER" : "rules",
  "REQUIRES" : [ "rules" ],
  "ON" : { "node_insert--timeclock" : { "bundle" : "timeclock" } },
  "IF" : [
        { "user_has_role" : { "account" : [ "node:author" ], "roles" : { 
        "value" : { "20" : "20" } } } },
       { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_advisor_" } }
    ],
  "DO" : [
    { "mail" : {
        "to" : "[node:field-advisor-:key]",
        "subject" : "Time Card Submitted for Review",
        "message" : "[node:author] has submitted a time card for your approval. \r\n\r\nPlease review the time card at: [node:edit-url]\r\n\r\nThank you.",
        "language" : [ "" ]
      }
    }
  ]
  }
}

Notes:

  • All of the faculty members (such as "John Doe") have accounts on the site.
  • If it would help (simplify things) to make the rule work, I'm OK with replacing the implementation of that field_advisor_-field by a more appropriate field (with similar functionality).
  • OK, that's what I was hoping (and assuming, but wanted to be sure). 1 More question (to prepare for a possible solution): would it be an option to replace the implementation of that field_advisor_-field by a more appropriate field (with similar functionality, but which would make your rule work ... for sure!)? – Pierre.Vriens Oct 9 '15 at 14:16
  • Absolutely, I am here to learn how to do things the right way! – Andrew Roseman Oct 9 '15 at 14:22
  • OK, I posted my answer (+ curious for your feedback of course). And removed some of the (now obsolete) comments above (you may want to also remove your obsolete comments above now). Maybe you want to further reformat your question to better integrate the "Notes" I added (which are like copy-paste of some of your comments above)? Feel free to do so, but make sure the extra comments you added are somehow reflected in it, OK? PS: maybe you also want to refine your question title a bit (something about "reference" instead of "key")? – Pierre.Vriens Oct 9 '15 at 15:41
  • I'll let you know as soon as I implement the changes. I have one more class for the day and then I'll make the changes and let you know how goes. Thank you so much for all the help! – Andrew Roseman Oct 9 '15 at 16:09
  • Any more news/feedback about my answer? Did you get it to work? Maybe you want to consider marking my answer as "accepted", similar to what is written in drupal.stackexchange.com/help/someone-answers – Pierre.Vriens Jun 30 '16 at 17:29

Part 1: Review the definition of your field

Assuming that all of the faculty members have an account on your site (as you confirmed in one of your comments), I recommend to replace the implementation of that field_advisor_-field. I.e. delete that field in your timeclock content type, and add a more appropriate field that looks like so:

  • Field Name = Anything you want, say with a machine name like field_timeclock_reviewer
  • Field Type = Entity Reference, with Target type = user and Number of values = 1 (pick any of the other options to define this field as you see fit).

By using this field, you don't have to maintain the data of that field as in your example with john@email.com|Doe, John (instead you take advantage of the already administered userids and their corresponding eMail IDs).

Note: to limit the list of users that can be referenced to only those users with a specific role (like "faculty"), you should use Views "Entity selection mode" for your entity reference field in the Entity Reference widget. But before you can select the appropriate "View" to do so, you should create a view (with the Views module) to create a list of all users with that specific role.

Part 2: Rule to send the eMail

After you adapted the setup of your field as described above, it is straight forward to write a rule for the Rules module to send such eMail that you're looking for.

Here is a sample of such rule (in Rules export format), which will send such eMail to the userid selected via the field field_blog_reviewer (= a field I added to my "Blog" content type that is defined as an entity reference to a 'user'), and this whenever a new node of content type "Blog" is saved:

{ "rules_notify_reviewer_about_outstanding_approval" : {
    "LABEL" : "Notify reviewer about outstanding approval",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--blog" : { "bundle" : "blog" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_blog_reviewer" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[node:field-blog-reviewer]",
          "subject" : "Blog Post waiting your approval",
          "message" : "[node:author] has submitted a new Blog post waiting your approval.\r\n\r\nYou can review it at: [node:edit-url].",
          "language" : [ "" ]
        }
      }
    ]
  }
}

If in the above exported rule you'd change "blog" to "timeclock" (before importing this rule in your environment), and after you also add that extra user_has_role condition (as in your question), you should be pretty close to getting it to work (with an eMail that is similar to what you wanted it to be).

  • "Limit the list of users": do you mean students for which the rules action is to be triggered, or users who can be referenced (as reviewers)? Can you edit your question to clarify that? Or maybe reusing that user_has_role condition is sufficient? "Display names (not emails): that is what you should be able to configure as part of all those options when defining that field. For sure it will not display (disclose) the "email id" when such nodes are created. But Rules is smart enough to know that in the Rules action the "to" actually corresponds to the eMail id of the reviewer. Does this help? – Pierre.Vriens Oct 9 '15 at 18:14
  • I chose to make the field an entity reference with a select list widget. I need the select-able options to only be users with the role of faculty. I hope that clarifies my question. – Andrew Roseman Oct 9 '15 at 18:27
  • Does the "note" i added to "Part 1" help? – Pierre.Vriens Oct 9 '15 at 18:50
  • Yes, just to clarify though, I essentially need to create a view that serves as a user table? – Andrew Roseman Oct 9 '15 at 18:54
  • Yes, which means you have all the Views facilities available to make that list fit your exact requirements, like selection (filter), sorting, etc – Pierre.Vriens Oct 9 '15 at 19:32

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.