You are here

function jsonapi_test_field_access_entity_field_access in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module \jsonapi_test_field_access_entity_field_access()

Implements hook_entity_field_access().

File

tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.module, line 15
Contains hook implementations for testing the JSON:API module.

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();
}