0

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?

1
  • I found out, that if I call the mymodule_vbo_actions_users_woformfunction 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.
    – schulle877
    Sep 1, 2014 at 10:50

2 Answers 2

0

You might need to grant a OG role to the new member after adding them.

e.g. og_role_grant('node', $node_page->nid, $account->uid, 2);

The magic number on the end determines the type of role granted. 1 = non-member, 2 = member, 3 = administrator member

2
  • May the magic number be the rid of the table og_role, where the gid = the id of the group the user should be added to and where the name=the name of the role the user should get? So my query for the rid looks like this: $result_roles = db_query("SELECT rid FROM og_role o where group_bundle = :bundle and name=:name and gid=:gid;",array(':bundle'=>$group_node->type,':name'=>'editor', ':gid'=>$group_node->nid)); I granted the role like this: og_role_grant('node',$group_node->nid,$account->uid,$role_id); Bute the result is the same, the user is not added... Although the logged in user is..
    – schulle877
    Sep 1, 2014 at 8:16
  • ...the admin for the group.
    – schulle877
    Sep 1, 2014 at 8:17
0

I had the same issue today.

VBO is reverting the action if you don't have the proper og_permission. So check if you have assigned the proper permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.