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;
} $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.