0

I'm using organic groups 7.x-2.x, as well as admin_views for administering users. I have a field that I'm able to sort users on depending on what department they are in using Views Bulk Operations at admin/people.

My question involves subscribing people in bulk to a specific organic group. Is there a way to achieve this using Views Bulk Operations?

4 Answers 4

1

Users can now be added in bulk to organic groups using Views Bulk Operations out of box. The simplest way to do this is to take the following steps:

  1. Download Admin Views
  2. Edit the people listing page view at /admin/structure/views/view/admin_views_user/edit
  3. In the view under fields, select "Bulk operations: User" Under "Modify entity values", be sure that either "All" or at least "Group Membership" is selected
  4. Save the view if needed
  5. Go to /admin/people and filter the users as desired Select "Change value" and click Execute
  6. Under "Fields", select "Group membership", and select the group(s) you wish to assign the users to.
  7. Be sure the "Add new value(s) to Group membership, instead of overwriting the existing values" checkbox is checked or your users will lose their existing membership settings.
  8. Click Next to complete the operation.
1
  • This only works if you are a user with "administer Organic Groups" permission or for user 1. Normal group admins do not see their groups in the second step of the bulk operation. Also the groups that are shown in the second step are generated by og_get_entity_groups which are the groups the user is member of, in this case we would like to see the groups the user is admin of. Feb 14, 2016 at 13:45
0

There is a module called Organic Groups Mass Add that uses VBO to add people en mass. http://drupal.org/project/og_massadd

The unfortunate part of this answer is that the module doesn't seem to be working with OG 2.x. I installed it yesterday and had issues (reported in the issue queue).

I'm not quite at the point where this feature must be implemented, but will be in a couple weeks. So, I'll be looking at the module closer to see if there are any bug fixes I can do to make it work.

0

You can also use the https://www.drupal.org/project/og_bulkadd module which has a different approach: A 'Bulk adding users' tab is added to the 'Add members' tab on the group managment section.

0

I made an action for this that works. It can be used with Views Bulk Operations and also works for users that don't have the "administer organic groups" permission so they can add in bulk members to the groups where they are administrator of.

In a first screen of VBO you can select the users, in the second screen you get checkbox list of the groups where you are administrator of.

Unfortunately VBO forces users to have the "administer users" permission set for the acting user to be able to do actions on the $user which is needed here. So the patch https://www.drupal.org/files/issues/views_bulk_operations-execute_as_user_one-1334088-41.patch of https://www.drupal.org/node/1334088 and then setting the checkbox "Skip permission step" is needed.

    /**
     * Get the groups where a certain user is administrator of
     *
     * @param $uid
     * @return mixed: array with gid and groupname to be used as options in the checkboxes
     */
    function MYMODULE_get_group_ids_current_user_is_admin_of($uid) {
      $query = db_select('og_users_roles', 'og');
      $query->condition('rid', '3', '=');
      $query->condition('uid', $uid, '=');
      $query->fields('og', array('gid'));
      $result = $query->execute();
      while ($record = $result->fetchAssoc()) {
        $gid = $record['gid'];
        // $groupname = node_load($gid)->title; // looks cleaner but is slower in this case
        $groupname = db_query("SELECT title from {node} where nid = :nid", array(':nid' => $gid))->fetchField();
        $groups[$gid] = $groupname;
      }
      return $groups;
    }

    /**
     * Implements hook_action_info()
     */
    function MYMODULE_action_info() {
      return array(
        'MYMODULE_add_user_to_ogs_action' => array(
          'type' => 'user',
          'label' => t('Add users to Organic groups'),
          'configurable' => TRUE,
          'behavior' => array('changes_property'),
          'triggers' => array('node_presave', 'node_insert', 'node_update'),
        ),
      );
    }

    /**
     * Generates settings form for action_example_node_sticky_action().
     */
    function MYMODULE_add_user_to_ogs_action_form($context) {
      // Get the current uid
      global $user;
      $uid = $user->uid;

      $groups = MYMODULE_get_group_ids_current_user_is_admin_of($uid);

      $form['add_to_groups'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Add to groups where I am administrator of'),
        '#description' => t('Select the groups where you want to add these users to'),
        '#options' => $groups,
      );

      return $form;
    }

    /**
     * Submit handler for MYMODULE_add_user_to_ogs_action
     */
    function MYMODULE_add_user_to_ogs_action_submit($form, $form_state) {
      return array('gids' => $form_state['values']['add_to_groups']);
    }

    /**
     * Action function for MYMODULE_add_user_to_ogs_action.
     *
     * Add the user to the selected organic groups
     *
     */
    function MYMODULE_add_user_to_ogs_action($user, $context) {
        // The account is the user where we do the action on
  $account = $user;
  $uid = $account->uid;

  $newref = array();

  // Get an array of all gids
  if (isset($context['gids'])) {
    $group_ids = $context['gids'];
  }

  $group_type = 'node';
  $field_name = 'og_user_node';

  foreach ($group_ids as $gid) {
    // make sure its an integer
    $gid = (int) $gid;

    // Groups that are not selected in the checkboxes have gid 0
    if (isset($gid) && ($gid!=0)) {
      // Set the og membership
      $group_type = 'node';
      $values = array(
        'entity_type' => 'user',
        'entity' => $account->uid,
        'field_name' => FALSE,
        'state' => 1,
      );
      $og_membership = og_group($group_type, $gid, $values);

      // Build an array with the new gids in the format that is needed for the entity reference field
      $newref[] = array(
        'target_id' => $gid,
      );

      $group = entity_load_single('node', $gid);
      watchdog('ogbulk_add_user',
        t('The user %username has been added to the group %group-title (%gid).'),
        array(
          '%gid' => $gid,
          '%username' => $account->name,
          '%group-title' => $group->title,
        )
      );
    }
  }

  // Why is saving $og_membership not enough to store the gid's?
  // Remove this part when $og_membership would save the gids as expected
  // For now as a fix we need to get all gids and save them in the user
  $existing_gids = og_get_groups_by_user($account);

  // Build an array with the existing gids in the format that is needed for the entity reference field
  $ref = array();
  foreach ($existing_gids['node'] as $existing_gid) {
    $ref[] = array(
      'target_id' => $existing_gid,
    );
  }

  // Merge the existing gids with the extra gids
  $ref = array_merge($ref, $newref);
  $data['og_user_node'][LANGUAGE_NONE] = $ref;
  user_save($account, $data);

  return;
}

Your Answer

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

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