function opigno_class_block_access in Opigno class 8
Same name and namespace in other branches
- 3.x opigno_class.module \opigno_class_block_access()
Implements hook_block_access().
File
- ./
opigno_class.module, line 139 - Contains opigno_class.module.
Code
function opigno_class_block_access(Block $block, $operation, AccountInterface $account) {
if ($block
->id() == 'opignoadmin') {
$uid = $account
->id();
$roles = $account
->getRoles();
$access = FALSE;
foreach ($roles as $role) {
if (in_array($role, [
'administrator',
'content_manager',
'user_manager',
]) || $uid == 1) {
$access = TRUE;
}
}
if (!$access) {
// Get trainings where the current user is a 'student manager' or user has global role 'class manager'.
/** @var \Drupal\group\GroupMembershipLoaderInterface $membership_service */
$membership_service = \Drupal::service('group.membership_loader');
$memberships = $membership_service
->loadByUser($account, [
'learning_path-user_manager',
'opigno_class-class_manager',
]);
$groups_ids = [];
foreach ($memberships as $membership) {
$group = $membership
->getGroup();
if ($group
->bundle() == 'opigno_class') {
$db_connection = \Drupal::service('database');
$query_class = $db_connection
->select('group_content_field_data', 'g_c_f_d')
->fields('g_c_f_d', [
'gid',
])
->condition('entity_id', $group
->id())
->condition('type', 'group_content_type_27efa0097d858')
->execute()
->fetchAll();
foreach ($query_class as $result_ids) {
$groups_ids[] = $result_ids->gid;
}
}
else {
$groups_ids[] = $group
->id();
}
}
if (!empty($groups_ids)) {
$access = TRUE;
}
}
if (!$access) {
return AccessResult::forbidden();
}
}
}