updated core to 8.6.3
This commit is contained in:
@@ -229,6 +229,8 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
|
||||
}
|
||||
|
||||
// Overwrite the received fields.
|
||||
// @todo Remove $changed_fields in https://www.drupal.org/project/drupal/issues/2862574.
|
||||
$changed_fields = [];
|
||||
foreach ($entity->_restSubmittedFields as $field_name) {
|
||||
$field = $entity->get($field_name);
|
||||
// It is not possible to set the language to NULL as it is automatically
|
||||
@@ -238,12 +240,18 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
|
||||
continue;
|
||||
}
|
||||
if ($this->checkPatchFieldAccess($original_entity->get($field_name), $field)) {
|
||||
$changed_fields[] = $field_name;
|
||||
$original_entity->set($field_name, $field->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// If no fields are changed, we can send a response immediately!
|
||||
if (empty($changed_fields)) {
|
||||
return new ModifiedResourceResponse($original_entity, 200);
|
||||
}
|
||||
|
||||
// Validate the received data before saving.
|
||||
$this->validate($original_entity);
|
||||
$this->validate($original_entity, $changed_fields);
|
||||
try {
|
||||
$original_entity->save();
|
||||
$this->logger->notice('Updated entity %type with ID %id.', ['%type' => $original_entity->getEntityTypeId(), '%id' => $original_entity->id()]);
|
||||
@@ -275,13 +283,6 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
|
||||
* @internal
|
||||
*/
|
||||
protected function checkPatchFieldAccess(FieldItemListInterface $original_field, FieldItemListInterface $received_field) {
|
||||
// If the user is allowed to edit the field, it is always safe to set the
|
||||
// received value. We may be setting an unchanged value, but that is ok.
|
||||
$field_edit_access = $original_field->access('edit', NULL, TRUE);
|
||||
if ($field_edit_access->isAllowed()) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// The user might not have access to edit the field, but still needs to
|
||||
// submit the current field value as part of the PATCH request. For
|
||||
// example, the entity keys required by denormalizers. Therefore, if the
|
||||
@@ -294,6 +295,13 @@ class EntityResource extends ResourceBase implements DependentPluginInterface {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// If the user is allowed to edit the field, it is always safe to set the
|
||||
// received value. We may be setting an unchanged value, but that is ok.
|
||||
$field_edit_access = $original_field->access('edit', NULL, TRUE);
|
||||
if ($field_edit_access->isAllowed()) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// It's helpful and safe to let the user know when they are not allowed to
|
||||
// update a field.
|
||||
$field_name = $received_field->getName();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user