core/lib/Drupal/Core/Entity/ContentEntityBase.php | 27 ++++++++-------------- .../Core/Entity/ContentEntityStorageBase.php | 3 +-- .../Core/Entity/FieldableEntityInterface.php | 9 +------- .../src/Plugin/rest/resource/EntityResource.php | 2 +- .../rest/resource/EntityResourceAccessTrait.php | 2 +- 5 files changed, 13 insertions(+), 30 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 9c5eb9f..8d97240 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -161,7 +161,7 @@ * * @var string[] */ - protected $changedFields = []; + protected $dirtyFields = []; /** * {@inheritdoc} @@ -261,7 +261,10 @@ protected function getLanguages() { * {@inheritdoc} */ public function postCreate(EntityStorageInterface $storage) { - $this->newRevision = TRUE; + if ($this->isDefaultTranslation()) { + $this->newRevision = TRUE; + } + $this->dirtyFields = []; } /** @@ -397,7 +400,7 @@ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $reco public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); - $this->resetChangedFieldList(); + $this->dirtyFields = []; // Update the status of all saved translations. $removed = []; @@ -576,20 +579,8 @@ public function getFields($include_computed = TRUE) { /** * {@inheritdoc} */ - public function getChangedFields() { - // Filter the all fields to only fields that have changed. - return array_filter($this->getFields(), function ($field) { - /** @var FieldItemListInterface $field */ - return isset($this->changedFields[$field->getName()]); - }); - } - - /** - * {@inheritdoc} - */ - public function resetChangedFieldList() { - $this->changedFields = []; - return $this; + public function getDirtyFields() { + return array_intersect_key($this->getFields(), $this->dirtyFields); } /** @@ -730,7 +721,7 @@ protected function updateFieldLangcodes($langcode) { */ public function onChange($name) { // Add the field to the changed fields list. - $this->changedFields[$name] = TRUE; + $this->dirtyFields[$name] = TRUE; // Check if the changed name is the value of an entity key and if the value // of that is currently cached, if so, reset it. Exclude the bundle from diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index fba244a..6449470 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -86,7 +86,6 @@ protected function doCreate(array $values) { } $entity = new $this->entityClass([], $this->entityTypeId, $bundle); $this->initFieldValues($entity, $values); - $entity->resetChangedFieldList(); return $entity; } @@ -136,7 +135,7 @@ public function createTranslation(ContentEntityInterface $entity, $langcode, arr $values[$this->getEntityType()->getKey('default_langcode')] = FALSE; $this->initFieldValues($translation, $values, $field_names); $this->invokeHook('translation_create', $translation); - $translation->resetChangedFieldList(); + $translation->postCreate($this); return $translation; } diff --git a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php index 90f7cff..3c9fd55 100644 --- a/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php +++ b/core/lib/Drupal/Core/Entity/FieldableEntityInterface.php @@ -242,13 +242,6 @@ public function setValidationRequired($required); * @return \Drupal\Core\Field\FieldItemListInterface[] * An array of field item lists implementing, keyed by field name. */ - public function getChangedFields(); - - /** - * Resets the list an array of changed field names. - * - * @return $this - */ - public function resetChangedFieldList(); + public function getDirtyFields(); } diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index d3ed2f9..5083fcd 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -261,7 +261,7 @@ public function patch(EntityInterface $original_entity, EntityInterface $entity // Overwrite the received properties. if ($entity instanceof FieldableEntityInterface) { $entity_keys = $entity->getEntityType()->getKeys(); - foreach ($entity->getChangedFields() as $field_name => $field) { + foreach ($entity->getDirtyFields() as $field_name => $field) { // Entity key fields need special treatment: together they uniquely // identify the entity. Therefore it does not make sense to modify any of diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResourceAccessTrait.php b/core/modules/rest/src/Plugin/rest/resource/EntityResourceAccessTrait.php index ebea0e9..076675c 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResourceAccessTrait.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResourceAccessTrait.php @@ -27,7 +27,7 @@ protected function checkEditFieldAccess(EntityInterface $entity) { // submitted by the user. Field access makes no difference between 'create' // and 'update', so the 'edit' operation is used here. if ($entity instanceof FieldableEntityInterface) { - foreach (array_keys($entity->getChangedFields()) as $field_name) { + foreach (array_keys($entity->getDirtyFields()) as $field_name) { if (!$entity->get($field_name)->access('edit')) { throw new AccessDeniedHttpException("Access denied on creating field '$field_name'."); }