public function EntityReferenceRevisionsItem::preSave in Multiversion 8
Change the logic around saving auto-created entities.
Overrides EntityReferenceRevisionsItem::preSave
See also
\Drupal\multiversion\EntityReferenceFieldTrait::preSave()
\Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem::preSave()
File
- src/
EntityReferenceRevisionsItem.php, line 23
Class
- EntityReferenceRevisionsItem
- Alternative entity reference revisions base field item type class.
Namespace
Drupal\multiversionCode
public function preSave() {
if (!$this
->parentIsEnabledEntityType()) {
// Call source class method if parent entity isn't supported by Multiversion.
parent::preSave();
return;
}
$has_new = $this
->hasNewEntity();
// If it is a new entity, parent will save it.
$this
->entityReferencePreSave();
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);
}
$needs_save = TRUE;
}
if ($needs_save) {
// Delete the paragraph when the host entity is deleted.
if ($host->_deleted->value == TRUE) {
$this->entity
->delete();
}
elseif ($host->_rev->new_edit == FALSE) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $multiversion_manager */
$multiversion_manager = \Drupal::service('multiversion.manager');
$entity_type = $this->entity
->getEntityType();
if ($multiversion_manager
->isEnabledEntityType($entity_type)) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
/** @var \Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface $storage */
$storage = $entity_type_manager
->getStorage($this->entity
->getEntityTypeId());
$entities = $storage
->loadByProperties([
'uuid' => $this->entity
->uuid(),
]);
$entity = reset($entities);
if ($entity) {
$parent_id = $this->entity
->getEntityType()
->get('entity_revision_parent_id_field');
$entity
->set($parent_id, $this->entity
->id());
$entity
->setNewRevision(FALSE);
$this->entity = $entity;
}
$storage
->saveWithoutForcingNewRevision($this->entity);
}
}
else {
$this->entity
->save();
}
}
}
if ($this->entity) {
$this->target_revision_id = $this->entity
->getRevisionId();
}
}