Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / rest / src / Plugin / rest / resource / EntityResourceValidationTrait.php
index 09b4b64baec0f47956107060f8068a899d0f8231..03201922f47d35c84a18c5ea729cb6d4c7910688 100644 (file)
@@ -14,16 +14,23 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
 trait EntityResourceValidationTrait {
 
   /**
-   * Verifies that the whole entity does not violate any validation constraints.
+   * Verifies that an entity does not violate any validation constraints.
+   *
+   * The validation errors will be filtered to not include fields to which the
+   * current user does not have access and if $fields_to_validate is provided
+   * will only include fields in that array.
    *
    * @param \Drupal\Core\Entity\EntityInterface $entity
    *   The entity to validate.
+   * @param string[] $fields_to_validate
+   *   (optional) An array of field names. If specified, filters the violations
+   *   list to include only this set of fields.
    *
    * @throws \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException
    *   If validation errors are found.
    */
-  protected function validate(EntityInterface $entity) {
-    // @todo Remove when https://www.drupal.org/node/2164373 is committed.
+  protected function validate(EntityInterface $entity, array $fields_to_validate = []) {
+    // @todo Update this check in https://www.drupal.org/node/2300677.
     if (!$entity instanceof FieldableEntityInterface) {
       return;
     }
@@ -33,6 +40,11 @@ trait EntityResourceValidationTrait {
     // changes.
     $violations->filterByFieldAccess();
 
+    if ($fields_to_validate) {
+      // Filter violations by explicitly provided array of field names.
+      $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions()), $fields_to_validate));
+    }
+
     if ($violations->count() > 0) {
       $message = "Unprocessable Entity: validation failed.\n";
       foreach ($violations as $violation) {