if (!
$entity instanceof FieldableEntityInterface
) { return;
} $violations =
$entity->
validate();
// Remove violations of inaccessible fields as they cannot stem from our
// 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) { // We strip every HTML from the error message to have a nicer to read
// message on REST responses.
$message .=
$violation->
getPropertyPath() . ': ' . PlainTextOutput::
renderFromHtml($violation->
getMessage()) . "\n";
} throw new UnprocessableEntityHttpException($message);
} }