Commit 65067ad9 authored by Berdir's avatar Berdir Committed by miro_dietiker

Issue #2953343 by Berdir, markhuot, miro_dietiker, plach: Experimental...

Issue #2953343 by Berdir, markhuot, miro_dietiker, plach: Experimental asymmetrical content translation with Paragraphs breaks concurrent drafts
parent 064f436e
......@@ -5,6 +5,7 @@ namespace Drupal\entity_reference_revisions\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\TranslatableRevisionableInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
......@@ -254,24 +255,37 @@ class EntityReferenceRevisionsItem extends EntityReferenceItem implements Option
// If it is a new entity, parent will save it.
parent::preSave();
$is_affected = TRUE;
if (!$has_new) {
// Create a new revision if it is a composite entity in a host with a new
// revision.
$host = $this->getEntity();
$needs_save = $this->entity instanceof EntityNeedsSaveInterface && $this->entity->needsSave();
if (!$host->isNew() && $host->isNewRevision() && $this->entity && $this->entity->getEntityType()->get('entity_revision_parent_id_field')) {
$this->entity->setNewRevision();
if ($host->isDefaultRevision()) {
$this->entity->isDefaultRevision(TRUE);
// The item is considered to be affected if the field is either
// untranslatable or there are translation changes. This ensures that for
// translatable fields, a new revision of the referenced entity is only
// created for the affected translations and that the revision ID does not
// change on the unaffected translations. In turn, the host entity is not
// marked as affected for these translations.
$is_affected = !$this->getFieldDefinition()->isTranslatable() || ($host instanceof TranslatableRevisionableInterface && $host->hasTranslationChanges());
if ($is_affected && !$host->isNew() && $this->entity && $this->entity->getEntityType()->get('entity_revision_parent_id_field')) {
if ($host->isNewRevision()) {
$this->entity->setNewRevision();
$needs_save = TRUE;
}
// Additionally ensure that the default revision state is kept in sync.
if ($this->entity && $host->isDefaultRevision() != $this->entity->isDefaultRevision()) {
$this->entity->isDefaultRevision($host->isDefaultRevision());
$needs_save = TRUE;
}
$needs_save = TRUE;
}
if ($needs_save) {
$this->entity->save();
}
}
if ($this->entity) {
if ($this->entity && $is_affected) {
$this->target_revision_id = $this->entity->getRevisionId();
}
}
......
......@@ -88,7 +88,8 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
'type' => 'article',
'composite_reference' => $entity_test,
]);
// Check the name is properly set.
// Check the name is properly set and that getValue() returns the entity
// when it is marked as needs save."
$values = $node->composite_reference->getValue();
$this->assertTrue(isset($values[0]['entity']));
static::assertEquals($values[0]['entity']->name->value, $text);
......@@ -102,20 +103,22 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
static::assertEquals($entity_test_after->name->value, $text);
$new_text = 'Dummy text again';
// Set the name again.
$entity_test->name = $new_text;
$entity_test->setNeedsSave(FALSE);
// Set another name and save the node without marking it as needs saving.
$entity_test_after->name = $new_text;
$entity_test_after->setNeedsSave(FALSE);
// Load the Node and check the composite reference field is not set.
// Load the Node and check the composite reference entity is not returned
// from getValue() if it is not marked as needs saving.
$node = Node::load($node->id());
$values = $node->composite_reference->getValue();
$this->assertFalse(isset($values[0]['entity']));
$node->composite_reference = $entity_test;
$node->composite_reference = $entity_test_after;
$node->save();
// Check the name is not updated.
\Drupal::entityTypeManager()->getStorage('entity_test_composite')->resetCache();
$entity_test_after = EntityTestCompositeRelationship::load($entity_test->id());
static::assertEquals($entity_test_after->name->value, $text);
static::assertEquals($text, $entity_test_after->name->value);
// Test if after delete the referenced entity there are no problems setting
// the referencing values to the parent.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment