public function DataProviderEntity::entityValidate in RESTful 7.2
Validate an entity before it is saved.
Parameters
\EntityDrupalWrapper $wrapper: The wrapped entity.
Throws
Overrides DataProviderEntityInterface::entityValidate
1 call to DataProviderEntity::entityValidate()
- DataProviderEntity::setPropertyValues in src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php - Set properties of the entity based on the request, and save the entity.
File
- src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php, line 348 - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.
Class
- DataProviderEntity
- Class DataProviderEntity.
Namespace
Drupal\restful\Plugin\resource\DataProviderCode
public function entityValidate(\EntityDrupalWrapper $wrapper) {
if (!module_exists('entity_validator')) {
// Entity validator doesn't exist.
return;
}
try {
$validator_handler = ValidatorPluginManager::EntityValidator($wrapper
->type(), $wrapper
->getBundle());
} catch (PluginNotFoundException $e) {
// Entity validator handler doesn't exist for the entity.
return;
}
if ($validator_handler
->validate($wrapper
->value(), TRUE)) {
// Entity is valid.
return;
}
$errors = $validator_handler
->getErrors(FALSE);
$map = array();
foreach ($this->fieldDefinitions as $resource_field_name => $resource_field) {
if (!($property = $resource_field
->getProperty())) {
continue;
}
$public_name = $resource_field
->getPublicName();
if (empty($errors[$public_name])) {
// Field validated.
continue;
}
$map[$public_name] = $resource_field_name;
$params['@fields'][] = $resource_field_name;
}
if (empty($params['@fields'])) {
// There was a validation error, but on non-public fields, so we need to
// throw an exception, but can't say on which fields it occurred.
throw new BadRequestException('Invalid value(s) sent with the request.');
}
$params['@fields'] = implode(',', $params['@fields']);
$exception = new BadRequestException(format_plural(count($map), 'Invalid value in field @fields.', 'Invalid values in fields @fields.', $params));
foreach ($errors as $property_name => $messages) {
if (empty($map[$property_name])) {
// Entity is not valid, but on a field not public.
continue;
}
$resource_field_name = $map[$property_name];
foreach ($messages as $message) {
$message['params']['@field'] = $resource_field_name;
$output = format_string($message['message'], $message['params']);
$exception
->addFieldError($resource_field_name, $output);
}
}
// Throw the exception.
throw $exception;
}