message_example.module in Message 6
Same filename and directory in other branches
File
modules/message_example/message_example.moduleView source
<?php
// $Id: message_example.module,v 1.3 2010/06/07 10:54:11 amitaibu Exp $
/**
* @file
* Message module example.
*/
include_once 'message_example.features.inc';
/**
* The name of the flag being used to follow users.
*/
define('MESSAGE_EXAMPLE_FLAG_USER', 'message_example_flag_user');
/**
* The name of the flag being used to follow content (nodes).
*/
define('MESSAGE_EXAMPLE_FLAG_NODE', 'message_example_flag_node');
/**
* Implementation of hook_nodeapi().
*/
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);
}
}
/**
* Implementation of hook_comment().
*/
function message_example_comment(&$a1, $op) {
if ($op == 'insert') {
// The comment ID.
$cid = $a1['cid'];
// The node ID that has this comment.
$nid = $a1['nid'];
// The user is the comment author.
$account = user_load($a1['uid']);
$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/' . $nid,
array(
'fragment' => 'comment-' . $cid,
),
),
),
// Hard code the comment's subject.
'@title' => $a1['subject'],
// Custom callback to return the most up-to-date node title.
'@node-title' => array(
'callback' => 'message_example_node_title',
'callback arguments' => array(
$nid,
),
),
);
// Save the message and assign it to the user realm and node realm. The node
// will allow us to see activity related to a single node.
// As in the hook_nodeapi() implementation, we assign it to the comment
// author instead of the acting user.
$realms = array();
$realms[] = array(
'realm' => 'user',
'realm id' => $account->uid,
);
$realms[] = array(
'realm' => 'node',
'realm id' => $nid,
);
message_save_message_to_realms('message_example_comment_insert', 'comment', $cid, $arguments, $realms, $account);
}
}
/**
* Implementation of hook_flag().
*
* Add a message about users/ content being followed.
*/
function message_example_flag($op, $flag, $content_id, $account, $fcid) {
if ($op == 'flag') {
$arguments = array();
if ($flag->name == MESSAGE_EXAMPLE_FLAG_USER) {
// User is following another user.
$friend_account = user_load($content_id);
// We don't need to save the whole user object, as we only need the partial
// object to be used on display with theme_username().
$dummy_account = new stdClass();
$dummy_account->uid = $friend_account->uid;
$dummy_account->name = $friend_account->name;
$arguments = array(
'!friend' => array(
'callback' => 'message_example_theme_username',
'callback arguments' => array(
'account' => $dummy_account,
),
),
);
}
elseif ($flag->name == MESSAGE_EXAMPLE_FLAG_NODE) {
// User is following a node.
$node = node_load($content_id);
$arguments = array(
'@link' => array(
'callback' => 'url',
'callback arguments' => array(
'node/' . $node->nid,
),
),
'@title' => $node->title,
);
}
if ($arguments) {
// Save the message to the user realm.
message_save_message_to_realms($flag->name, $flag->content_type, $content_id, $arguments);
}
}
}
/**
* Return the teaser of a node.
*
* @param $nid
* The node ID.
*/
function message_example_node_view_teaser($nid) {
$output = '';
if ($node = node_load($nid)) {
$output = '<div class="message-node-teaser">' . node_view($node, TRUE) . '</div>';
}
return $output;
}
/**
* Return a node title.
*
* The node title is not sanitized by check_plain() as this be taken care of by
* the message module.
*
* @param $nid
* The node ID.
*/
function message_example_node_title($nid) {
$output = '';
if ($node = node_load($nid)) {
$output = $node->title;
}
return $output;
}
/**
* Helper function to theme a username.
*
* @param $account
* A user object (or partial object that contains the user ID and user name)
* that will be used along with theme_username().
*/
function message_example_theme_username($account) {
return theme('username', $account);
}
/**
* Implementation of hook_theme().
*
* Theme override a the user's name field in the "message_example" view. It is
* not related specifically to the message module, but rather a demonstration of
* how a view may be themed via a module to provide a nicer looking news feed.
*/
function message_example_theme() {
return array(
'views_view_field__message_example__name' => array(
'arguments' => array(
'view' => FALSE,
'field' => FALSE,
'row' => FALSE,
),
'original hook' => 'views_view_field',
),
);
}
/**
* Theme the user name, and add an icon before it, based on the message name.
*/
function theme_views_view_field__message_example__name($view, $field, $row) {
// Add out custom CSS to the view.
$css =& ctools_static(__FUNCTION__, FALSE);
if (!$css) {
// Add our css.
drupal_add_css(drupal_get_path('module', 'message_example') . '/message_example.css');
$css = TRUE;
}
// TODO: Isn't there a shorter way?
$output = $view->field[$field->options['id']]
->advanced_render($row);
if (!empty($view->field['name_1']->field_alias) && ($message_name = $row->{$view->field['name_1']->field_alias})) {
$image = '';
// Check if the message name has a matching icon in the images folder.
switch ($message_name) {
case 'message_example_node_insert':
case 'message_example_comment_insert':
case 'message_example_flag_user':
case 'message_example_flag_node':
// Remove the module name prefix.
$message_name = str_replace('message_example_', '', $message_name);
// Replace underscore with hyphens.
$image = str_replace('_', '-', $message_name);
break;
}
if ($image) {
// Add the image.
$image = theme('image', drupal_get_path('module', 'message_example') . '/images/' . $image . '.png');
$output = $image . ' ' . $output;
}
}
return $output;
}
Functions
Name | Description |
---|---|
message_example_comment | Implementation of hook_comment(). |
message_example_flag | Implementation of hook_flag(). |
message_example_nodeapi | Implementation of hook_nodeapi(). |
message_example_node_title | Return a node title. |
message_example_node_view_teaser | Return the teaser of a node. |
message_example_theme | Implementation of hook_theme(). |
message_example_theme_username | Helper function to theme a username. |
theme_views_view_field__message_example__name | Theme the user name, and add an icon before it, based on the message name. |
Constants
Name | Description |
---|---|
MESSAGE_EXAMPLE_FLAG_NODE | The name of the flag being used to follow content (nodes). |
MESSAGE_EXAMPLE_FLAG_USER | The name of the flag being used to follow users. |