Programmatically Get All Roles For a Group Type

Created on 6 January 2018, over 6 years ago
Updated 28 February 2024, 4 months ago

At this point I am extreme frustrated, I have spent all day on this, and still can't accomplish what should be a basic task. Below I have some code to load all the group roles available to the "client" group type. $group_role_syncronizer produces an array with 3 group role ids, but when I go into the foreach loop to load each group role, only the first one loads, and the 2nd group role returns null upon load. To try and rule out issues here, I have added my admin (whom I'm logged in as) to all roles, but it still only loads the first role.

I simply need a list of all roles available to a group type, how do I accomplish this?

  $group_role_syncronizer = \Drupal::service('group_role.synchronizer');

    $roleIds = $group_role_syncronizer->getGroupRoleIdsByGroupType('client');

    $options = [];


    foreach ($roleIds as $id) {
      $role = \Drupal\group\Entity\GroupRole::load($id);
      $options[$id] = $role->label();
    }
πŸ“Œ Task
Status

Fixed

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand jlscott

    When working with Group v2 or Group v3, the query will need to be updated to:

        $group_roles = $this->entityTypeManager->getStorage('group_role')->loadByProperties([
          'group_type' => 'my_group_type',
          'scope' => 'individual',
        ]);
Production build 0.69.0 2024