Last updated February 11, 2011. Created by Amitaibu on December 31, 2008.
Edited by HongPong. Log in to edit this page.
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.
<?php
// 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']);
}
}
?>
Comments
For drupal5
Is it possible to do the same for drupal 5?
Submit functions on drupal 5 recieve ($form_id, $form_values) parameters. They are not passed by reference, so they can't be modified there.
Not working with js
I can't get this to work. The OG javascript enters in to a loop and crashes the browser if I do not kill it and the OG section simply is not displayed on the form, I think we need to modify the js as wel for this to work properly. Does someone have a solution? Thanks
Johan Bichel Lindegaard
http://johan.cc