You are here

public function ContentEntityStorageTrait::save in Multiversion 8

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


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






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 = [
    $entity->_rev->new_edit = FALSE;

  // Every update is a new revision with this storage model.

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

  // Prepare the file directory.
  if ($entity instanceof FileInterface) {

  // 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
            'uuid' => $item["entity"]

          // 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
            $value[$delta] = $item;

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

    // Update indexes.
    if (!$local) {
    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);