1

enter image description here

Actually this is my content and I have one select list field in it. If I select a "New litter" and click on save, I have to generate an email to the author. If however I select any other value for that field (and click on save), then there is no need to generate an email.

How can I do this?

1

You can use hook_node_presave() and in this function check if the select value is "new litter" and generate email.

function YOUR_MODULE_NAME_node_presave($node){
    if($node->type == 'YOUR_CONTENT_TYPE'){
        //get your field value
        $field = field_get_items('node', $node, 'YOUR_FIELD_MACHINE_NAME')[0]['value'];
        //check if value equals "new litter" key
        if($field == 'NEW_LITTER_KEY'){
            generate_email(); 
        }
    }
}

change uppercase strings with your module and machine names

| improve this answer | |
  • @geg-Thanks for your response ,can you explain briefly step by step i am new to drupal – DINE Sep 8 '15 at 11:29
1

Hope below code will help you.

function mycustom_node_insert($node) {
  switch ($node->type) {
    case 'cage-card-info':      
      $warning = $node->field_warnings['und'][0]['value']
    if($warning == "new litter"){

      $user = user_load($node->uid);
       $authr_email = $user->email;
      //Send mail function to the author 
     }
     break;
  }
}
| improve this answer | |
  • Updated my answer based on your comment – Bipin K Sep 8 '15 at 12:10
  • Bipin k -Thanks for your response can i import this code? In rules by using import rule... – DINE Sep 9 '15 at 9:57
  • This is the custom module code.. Not sure about rules Import whether same code will work or not. – Bipin K Sep 9 '15 at 10:07
0

Below is a sample of a rule (in Rules export format) that will send an eMail similar to what is asked in the question:

{ "rules_notify_author_of_selected_field_updates" : {
    "LABEL" : "Notify author of selected field updates",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [], "node_insert" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "data_is" : { "data" : [ "node:field-optionlist" ], "value" : "C" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[node:author:mail]",
          "subject" : "Notification about a node with a special field value",
          "message" : "Go checkout the node with nid [node:nid] and title [node:title]: it has a field with a value you needed to be informed about ..."
        }
      }
    ]
  }
}

Some more details to further explain the above above sample:

  • The Rules event I'm using is "After saving new content" or "Updating existing content.
  • In my case I have a selection list field with machine name field_optionlist.
  • This field actually has 3 possible values, like so:
    • A (=Advanced).
    • B (=Basic).
    • C (=Custom).
  • As a sample, I decided to use as condition "The selected value must be Custom".
  • If that value is selected, then (as Rules action), an eMail will be triggered to the node author.

Change the field name of field_optionlist to match the field name of your content type, and adapt the actual eMail details (subject, etc) to fit your needs. Then import it in your own site (using the rules UI). That's it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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