.../src/Plugin/rest/resource/EntityResource.php | 26 ++++++++++------------ core/modules/rest/src/Tests/UpdateTest.php | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 18a1101..be66ee8 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -4,6 +4,7 @@ use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Core\Config\Entity\ConfigEntityType; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Config\ConfigFactoryInterface; @@ -213,20 +214,17 @@ public function patch(EntityInterface $original_entity, EntityInterface $entity foreach ($entity->_restSubmittedFields as $field_name) { $field = $entity->get($field_name); - // Entity key fields need special treatment: together they uniquely - // identify the entity. Therefore it does not make sense to modify any of - // them. However, rather than throwing an error, we just ignore them as - // long as their specified values match their current values. - if (in_array($field_name, $entity_keys, TRUE)) { - // Unchanged values for entity keys don't need access checking. - if ($original_entity->get($field_name)->getValue() === $entity->get($field_name)->getValue()) { - continue; - } - // It is not possible to set the language to NULL as it is automatically - // re-initialized. As it must not be empty, skip it if it is. - elseif (isset($entity_keys['langcode']) && $field_name === $entity_keys['langcode'] && $field->isEmpty()) { - continue; - } + // It is not possible to set the language to NULL as it is automatically + // re-initialized. As it must not be empty, skip it if it is. + if (isset($entity_keys['langcode']) && $field_name === $entity_keys['langcode'] && $field->isEmpty()) { + continue; + } + + // Allow sending read-only fields, as long as their value is unchanged. + if ($entity instanceof ContentEntityInterface + && $field->getFieldDefinition()->isReadOnly() + && $original_entity->get($field_name)->getValue() === $entity->get($field_name)->getValue()) { + continue; } if (!$original_entity->get($field_name)->access('edit')) { diff --git a/core/modules/rest/src/Tests/UpdateTest.php b/core/modules/rest/src/Tests/UpdateTest.php index cdef82c..727324f 100644 --- a/core/modules/rest/src/Tests/UpdateTest.php +++ b/core/modules/rest/src/Tests/UpdateTest.php @@ -283,6 +283,7 @@ public function testUpdateComment() { $entity_values['uid'] = $account->id(); $comment = Comment::create($entity_values); $comment->save(); + $comment_id = $comment->id(); $this->pass('Test case 1: PATCH comment using HAL+JSON.'); $comment->setSubject('Initial subject')->save(); @@ -300,7 +301,7 @@ public function testUpdateComment() { $this->assertNotEqual('Updated subject', $comment->getSubject()); $comment->setSubject('Updated subject'); $this->patchEntity($comment, $read_only_fields, $account, 'hal_json', 'application/hal+json'); - $comment = Comment::load($comment->id()); + $comment = Comment::load($comment_id); $this->assertEqual('Updated subject', $comment->getSubject()); $this->pass('Test case 1: PATCH comment using JSON.');