Once a scheduled unpublish occurs on a content item via the Schedule module, I would like to send out an email to the content owner to let them know that their content has been unpublished (or is about to be unpublished). Any ideas on how to accomplish this? I would expect I might be able to do this via Rules, but if someone could lend some guidance, that would be great.

Comments

jonathan1055’s picture

Hi Joshua,

If you have your own custom module in which you can implement hooks, then you can do it using hook_scheduler_api() and hook_mail().
hook_scheduler_api() accepts two parameters - $node is the node object being processed, and $action is either 'publish' or 'unpublish'

/**
 * Implementation of hook_scheduler_api.
 *
 * @param $node
 *  The node object
 * @param $action
 *  The action being performed, either "publish" or "unpublish"
 */
function yourmodule_scheduler_api($node, $action) {
  if (some condition) {
    // parameters: drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
    drupal_mail('yourmodule', 'mail key', $user->mail, etc depending on your needs);
  }
}

drupal_mail() calls the specified hook_mail() function given by the first parameter.
hook_mail() defines values for the subject, body, etc

/**
 * Implementation of hook_mail().
 *
 * Constructs an email.
 *
 * @param $key
 *   Unique key to indicate what message to build.
 * @param $message
 *   Reference to the message array being built.
 * @param $params
 *   Array of parameters to indicate what kind of text to include in the
 *   message body.
 *
 */
function yourmodule_mail($key, &$message, $params) {
  switch ($key) {
    case 'this-mail-key':
      $message['subject'] .= 'your subject';
      $message['body'][] = 'text';
      // etc ...       
      break;
    // etc ...       
}

Hope that gives you some ideas. I've not tested the above php, although I have copied this from various different bits of working code. If you want to try it, let me know and I'm happy to help some more.

Jonathan

joshua.boltz’s picture

Thanks. After more investigating, I may be able to figure out a solution using Rules, but if that fails, I will definitely give this option a try.

jonathan1055’s picture

Status: Active » Fixed

OK that's fine.
For the record, and my own interest, I checked out my idea above, and here is a working version, in case anyone else needs this:

/**
 * Implements hook_scheduler_api.
 *
 * @param $node
 *  The node object
 * @param $action
 *  The action being performed, either 'pre_publish', 'publish', 'pre_unpublish' or 'unpublish'
 */
function d7testing_scheduler_api($node, $action) {
  if (substr($action, 0, 3) != 'pre') {
    $user = user_load($node->uid);
    $params = array('title' => $node->title, 'type' => $node->type, 'action' => $action,
                    'name' => $user->name, 'mail' => $user->mail);
    // parameters: drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
    drupal_mail('d7testing', 'scheduler_notification', $user->mail, NULL, $params);
  }
}


/**
 * Implements hook_mail().
 *
 * Constructs an email.
 *
 * @param $key
 *   Unique key to indicate what message to build.
 * @param $message
 *   Reference to the message array being built.
 * @param $params
 *   Array of parameters to indicate what kind of text to include in the
 *   message body.
 */
function d7testing_mail($key, &$message, $params) {
  switch ($key) {
    case 'scheduler_notification':
      $message['subject'] = 'Drupal ' . $params['type'] . ' "' . $params['title'] . '"';
      $message['body'][] = 'Dear ' . $params['name']; 
      $message['body'][] = 'Your ' . $params['type'] . ' "' . $params['title'] . '" has just been '
                           . ($params['action'] == 'publish' ? 'published' : 'unpublished')
                           . ' by the scheduler module.'; 
      $message['body'][] = 'Site: ' . variable_get('site_name'); 
      $message['body'][] = 'Date: ' . date(variable_get('date_format_long')); 
      $message['body'][] = $message['from']; 
      break;
  }       
}

Jonathan

jonathan1055’s picture

Hi Joshua,
I have now started on the Rules Integration for Scheduler, and provided two default reaction rules, which do exactly this.
See #773510: Integration with Rules module

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jonathan1055’s picture

Issue summary: View changes
Related issues: +#773510: Integration with Rules module