You are here

public function EntityReferenceRevisionsItem::postSave in Multiversion 8

Change the logic around revisions handling.

By default multiversion storage forces new revision on entity save. But this should be not done on "postSave" call, as we will finish the save process only after this method call.

Overrides EntityReferenceRevisionsItem::postSave

See also

\Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem::postSave()

\Drupal\multiversion\Entity\Storage\ContentEntityStorageTrait::saveWithoutForcingNewRevision()

File

src/EntityReferenceRevisionsItem.php, line 94

Class

EntityReferenceRevisionsItem
Alternative entity reference revisions base field item type class.

Namespace

Drupal\multiversion

Code

public function postSave($update) {
  $needs_save = FALSE;

  // If any of entity, parent type or parent id is missing then return.
  if (!$this->entity || !$this->entity
    ->getEntityType()
    ->get('entity_revision_parent_type_field') || !$this->entity
    ->getEntityType()
    ->get('entity_revision_parent_id_field')) {
    return;
  }
  if (!$this
    ->parentIsEnabledEntityType()) {

    // Call source class method if parent entity isn't supported by Multiversion.
    parent::postSave($update);
    return;
  }
  $entity = $this->entity;
  $parent_entity = $this
    ->getEntity();

  // If the entity has a parent field name get the key.
  if ($entity
    ->getEntityType()
    ->get('entity_revision_parent_field_name_field')) {
    $parent_field_name = $entity
      ->getEntityType()
      ->get('entity_revision_parent_field_name_field');

    // If parent field name has changed then set it.
    if ($entity
      ->get($parent_field_name)->value != $this
      ->getFieldDefinition()
      ->getName()) {
      $entity
        ->set($parent_field_name, $this
        ->getFieldDefinition()
        ->getName());
      $needs_save = TRUE;
    }
  }
  $parent_type = $entity
    ->getEntityType()
    ->get('entity_revision_parent_type_field');
  $parent_id = $entity
    ->getEntityType()
    ->get('entity_revision_parent_id_field');

  // If the parent type has changed then set it.
  if ($entity
    ->get($parent_type)->value != $parent_entity
    ->getEntityTypeId()) {
    $entity
      ->set($parent_type, $parent_entity
      ->getEntityTypeId());
    $needs_save = TRUE;
  }

  // If the parent id has changed then set it.
  if ($entity
    ->get($parent_id)->value != $parent_entity
    ->id()) {
    $entity
      ->set($parent_id, $parent_entity
      ->id());
    $needs_save = TRUE;
  }
  if ($needs_save) {

    // Check if any of the keys has changed, save it, do not create a new
    // revision.
    $entity
      ->setNewRevision(FALSE);

    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');

    /** @var \Drupal\multiversion\MultiversionManagerInterface $multiversion_manager */
    $multiversion_manager = \Drupal::service('multiversion.manager');
    $entity_type_id = $entity
      ->getEntityTypeId();
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    if ($multiversion_manager
      ->isEnabledEntityType($entity_type)) {

      /** @var \Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface $storage */
      $storage = $entity_type_manager
        ->getStorage($entity_type_id);
      $storage
        ->saveWithoutForcingNewRevision($entity);
    }
  }
}