"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php" (26 Nov 2020, 2176 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "EntityResourceValidationTrait.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\rest\Plugin\rest\resource;
    4 
    5 use Drupal\Component\Render\PlainTextOutput;
    6 use Drupal\Core\Entity\EntityInterface;
    7 use Drupal\Core\Entity\FieldableEntityInterface;
    8 use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
    9 
   10 /**
   11  * @internal
   12  * @todo Consider making public in https://www.drupal.org/node/2300677
   13  */
   14 trait EntityResourceValidationTrait {
   15 
   16   /**
   17    * Verifies that an entity does not violate any validation constraints.
   18    *
   19    * The validation errors will be filtered to not include fields to which the
   20    * current user does not have access and if $fields_to_validate is provided
   21    * will only include fields in that array.
   22    *
   23    * @param \Drupal\Core\Entity\EntityInterface $entity
   24    *   The entity to validate.
   25    * @param string[] $fields_to_validate
   26    *   (optional) An array of field names. If specified, filters the violations
   27    *   list to include only this set of fields.
   28    *
   29    * @throws \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException
   30    *   If validation errors are found.
   31    */
   32   protected function validate(EntityInterface $entity, array $fields_to_validate = []) {
   33     // @todo Update this check in https://www.drupal.org/node/2300677.
   34     if (!$entity instanceof FieldableEntityInterface) {
   35       return;
   36     }
   37     $violations = $entity->validate();
   38 
   39     // Remove violations of inaccessible fields as they cannot stem from our
   40     // changes.
   41     $violations->filterByFieldAccess();
   42 
   43     if ($fields_to_validate) {
   44       // Filter violations by explicitly provided array of field names.
   45       $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions()), $fields_to_validate));
   46     }
   47 
   48     if ($violations->count() > 0) {
   49       $message = "Unprocessable Entity: validation failed.\n";
   50       foreach ($violations as $violation) {
   51         // We strip every HTML from the error message to have a nicer to read
   52         // message on REST responses.
   53         $message .= $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation->getMessage()) . "\n";
   54       }
   55       throw new UnprocessableEntityHttpException($message);
   56     }
   57   }
   58 
   59 }