I like to add users to an OG group using vbo, a custom action and og_group().
I build up a page with a view of users to add (/node/%nid/add_user).
On this view I added a views bulk operations field with the "skip batch" option. (I skipped the batch to get the current %nid from the url).
In the custom code I like to add the selected user to the page shown. I know I can do this also with the standard group forms, but I want to do it this way :-)
So here is my code to subscribe the user to the group:
function mymodule_vbo_actions_users_woform(&$user_node, $context) {
global $user;
//dpm($node); dpm($context);return;
switch ($context['settings']['action_type']) {
case 'add_user_to_group':
$nid_page = menu_get_object();
if (!isset($nid_page)) $nid_page = arg(1);
$nid_page = $context['page_nid'];
$account = user_load($user_node->uid);
//dpm($nid_page);
$node_page = node_load($nid_page);
dpm($user_node);
dpm($account);
dpm($node_page);
$aValues = array('entity_type'=>'user','entity'=>$account,'membership type' => 'OG_MEMBERSHIP_TYPE_DEFAULT',);
$og_membership = og_group('node',$node_page->nid,$aValues,FALSE);
$og_membership->save();
dpm($og_membership);
// get member roles id for selected group bundle
$result_roles = db_query("SELECT rid FROM og_role o where group_bundle = :bundle and name=:name and gid=:gid;",array(':bundle'=>$node_page->type,':name'=>'bidder', ':gid'=>$node_page->nid));
$role_id = NULL;
foreach ($result_roles as $key_roles => $value_roles) {
$role_id = $value_roles->rid;
break;
}
dpm($role_id);
og_role_grant('node',$node_page->nid,$account->uid,$role_id);
break;
}
}
This code seems to be correct. I watched the og_membership
table and the record for the selected user is inserted, but then DELETED immediately!?
So the question is of course: Why is the record deleted?
mymodule_vbo_actions_users_woform
function from another hook, e.g. a hook_node_view, the user is added to the group and is NOT deleted... So VBO deletes the record.