message_example.module in Message 7
File
message_example/message_example.module
View source
<?php
include_once 'message_example.features.inc';
function message_example_ctools_plugin_directory($module, $plugin) {
if ($module == 'message') {
return 'plugins/' . $plugin;
}
}
function message_example_node_insert($node) {
$message = message_create('example_create_node', array(
'uid' => $node->uid,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_node_ref
->set($node);
$wrapper->field_published
->set($node->status);
$wrapper
->save();
}
function message_example_comment_insert($comment) {
$account = user_load($comment->uid);
$node = node_load($comment->nid);
$message = message_create('example_create_comment', array(), $account);
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_node_ref
->set($node);
$wrapper->field_comment_ref
->set($comment);
$published = $node->status && $comment->status;
$wrapper->field_published
->set($published);
$wrapper
->save();
}
function message_example_user_insert(&$edit, $account, $category) {
$message = message_create('example_user_register', array(), $account);
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_published
->set($account->status);
$wrapper
->save();
}
function message_example_node_update($node) {
message_example_update_message_status('node', $node);
}
function message_example_comment_update($comment) {
message_example_update_message_status('comment', $comment);
}
function message_example_update_message_status($entity_type, $entity) {
if ($entity->status == $entity->original->status) {
return;
}
list($id) = entity_extract_ids($entity_type, $entity);
$field_name = 'field_' . $entity_type . '_ref';
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'message')
->fieldCondition($field_name, 'target_id', $id, '=')
->execute();
if (empty($result['message'])) {
return;
}
foreach (array_keys($result['message']) as $mid) {
$wrapper = entity_metadata_wrapper('message', $mid);
if ($entity_type == 'comment') {
$node = node_load($entity->nid);
$status = intval($entity->status && $node->status);
}
else {
$status = $entity->status;
}
if ($wrapper->field_published
->value() != $status) {
$wrapper->field_published
->set($status);
$wrapper
->save();
}
}
}