You are here

function message_example_nodeapi in Message 6

Implementation of hook_nodeapi().

File

modules/message_example/message_example.module, line 24

Code

function message_example_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;
  if ($op == 'insert') {

    // Set the arguments that would be replaced on run-time.
    $arguments = array(
      // The link will be replaced with the url of the node using url() upon
      // display. Even if the node alias changes, then the link will always be
      // displayed correctly.
      '@link' => array(
        'callback' => 'url',
        'callback arguments' => array(
          'node/' . $node->nid,
        ),
      ),
      // The title of the node will be sanitized using check_plain() upon
      // display. We hard code the title to prevent a node load, but if the
      // node's title will change it will not be reflected in the message.
      // see message_example_comment() for an example of using a callback to get
      // the most up-to-date title.
      '@title' => $node->title,
      // Add the teaser argument. The teaser may contain HTML, so we make sure
      // it's not escaped by prefixing the argument with the "!" sign.
      '!teaser' => array(
        'callback' => 'message_example_node_view_teaser',
        'callback arguments' => array(
          $node->nid,
        ),
      ),
    );

    // 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 explicetly 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.
    $account = user_load($node->uid);
    message_save_message_to_realms('message_example_node_' . $op, 'node', $node->nid, $arguments, array(), $account);
  }
}