function social_group_flexible_group_group_access in Open Social 10.0.x
Same name and namespace in other branches
- 10.3.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_group_access()
- 10.1.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_group_access()
- 10.2.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_group_access()
Implements hook_ENTITY_TYPE_access().
File
- modules/
social_features/ social_group/ modules/ social_group_flexible_group/ social_group_flexible_group.module, line 320 - The Social Group Flexible Group module.
Code
function social_group_flexible_group_group_access(EntityInterface $entity, $operation, AccountInterface $account) {
$result = AccessResult::neutral();
// Write custom access checks based on the new group visibility field.
// If group visibility doesn't exist we can skip this.
/** @var \Drupal\group\Entity\GroupInterface $entity */
if ($operation !== 'view' || !$entity
->hasField('field_flexible_group_visibility')) {
return $result;
}
// If user has administer groups access we can skip this.
if ($account
->hasPermission('manage all groups')) {
return $result;
}
// If group visibility value doesn't exist we can skip.
if (empty($entity
->getFieldValue('field_flexible_group_visibility', 'value'))) {
return $result;
}
// If group visibility exists and public is selected, we can skip.
$group_visibility = $entity
->getFieldValue('field_flexible_group_visibility', 'value');
if ($group_visibility === 'public') {
return $result;
}
// If group visibility exists and community or members is selected, check
// if user is logged.
if ($group_visibility === 'community') {
return AccessResult::forbiddenIf($account
->isAnonymous())
->cachePerUser()
->addCacheableDependency($entity);
}
// If group visibility exists and members only is selected, we need to check
// if user is logged in and is a member of the group.
if ($group_visibility === 'members') {
$not_a_member = !$entity
->getMember($account) || $account
->isAnonymous();
return AccessResult::forbiddenIf($not_a_member)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($entity)
->addCacheableDependency($account);
}
return $result;
}