You are here

public function SocialGroupHelperService::getGroupsToAddUrl in Open Social 8.9

Same name and namespace in other branches
  1. 8.8 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
  2. 10.3.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
  3. 10.0.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
  4. 10.1.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
  5. 10.2.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()

Get the add group URL for given user.

This returns either /group/add or /group/add/{group_type} depending upon the permission of the user to create group.

Parameters

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

Return value

\Drupal\Core\Url URL of the group add page.

File

modules/social_features/social_group/src/SocialGroupHelperService.php, line 214

Class

SocialGroupHelperService
Class SocialGroupHelperService.

Namespace

Drupal\social_group

Code

public function getGroupsToAddUrl(AccountInterface $account) {
  $url = NULL;
  $user_can_create_groups = [];

  // Get all available group types.
  foreach (GroupType::loadMultiple() as $group_type) {

    // When the user has permission to create a group of the current type, add
    // this to the create group array.
    if ($account
      ->hasPermission('create ' . $group_type
      ->id() . ' group')) {
      $user_can_create_groups[$group_type
        ->id()] = $group_type;
    }
    if (count($user_can_create_groups) > 1) {
      break;
    }
  }

  // There's just one group this user can create.
  if (count($user_can_create_groups) === 1) {

    // When there is only one group allowed, add create the url to create a
    // group of this type.
    $allowed_group_type = reset($user_can_create_groups);

    /** @var \Drupal\group\Entity\Group $allowed_group_type */
    $url = Url::fromRoute('entity.group.add_form', [
      'group_type' => $allowed_group_type
        ->id(),
    ]);
  }
  return $url;
}