2

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:

[email protected]|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).
5
  • 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!)? Oct 9, 2015 at 14:16
  • Absolutely, I am here to learn how to do things the right way! Oct 9, 2015 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")? Oct 9, 2015 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! Oct 9, 2015 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 Jun 30, 2016 at 17:29

1 Answer 1

1

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 [email protected]|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).

6
  • "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? Oct 9, 2015 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. Oct 9, 2015 at 18:27
  • Does the "note" i added to "Part 1" help? Oct 9, 2015 at 18:50
  • Yes, just to clarify though, I essentially need to create a view that serves as a user table? Oct 9, 2015 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 Oct 9, 2015 at 19:32

Your Answer

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

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