jsonapi_test_field_access.module in JSON:API 8
Same filename and directory in other branches
Contains hook implementations for testing the JSON API module.
File
tests/modules/jsonapi_test_field_access/jsonapi_test_field_access.moduleView source
<?php
/**
* @file
* Contains hook implementations for testing the JSON API module.
*/
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_entity_field_access().
*/
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();
}
Functions
Name | Description |
---|---|
jsonapi_test_field_access_entity_field_access | Implements hook_entity_field_access(). |