message_og_example.module in Message 7
File
message_og_example/message_og_example.module
View source
<?php
include_once 'message_og_example.features.inc';
function message_og_example_og_membership_insert($og_membership) {
if ($og_membership->state != OG_STATE_ACTIVE || $og_membership->entity_type != 'user') {
return;
}
if ($og_membership->group_type != 'node') {
return;
}
if (!$og_membership->etid) {
return;
}
$node = node_load($og_membership->gid);
$message = message_create('example_og_user_membership', array(
'uid' => $og_membership->etid,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_node_groups_ref[] = $node;
$wrapper->field_published
->set($node->status);
$wrapper
->save();
}
function message_og_example_node_insert($node) {
list(, , $bundle) = entity_extract_ids('node', $node);
if (!og_is_group_content_type('node', $bundle)) {
return;
}
$wrapper = entity_metadata_wrapper('node', $node);
if (!$wrapper->{'og_membership__' . OG_STATE_ACTIVE}
->value()) {
return;
}
$nids = array();
foreach ($wrapper->{'og_membership__' . OG_STATE_ACTIVE}
->value() as $og_membership) {
if ($og_membership->group_type != 'node') {
continue;
}
$nids[] = $og_membership->gid;
}
if (!$nids) {
return;
}
$arguments['!groups'] = array(
'callback' => 'message_og_example_group_list',
'pass message' => TRUE,
);
$message = message_create('example_og_post_in_group', array(
'arguments' => $arguments,
'uid' => $node->uid,
));
$wrapper = entity_metadata_wrapper('message', $message);
$wrapper->field_node_ref
->set($node);
$wrapper->field_node_groups_ref
->set($nids);
$wrapper->field_published
->set($node->status);
$wrapper
->save();
}
function message_og_example_group_list(Message $message) {
$wrapper = entity_metadata_wrapper('message', $message);
$items = array();
foreach ($wrapper->field_node_groups_ref
->value() as $node) {
if (!node_access('view', $node)) {
continue;
}
$url = entity_uri('node', $node);
$label = entity_label('node', $node);
$items[] = l($label, $url['path'], $url['options']);
}
return implode(', ', $items);
}