On this page
Organic Groups for developers
Last updated on
30 April 2025
This is a placeholder for different code snippets related to the organic groups module.
Allow only a single group for every group post.
In OG every group post can be associated with several group nodes. Here's the code you can use to force it to accept only a single group per post.
// Some hook_alter() function that calls _og_form_alters_audience_form_alter().
/**
* Helper function; Make the groups audience accept only a single group.
*/
function _og_form_alters_audience_form_alter(&$form) {
if (!empty($form['og_nodeapi']['visible']['og_groups'])) {
$form['og_nodeapi']['visible']['og_groups']['#multiple'] = FALSE;
// Add own submit handler so we can cast the groups back to an array.
$form['#submit'][] = 'og_form_alters_audience_form_alter_submit';
}
}
/**
* Submit handler; Since we permit only a single group per content, we need to
* case to groups into an array
*/
function og_form_alters_audience_form_alter_submit($form, &$form_state){
if (!empty($form_state['values']['og_groups']) && !is_array($form_state['values']['og_groups'])) {
$form_state['values']['og_groups'] = drupal_map_assoc((array)$form_state['values']['og_groups']);
}
}
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion