function UserAccessTestHooks::entityFieldAccess

Implements hook_entity_field_access().

File

core/modules/user/tests/modules/user_access_test/src/Hook/UserAccessTestHooks.php, line 54

Class

UserAccessTestHooks
Hook implementations for user_access_test.

Namespace

Drupal\user_access_test\Hook

Code

public function entityFieldAccess($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');
        }
    }
    if (\Drupal::state()->get('user_access_test_forbid_mail_edit', FALSE)) {
        if ($operation === 'edit' && $items && $items->getEntity()
            ->getEntityTypeId() === 'user' && $field_definition->getName() === 'mail') {
            return AccessResult::forbidden();
        }
    }
    return AccessResult::neutral();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.