function commons_groups_tokens in Drupal Commons 7.3
Implements hook_tokens().
File
- modules/
commons/ commons_groups/ commons_groups.module, line 551
Code
function commons_groups_tokens($type, $tokens, $data = array(), $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$group = $data['node'];
foreach ($tokens as $name => $original) {
if ($name == 'commons-groups-group-contributors-count-topics') {
$replacements[$original] = commons_groups_group_contributors_count_topics($group);
return $replacements;
}
}
}
if ($type == 'commons-groups') {
if (!empty($tokens['in-groups-text'])) {
// Build a list of groups associated with this message.
$text = '';
$target_nids = array();
$related_groups = array();
$related_gids = array();
// First, build an array of target nodes associated with the message.
foreach ($data['message']->field_target_nodes[LANGUAGE_NONE] as $key => $value) {
$target_nids[] = $value['target_id'];
}
// If there are no target nodes, the in-groups-text token should be empty.
if (empty($target_nids)) {
$replacements['[commons-groups:in-groups-text]'] = $text;
return $replacements;
}
// Build a list of groups associated with the target nodes.
// For now, we assume that the group type is node.
foreach ($target_nids as $key => $nid) {
$og_memberships_this_target = og_get_entity_groups('node', $nid);
if (!empty($og_memberships_this_target['node'])) {
$og_memberships_this_target = $og_memberships_this_target['node'];
foreach ($og_memberships_this_target as $membership_id => $gid) {
$related_gids[] = $gid;
}
}
}
// If no groups are associated with any of the target nodes,
// then we have no "in the groups" text.
if (empty($related_gids)) {
$replacements['[commons-groups:in-groups-text]'] = '';
return $replacements;
}
$related_groups = entity_load('node', $related_gids);
// Key the array of groups in a predictable way.
$related_groups = array_values($related_groups);
// Generate the appropriate text depending on the number of groups
// associated with the message:
$replacements['[commons-groups:in-groups-text]'] = commons_groups_related_groups_text($related_groups);
return $replacements;
}
}
if ($type == 'node' && !empty($data['node'])) {
if (!empty($tokens['commons-groups-first-group'])) {
$group = $data['node'];
$text = '';
if (!empty($group->og_group_ref[LANGUAGE_NONE])) {
$wrapper = entity_metadata_wrapper('node', $group);
$groups = $wrapper->og_group_ref
->value();
// Return the title of the first group associated with this node.
$first_group = array_shift($groups);
// Use the title field by default and fall back to the node title.
$first_group_wrapper = entity_metadata_wrapper('node', $first_group);
$text = isset($first_group_wrapper->title_field) ? $first_group_wrapper->title_field
->value() : $first_group_wrapper
->label();
}
$replacements['[node:commons-groups-first-group]'] = $text;
return $replacements;
}
}
}