You are here

public function ContentEntityStorageTrait::save in Multiversion 8.2

Same name and namespace in other branches
  1. 8 src/Entity/Storage/ContentEntityStorageTrait.php \Drupal\multiversion\Entity\Storage\ContentEntityStorageTrait::save()
1 call to ContentEntityStorageTrait::save()
ContentEntityStorageTrait::delete in src/Entity/Storage/ContentEntityStorageTrait.php

File

src/Entity/Storage/ContentEntityStorageTrait.php, line 175

Class

ContentEntityStorageTrait

Namespace

Drupal\multiversion\Entity\Storage

Code

public function save(EntityInterface $entity) {

  // When importing with default content we want to it to be treated like a
  // replicate, and not as a new edit.
  if (isset($entity->default_content)) {
    list(, $hash) = explode('-', $entity->_rev->value);
    $entity->_rev->revisions = [
      $hash,
    ];
    $entity->_rev->new_edit = FALSE;
  }

  // Every update is a new revision with this storage model.
  $entity
    ->setNewRevision();

  // Index the revision.
  $branch = $this
    ->buildRevisionBranch($entity);
  $local = (bool) $this->entityType
    ->get('local');
  if (!$local) {
    $this
      ->indexEntityRevision($entity);
    $this
      ->indexEntityRevisionTree($entity, $branch);
  }

  // Prepare the file directory.
  if ($entity instanceof FileInterface) {
    multiversion_prepare_file_destination($entity
      ->getFileUri());
  }

  // We prohibit creation of the url alias for entities with a random label,
  // because this can lead to unnecessary redirects.
  if ($entity->_rev->is_stub && isset($entity->path->pathauto)) {
    $entity->path->pathauto = PathautoState::SKIP;
  }
  foreach ($entity
    ->getFields() as $name => $field) {
    if ($field instanceof EntityReferenceFieldItemListInterface && !$field instanceof EntityReferenceRevisionsFieldItemList) {
      $value = [];

      // For the entity reference field with stub entity referenced we check
      // if the entity with corresponding UUID and real values
      // have been created in the database already and use it instead.
      foreach ($field
        ->getValue() as $delta => $item) {

        // At first we take value we receive as it is.
        $value[$delta] = $item;

        // Only stub entities will satisfy this condition.
        if ($item['target_id'] === NULL && isset($item['entity']) && $item['entity']->_rev->is_stub) {

          // Lookup for entities with corresponding UUID.
          $target_entities = $this
            ->loadByProperties([
            'uuid' => $item["entity"]
              ->uuid(),
          ]);

          // Replace stub with existing entity if we found such.
          if (!empty($target_entities)) {

            // Here we take first assuming there should be no entities
            // with duplicated UUIDs in one workspace.
            $target_entity = reset($target_entities);
            $item['target_id'] = $target_entity
              ->id();
            unset($item['entity']);
            $value[$delta] = $item;
          }
        }
      }

      // @todo This conditions is not obligatory but will prevent
      // unnecessary action when field value already empty.
      if (!empty($value)) {
        $field
          ->setValue($value, FALSE);
      }
    }
  }
  try {

    /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspaces_manager */
    $workspaces_manager = \Drupal::service('workspaces.manager');
    if ($local) {
      $save_result = $workspaces_manager
        ->executeInWorkspace(WorkspaceInterface::DEFAULT_WORKSPACE, function () use ($entity) {
        return parent::save($entity);
      });
    }
    else {
      $save_result = parent::save($entity);
    }

    // Update indexes.
    $this
      ->indexEntity($entity);
    if (!$local) {
      $this
        ->indexEntitySequence($entity);
      $this
        ->indexEntityRevision($entity);
      $this
        ->trackConflicts($entity);
    }
    return $save_result;
  } catch (\Exception $e) {

    // If a new attempt at saving the entity is made after an exception its
    // important that a new rev token is not generated.
    $entity->_rev->new_edit = FALSE;
    throw new EntityStorageException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}