function user_access_test_entity_field_access in Drupal 9
Same name and namespace in other branches
- 8 core/modules/user/tests/modules/user_access_test/user_access_test.module \user_access_test_entity_field_access()
Implements hook_entity_field_access().
File
- core/
modules/ user/ tests/ modules/ user_access_test/ user_access_test.module, line 50 - Dummy module implementing hook_user_access() to test if entity access is respected.
Code
function user_access_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Account with role sub-admin can view the status, init and mail fields for
// user with no roles.
if ($field_definition
->getTargetEntityTypeId() == 'user' && $operation === 'view' && in_array($field_definition
->getName(), [
'status',
'init',
'mail',
])) {
if ($items == NULL || count($items
->getEntity()
->getRoles()) == 1) {
return AccessResult::allowedIfHasPermission($account, 'sub-admin');
}
}
return AccessResult::neutral();
}