function EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects()
  2. 10 core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects()
  3. 11.x core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php \Drupal\jsonapi\Normalizer\EntityAccessDeniedHttpExceptionNormalizer::buildErrorObjects()

Overrides HttpExceptionNormalizer::buildErrorObjects

File

core/modules/jsonapi/src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php, line 34

Class

EntityAccessDeniedHttpExceptionNormalizer
Normalizes an EntityAccessDeniedException.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function buildErrorObjects(HttpException $exception) {
    $errors = parent::buildErrorObjects($exception);
    if ($exception instanceof EntityAccessDeniedHttpException) {
        $error = $exception->getError();
        
        /** @var \Drupal\Core\Entity\EntityInterface $entity */
        $entity = $error['entity'];
        $pointer = $error['pointer'];
        $reason = $error['reason'];
        $relationship_field = $error['relationship_field'] ?? NULL;
        if (isset($entity)) {
            $entity_type_id = $entity->getEntityTypeId();
            $bundle = $entity->bundle();
            
            /** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
            $resource_type = \Drupal::service('jsonapi.resource_type.repository')->get($entity_type_id, $bundle);
            $resource_type_name = $resource_type->getTypeName();
            $route_name = !is_null($relationship_field) ? "jsonapi.{$resource_type_name}.{$relationship_field}.related" : "jsonapi.{$resource_type_name}.individual";
            $url = Url::fromRoute($route_name, [
                'entity' => $entity->uuid(),
            ]);
            $errors[0]['links']['via']['href'] = $url->setAbsolute()
                ->toString(TRUE)
                ->getGeneratedUrl();
        }
        $errors[0]['source']['pointer'] = $pointer;
        if ($reason) {
            $errors[0]['detail'] = isset($errors[0]['detail']) ? $errors[0]['detail'] . ' ' . $reason : $reason;
        }
    }
    return $errors;
}

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