Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EntityValidationTrait.php

trait EntityValidationTrait

Provides a method to validate an entity.

@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.

Hierarchy

  • trait \Drupal\jsonapi\Entity\EntityValidationTrait

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

2 files declare their use of EntityValidationTrait
EntityResource.php in core/modules/jsonapi/src/Controller/EntityResource.php
FileUpload.php in core/modules/jsonapi/src/Controller/FileUpload.php

File

core/modules/jsonapi/src/Entity/EntityValidationTrait.php, line 18

Namespace

Drupal\jsonapi\Entity
View source
trait EntityValidationTrait {
    
    /**
     * Verifies that an entity does not violate any validation constraints.
     *
     * @param \Drupal\Core\Entity\EntityInterface $entity
     *   The entity object.
     * @param string[] $field_names
     *   (optional) An array of field names. If specified, filters the violations
     *   list to include only this set of fields. Defaults to NULL,
     *   which means that all violations will be reported.
     *
     * @throws \Drupal\jsonapi\Exception\UnprocessableHttpEntityException
     *   Thrown when violations remain after filtering.
     *
     * @see \Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait::validate()
     */
    protected static function validate(EntityInterface $entity, ?array $field_names = NULL) {
        if (!$entity instanceof FieldableEntityInterface) {
            return;
        }
        $violations = $entity->validate();
        // Remove violations of inaccessible fields as they cannot stem from our
        // changes.
        $violations->filterByFieldAccess();
        // Filter violations based on the given fields.
        if ($field_names !== NULL) {
            $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions()), $field_names));
        }
        if (count($violations) > 0) {
            // Instead of returning a generic 400 response we use the more specific
            // 422 Unprocessable Entity code from RFC 4918. That way clients can
            // distinguish between general syntax errors in bad serializations (code
            // 400) and semantic errors in well-formed requests (code 422).
            // @see \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer
            $exception = new UnprocessableHttpEntityException();
            $exception->setViolations($violations);
            throw $exception;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
EntityValidationTrait::validate protected static function Verifies that an entity does not violate any validation constraints.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal