You are here

protected function EntityReferenceRevisions::getEntity in Entity Reference Revisions 8

Gets the entity.

Parameters

\Drupal\migrate\Row $row: The row object.

array $old_destination_id_values: The old destination IDs.

Return value

\Drupal\Core\Entity\EntityInterface|false The entity or false if it can not be created.

Overrides EntityRevision::getEntity

File

src/Plugin/migrate/destination/EntityReferenceRevisions.php, line 103

Class

EntityReferenceRevisions
Provides entity_reference_revisions destination plugin.

Namespace

Drupal\entity_reference_revisions\Plugin\migrate\destination

Code

protected function getEntity(Row $row, array $oldDestinationIdValues) {
  $entity_id = $oldDestinationIdValues ? array_shift($oldDestinationIdValues) : $this
    ->getEntityId($row);
  $configuration = $this
    ->getConfiguration();
  if (isset($configuration['force_revision']) && $configuration['force_revision'] == TRUE) {
    $revision_id = NULL;
  }
  else {
    $revision_id = $oldDestinationIdValues ? array_pop($oldDestinationIdValues) : $row
      ->getDestinationProperty($this
      ->getKey('revision'));
  }

  // If a specific revision_id is supplied and exists, assert the entity_id
  // matches (if supplied), and update the revision.

  /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityInterface $entity */
  if (!empty($revision_id) && ($entity = $this->storage
    ->loadRevision($revision_id))) {
    if (!empty($entity_id) && $entity
      ->id() != $entity_id) {
      throw new MigrateException("The revision_id exists for this entity type, but does not belong to the given entity id");
    }
    $entity = $this
      ->updateEntity($entity, $row) ?: $entity;
  }
  elseif (!empty($entity_id) && ($entity = $this->storage
    ->load($entity_id))) {

    // If so configured, create a new revision while updating.
    if (!empty($this->configuration['new_revisions'])) {
      $entity
        ->setNewRevision(TRUE);
    }
    $entity = $this
      ->updateEntity($entity, $row) ?: $entity;
  }
  else {

    // Attempt to ensure we always have a bundle.
    if ($bundle = $this
      ->getBundle($row)) {
      $row
        ->setDestinationProperty($this
        ->getKey('bundle'), $bundle);
    }

    // Stubs might need some required fields filled in.
    if ($row
      ->isStub()) {
      $this
        ->processStubRow($row);
    }
    $entity = $this->storage
      ->create($row
      ->getDestination())
      ->enforceIsNew(TRUE);
    $entity
      ->setNewRevision(TRUE);
  }
  $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
  return $entity;
}