class UnprocessableHttpEntityExceptionNormalizer
Normalizes and UnprocessableHttpEntityException.
Normalizes an UnprocessableHttpEntityException in compliance with the JSON API specification. A source pointer is added to help client applications report validation errors, for example on an Entity edit form.
@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, \Drupal\serialization\Normalizer\CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\jsonapi\Normalizer\NormalizerBase extends \Drupal\serialization\Normalizer\NormalizerBase
- class \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer extends \Drupal\jsonapi\Normalizer\NormalizerBase
- class \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer extends \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer
 
 
 - class \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer extends \Drupal\jsonapi\Normalizer\NormalizerBase
 
 - class \Drupal\jsonapi\Normalizer\NormalizerBase extends \Drupal\serialization\Normalizer\NormalizerBase
 
Expanded class hierarchy of UnprocessableHttpEntityExceptionNormalizer
See also
https://www.drupal.org/project/drupal/issues/3032787
http://jsonapi.org/format/#error-objects
1 string reference to 'UnprocessableHttpEntityExceptionNormalizer'
- jsonapi.services.yml in core/
modules/ jsonapi/ jsonapi.services.yml  - core/modules/jsonapi/jsonapi.services.yml
 
1 service uses UnprocessableHttpEntityExceptionNormalizer
File
- 
              core/
modules/ jsonapi/ src/ Normalizer/ UnprocessableHttpEntityExceptionNormalizer.php, line 24  
Namespace
Drupal\jsonapi\NormalizerView source
class UnprocessableHttpEntityExceptionNormalizer extends HttpExceptionNormalizer {
  
  /**
   * {@inheritdoc}
   */
  protected function buildErrorObjects(HttpException $exception) {
    /** @var \Drupal\jsonapi\Exception\UnprocessableHttpEntityException $exception */
    $errors = parent::buildErrorObjects($exception);
    $error = $errors[0];
    unset($error['links']);
    $errors = [];
    $violations = $exception->getViolations();
    $entity_violations = $violations->getEntityViolations();
    foreach ($entity_violations as $violation) {
      /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
      $error['detail'] = 'Entity is not valid: ' . $violation->getMessage();
      $error['source']['pointer'] = '/data';
      $errors[] = $error;
    }
    $entity = $violations->getEntity();
    foreach ($violations->getFieldNames() as $field_name) {
      $field_violations = $violations->getByField($field_name);
      $cardinality = $entity->get($field_name)
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getCardinality();
      foreach ($field_violations as $violation) {
        /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
        $error['detail'] = $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation->getMessage());
        $pointer = '/data/attributes/' . str_replace('.', '/', $violation->getPropertyPath());
        if ($cardinality == 1) {
          // Remove erroneous '/0/' index for single-value fields.
          $pointer = str_replace("/data/attributes/{$field_name}/0/", "/data/attributes/{$field_name}/", $pointer);
        }
        $error['source']['pointer'] = $pointer;
        $errors[] = $error;
      }
    }
    return $errors;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSupportedTypes(?string $format) : array {
    return [
      UnprocessableHttpEntityException::class => TRUE,
    ];
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides | 
|---|---|---|---|---|---|
| CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY | constant | Name of key for bubbling cacheability metadata via serialization context. | |||
| HttpExceptionNormalizer::$currentUser | protected | property | The current user making the request. | ||
| HttpExceptionNormalizer::getInfoUrl | public static | function | Return a string to the common problem type. | ||
| HttpExceptionNormalizer::hasCacheableSupportsMethod | public | function | Overrides NormalizerBase::hasCacheableSupportsMethod | ||
| HttpExceptionNormalizer::normalize | public | function | |||
| HttpExceptionNormalizer::__construct | public | function | HttpExceptionNormalizer constructor. | ||
| NormalizerBase::$format | protected | property | List of formats which supports (de-)normalization. | Overrides NormalizerBase::$format | |
| NormalizerBase::addCacheableDependency | protected | function | Adds cacheability if applicable. | ||
| NormalizerBase::checkFormat | protected | function | Checks if the provided format is supported by this normalizer. | Overrides NormalizerBase::checkFormat | |
| NormalizerBase::rasterizeValueRecursive | protected static | function | Rasterizes a value recursively. | ||
| NormalizerBase::supportsDenormalization | public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 | |
| NormalizerBase::supportsNormalization | public | function | 1 | ||
| UnprocessableHttpEntityExceptionNormalizer::buildErrorObjects | protected | function | Builds the normalized JSON:API error objects for the response. | Overrides HttpExceptionNormalizer::buildErrorObjects | |
| UnprocessableHttpEntityExceptionNormalizer::getSupportedTypes | public | function | Overrides HttpExceptionNormalizer::getSupportedTypes | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.