Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I reviewed the code to make it work with OG 7.2
If your group content is only attached to a single OG, you might want to use the following pattern (i.e with pathauto) to display the group title/node title
[node:og-group-audience:first]/[node:title]
Hope it might help somebody
<?php
/**
* @file
* A simple Groups Audience token for Organic Groups.
*/
/**
* Implements hook_token_info().
*/
function og_token_token_info() {
$info = array();
$info['tokens']['node']['og-group-audience'] = array(
'name' => t('Group audience labels'),
'description' => t('Outputs the group audience labels of a node.'),
'type' => 'array',
);
return $info;
}
/**
* Implements hook_tokens().
*/
function og_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$join = isset($options['join']) ? $options['join'] : ', ';
// Skip if we are not given a node with a group audience field.
if ($type == 'node' && isset($data['node']->{OG_AUDIENCE_FIELD}) && !empty($data['node']->{OG_AUDIENCE_FIELD})) {
// Loop through the given tokens.
foreach ($tokens as $name => $original) {
switch ($name) {
case 'og-group-audience':
// Avoid loading the labels more than once.
if (!isset($labels)) {
$nodes = og_token_get_group_audience_groups($data['node']);
$labels = og_token_get_group_labels($nodes, $options);
}
$replacements[$original] = implode($join, $labels);
break;
}
}
// Resolve chained token relationships.
if ($group_audience_tokens = token_find_with_prefix($tokens, 'og-group-audience')) {
if (!isset($labels)) {
$nodes = og_token_get_group_audience_groups($data['node']);
$labels = og_token_get_group_labels($nodes, $options);
}
$replacements += token_generate('array', $group_audience_tokens, array('array' => $labels), $options);
}
}
return $replacements;
}
/**
* Return the group labels of a given array of nodes.
*
* @param array $nodes
* An array of Organic Group group objects.
* @param array $options
* An associative array of options for token replacement.
*
* @return array
* An array containing the labels of the passed in groups.
*/
function og_token_get_group_labels($nodes, $options) {
$labels = array();
$sanitize = !empty($options['sanitize']);
foreach ($nodes as $group) {
$labels[] = $sanitize ? check_plain($group->title) : $group->title;
}
return $labels;
}
/**
* Return the groups that are referenced in the group_audience field of a node.
*
* @param object $node
* A node object.
*
* @return array
* An array of Organic Group group objects.
*/
function og_token_get_group_audience_groups($node) {
$nodes = array();
$group_audiences = field_get_items('node', $node, OG_AUDIENCE_FIELD);
foreach ($group_audiences as $group_audience) {
if (!empty($group_audience['target_id']) && $node_parent = node_load($group_audience['target_id'])) {
$nodes[] = $node_parent;
}
}
return $nodes;
}
Comment | File | Size | Author |
---|---|---|---|
#4 | og_token.zip | 1.51 KB | joshuautley |
#1 | og_token-og_7.2-1930132-1.patch | 3.51 KB | jeffschuler |
Comments
Comment #1
jeffschulerThis is working for me with OG 7.x-2.1.
Here is this change as a patch.
Comment #2
RKopacz CreditAttribution: RKopacz commentedHas this patch been committed to the Dev version of OG 7.x-2.x? Is it in 7x-2.4?
Comment #3
vrwired CreditAttribution: vrwired commentedThis patch works for me with OG 7.x-2.7 using [node:og-group-audience:first]/[node:title] in pathauto - thanks!
Comment #4
joshuautley CreditAttribution: joshuautley commentedI've tested the patch and I too can confirm it works as designed. Any chance of getting this released as an actual project?
Attached is the module and info files with the code applied from lahode.
Comment #5
ajayg CreditAttribution: ajayg as a volunteer commentedIf you have a very simple need just using the tokens mentioned in this issue works and you may not need this module
https://www.drupal.org/node/1423790
I tried the og_token module but it won't allow me to set the OG Audience field as a title for block created by og extras module. But I used [site:og-context--node] and that worked , without using og_token module.