function jsonapi_test_field_access_entity_field_access

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module \jsonapi_test_field_access_entity_field_access()
  2. 10 core/modules/jsonapi/tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module \jsonapi_test_field_access_entity_field_access()
  3. 11.x core/modules/jsonapi/tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module \jsonapi_test_field_access_entity_field_access()

Implements hook_entity_field_access().

File

core/modules/jsonapi/tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module, line 15

Code

function jsonapi_test_field_access_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account) {
    // @see \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testRelationships().
    if ($field_definition->getName() === 'field_jsonapi_test_entity_ref') {
        // Forbid access in all cases.
        $permission = "field_jsonapi_test_entity_ref {$operation} access";
        $access_result = $account->hasPermission($permission) ? AccessResult::allowed() : AccessResult::forbidden("The '{$permission}' permission is required.");
        return $access_result->addCacheContexts([
            'user.permissions',
        ]);
    }
    // No opinion.
    return AccessResult::neutral();
}

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