You are here

function commons_notify_send_message_on_shutdown in Drupal Commons 7.3

Send a notification for newly created nodes.

This function is registered as a shutdown function in commons_notify_node_insert.

Note that shutdown functions run after drupal_set_message() messages are displayed, so you'll need to use another method such as watchdog() logging if you wish to add debugging messages to this function.

1 string reference to 'commons_notify_send_message_on_shutdown'
commons_notify_node_insert in modules/commons/commons_notify/commons_notify.module
Implements hook_node_insert(). In order to respect node access restrictions, we register a shutdown function, because node access grants haven not been written by the time hook_node_insert() is invoked. For more background, see…

File

modules/commons/commons_notify/commons_notify.module, line 31

Code

function commons_notify_send_message_on_shutdown($node) {

  // Create a message when a user creates a new node.
  // Borrowed from message_example.module:
  // Save the message and assign it to the user realm. Since another user,
  // usually an admin might create the node, but populate the author field
  // with another user, we make sure the user realm is populated with the
  // node's author, instead of the acting user (although in most cases it
  // would be the same user).
  // The following example demonstrates that we don't need to explicitly set
  // the realm to the user, since if no realms are provided then the message
  // is automatically assigned to the user passed in the function, or if no
  // user object is provided, then to the acting user.
  $message_type = 'commons_notify_node_created';

  // We need to clear the static loading cache so that the node body will be
  // ready in time for token replacement for the  message.
  entity_get_controller('node')
    ->resetCache(array(
    $node->nid,
  ));
  if (!isset($node->og_group_ref[LANGUAGE_NONE][0]['target_id'])) {
    $message_type = 'commons_notify_node_created_no_groups';
  }

  // Allow other modules to specify an alternative message type to use.
  $hook = 'node_insert';
  drupal_alter('commons_notify_message_selection', $message_type, $hook, $node);
  $account = user_load($node->uid);
  $message = message_create($message_type, array(
    'uid' => $account->uid,
    'timestamp' => $node->created,
  ));

  // Save reference to the node in the node reference field.
  $wrapper = entity_metadata_wrapper('message', $message);

  // We use a multiple value field in case we wish to use the same
  // field for grouping messages in the future
  // (eg http://drupal.org/node/1757060).
  $wrapper->field_target_nodes[] = $node;
  $wrapper
    ->save();
  $options = array(
    'rendered fields' => array(
      'message_notify_email_subject' => 'field_message_rendered_subject',
      'message_notify_email_body' => 'field_message_rendered_body',
    ),
  );
  message_subscribe_send_message('node', $node, $message, array(
    'email' => $options,
  ));
}