function JsonapiTestFieldAccessHooks::entityFieldAccess

Implements hook_entity_field_access().

File

core/modules/jsonapi/tests/modules/jsonapi_test_field_access/src/Hook/JsonapiTestFieldAccessHooks.php, line 20

Class

JsonapiTestFieldAccessHooks
Hook implementations for jsonapi_test_field_access.

Namespace

Drupal\jsonapi_test_field_access\Hook

Code

public function entityFieldAccess($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.