public function SocialGroupHelperService::getGroupsToAddUrl in Open Social 10.1.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
- 8.8 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
- 10.3.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
- 10.0.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getGroupsToAddUrl()
- 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 271
Class
- SocialGroupHelperService
- Class SocialGroupHelperService.
Namespace
Drupal\social_groupCode
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;
}