Commit b862bc7b authored by catch's avatar catch
Browse files

Issue #3554269 by amateescu, berdir: Impossible to save a pending revision as...

Issue #3554269 by amateescu, berdir: Impossible to save a pending revision as the default one without creating a new revision

(cherry picked from commit c0d521a7)
parent d31ff79c
Loading
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -1311,8 +1311,13 @@ protected function saveToDedicatedTables(ContentEntityInterface $entity, $update
      }

      // When updating an existing revision, keep the existing records if the
      // field values did not change.
      if (!$entity->isNewRevision() && $original && !$this->hasFieldValueChanged($field_definition, $entity, $original)) {
      // field values did not change or if we're not re-saving a pending
      // revision as the default one.
      if (!$entity->isNewRevision()
        && $original
        && $entity->isDefaultRevision() === $original->isDefaultRevision()
        && !$this->hasFieldValueChanged($field_definition, $entity, $original)
      ) {
        continue;
      }

+4 −3
Original line number Diff line number Diff line
@@ -56,11 +56,13 @@ public function publish() {
        foreach ($tracked_entities as $entity_type_id => $revision_difference) {
          $entity_revisions = $this->entityTypeManager->getStorage($entity_type_id)
            ->loadMultipleRevisions(array_keys($revision_difference));
          $default_revisions = $this->entityTypeManager->getStorage($entity_type_id)
            ->loadMultiple(array_values($revision_difference));

          /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
          foreach ($entity_revisions as $entity) {
            // We might be saving a lot of entities during workspace publishing,
            // so we set the original entity manually for performance.
            $entity->setOriginal(clone $entity);

            // When pushing workspace-specific revisions to the default
            // workspace (Live), we simply need to mark them as default
            // revisions.
@@ -71,7 +73,6 @@ public function publish() {
            $field_name = $entity->getEntityType()->getRevisionMetadataKey('workspace');
            $entity->{$field_name}->target_id = NULL;

            $entity->setOriginal($default_revisions[$entity->id()]);
            $entity->save();
            $counter++;

+20 −0
Original line number Diff line number Diff line
@@ -131,6 +131,26 @@ public function testFieldEntityRevisionWrite(): void {
    // The updated field value should have correctly saved as 'foo'.
    $forward_revision = $storage->loadRevision($forward_revision_id);
    $this->assertEquals('foo', $forward_revision->field_test_text->value);

    // Create another entity.
    $entity = EntityTestRev::create();
    $entity->field_test_text->value = 'foo';
    $entity->save();

    // Create a new non-default revision and set the field value to 'bar'.
    $entity->setNewRevision(TRUE);
    $entity->isDefaultRevision(FALSE);
    $entity->field_test_text->value = 'bar';
    $entity->save();

    // Now save the pending revision as the default one, without creating a new
    // revision.
    $entity->isDefaultRevision(TRUE);
    $entity->save();

    // The updated field value should have correctly saved as 'bar'.
    $default_revision = $storage->loadUnchanged($entity->id());
    $this->assertEquals('bar', $default_revision->field_test_text->value);
  }

  /**