2

I want to send an email to a user when the user's earlier unpublished content is published by somebody else (site administrator, moderator, etc).

I tried doing it with Rules but could not figure out how.

Any help?

4 Answers 4

1

Programmatically it is easy to do:

On a custom module create a custom table: entity_notifications

https://api.drupal.org/api/drupal/includes%21database%21schema.inc/group/schemaapi/7

Entity_id: Integer Entity_type: varchar | SENT: Boolean 

/**
 * Implements hook_schema().
 */
function custommodule_schema() {
  $schema = array();
  $schema['entity_notifications'] = array(
    'description' => 'The base table for entity notifications.',
    'fields' => array(
      'entity_id' => array(
        'description' => 'The primary identifier for the entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'description' => 'Bundle',
        'type' => 'varchar',
      ),
      'sent' => array(
        'description' => 'Boolean indicating whether a email has been sent.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
    )
    'primary key' => array('entity_id', 'entity_type'),
  );
  return $schema;
}

Control node status on hook_node_save or presave:

https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7

function custommodule_node_save($node){
  // @TODO: compare $node->status vs $node->original->status (Check first than $node->original exists

  // @TODO: if current status is == "1" and original "0" check that in the DB is not checked

  // @TODO: send email and write to DB that this has been sent (db_query)! 

https://stackoverflow.com/questions/11986683/how-to-send-email-in-drupal-7 }


Another approach could be using module flag: https://drupal.org/project/flag

  1. Create a flag (type global: not by user) for example "sent"
  2. On your rules condition check that this node has not been flagged with "sent" flag.
  3. On rules actions flag this content after the email action
1
  • Liked the approach of using flag module. Decided to use that. Thanks. Commented Mar 26, 2014 at 15:59
3

The Workflow module can do it exactly what you want. By using this module you can trigger different action at each flow of content.

Workflows are made up of workflow states. Transitions between states can be allowed per role. For example, a workflow with the states Draft, Review, and Published could be assigned to the Story node type. Only users with role 'chief editor' can set Stories to the published state. You can set up the Workflow to alter states from form, page, comment and a special workflow tab.

2

Use two Data Comparison conditions.

One should compare node:unchanged with a status of unpublished. The other should compare node:updated wit ha status of published.

Then fire your action.

5
  • I tried it. But that does not serve my purpose. I want to add a condition that the node should not have been published before. I don't get get such a condition in rules. Commented Mar 26, 2014 at 8:26
  • I think you are making it more complicated than it is. That kind of condition is not necessary. The node has already been saved unpublished. Therefore when it is updated and the status now unpublished, you can have the email sent.
    – filmoreha
    Commented Mar 26, 2014 at 8:33
  • 1
    If you really want to do that, I think there is a condition that compares old value to new value that you can try.
    – filmoreha
    Commented Mar 26, 2014 at 8:36
  • Can you elaborate a bit more? The settings you suggested send an email everytime a published article is saved. I want it to send email only if a previously unpublished article is published. I think you got my point. Commented Mar 26, 2014 at 8:59
  • Alright, I have rewritten my answer.
    – filmoreha
    Commented Mar 26, 2014 at 9:09
2

The prior answer from filmoreha is close, but a bit incomplete. It does not take into account the "by somebody else" part of your question, and does not mention any Rules Event(s) or Rules Action(s).

Here is a rule (in Rules export format) that can be used to send an email to the node author after the node is published:

{ "rules_send_email_after_node_is_published" : {
    "LABEL" : "Send email after node is published",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "node-unchanged:status" ], "value" : "0" } },
      { "data_is" : { "data" : [ "node:status" ], "value" : "1" } },
      { "NOT data_is" : {
          "data" : [ "node-unchanged:author:uid" ],
          "value" : [ "site:current-user:uid" ]
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "node-unchanged:author:mail" ],
          "subject" : "Your content is published ...",
          "message" : "This is to inform you that your node (with node id = [node-unchanged:nid] and node title = [node-unchanged:title]) has been plublished ...",
          "language" : [ "" ]
        }
      }
    ]
  }
}

If you have the Rules UI enabled, you should be able to import the above rule in your own site.

Some more details about this rule:

  • Rules Event: After updating existing content
  • Rules Conditions (all of them must be TRUE):
    • Before the update, the node must be Unpublished.
    • After the update, the node must be Published.
    • The user performing the update must not be the author of the node (as per the "... by somebody else ..." part of your question).
  • Rules Action: Send an appropriate eMail (adapt its content to fit your needs).

Obviously, you could add other Rules Conditions (eg to check the role of the user performing the update), but at its core this rule will get the job done.

2
  • Thank you, this seems perfect. However, I have managed to get this done using flag module. But this seems to be a more elegant solution which fits my needs hence I will try to implement this. Commented Aug 23, 2017 at 6:12
  • @RanaPrathap merci for the feedback, and +1! Commented Aug 23, 2017 at 6:14

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.