You are here

protected function WebformGroupManager::getUserGroupRoles in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_group/src/WebformGroupManager.php \Drupal\webform_group\WebformGroupManager::getUserGroupRoles()

Get current user group roles for group content.

Parameters

\Drupal\group\Entity\GroupContentInterface $group_content: Group content.

\Drupal\Core\Session\AccountInterface $account: A user account.

Return value

array An array of group roles for the group content.

2 calls to WebformGroupManager::getUserGroupRoles()
WebformGroupManager::getCurrentUserGroupRoles in modules/webform_group/src/WebformGroupManager.php
Get the current user's group roles.
WebformGroupManager::getWebformSubmissionUserGroupRoles in modules/webform_group/src/WebformGroupManager.php
Get group roles for a webform submission and a specified user account.

File

modules/webform_group/src/WebformGroupManager.php, line 272

Class

WebformGroupManager
Webform group manager manager.

Namespace

Drupal\webform_group

Code

protected function getUserGroupRoles(GroupContentInterface $group_content, AccountInterface $account) {
  $group = $group_content
    ->getGroup();
  $group_type_id = $group
    ->getGroupType()
    ->id();

  // Must get implied groups, which includes outsider, by calling
  // \Drupal\group\Entity\Storage\GroupRoleStorage::loadByUserAndGroup.
  // @see \Drupal\group\Entity\Storage\GroupRoleStorageInterface::loadByUserAndGroup

  /** @var \Drupal\group\Entity\Storage\GroupRoleStorageInterface $group_role_storage */
  $group_role_storage = $this->entityTypeManager
    ->getStorage('group_role');
  $group_roles = $group_role_storage
    ->loadByUserAndGroup($account, $group, TRUE);
  if (!$group_roles) {
    return [];
  }
  $group_roles = array_keys($group_roles);
  $group_roles = array_combine($group_roles, $group_roles);

  // Add global roles (i.e. member, outsider, etc...)
  foreach ($group_roles as $group_role_id) {
    if (strpos($group_role_id, $group_type_id . '-') === 0) {
      $global_role_id = str_replace($group_type_id . '-', '', $group_role_id);
      $group_roles[$global_role_id] = $global_role_id;
    }
  }
  return $group_roles;
}