You are here

protected function EntityContentComplete::getEntity in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/destination/EntityContentComplete.php \Drupal\migrate\Plugin\migrate\destination\EntityContentComplete::getEntity()
  2. 9 core/modules/migrate/src/Plugin/migrate/destination/EntityContentComplete.php \Drupal\migrate\Plugin\migrate\destination\EntityContentComplete::getEntity()

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 The entity.

Overrides Entity::getEntity

File

core/modules/migrate/src/Plugin/migrate/destination/EntityContentComplete.php, line 56

Class

EntityContentComplete
Provides a destination for migrating the entire entity revision table.

Namespace

Drupal\migrate\Plugin\migrate\destination

Code

protected function getEntity(Row $row, array $old_destination_id_values) {
  $revision_id = $old_destination_id_values ? $old_destination_id_values[1] : $row
    ->getDestinationProperty($this
    ->getKey('revision'));

  // If we are re-running a migration with set revision IDs and the
  // destination revision ID already exists then do not create a new revision.
  if (!empty($revision_id) && ($entity = $this->storage
    ->loadRevision($revision_id))) {
    $entity
      ->setNewRevision(FALSE);
  }
  elseif (($entity_id = $row
    ->getDestinationProperty($this
    ->getKey('id'))) && ($entity = $this->storage
    ->load($entity_id))) {

    // We want to create a new entity. Set enforceIsNew() FALSE is  necessary
    // to properly save a new entity while setting the ID. Without it, the
    // system would see that the ID is already set and assume it is an update.
    $entity
      ->enforceIsNew(FALSE);

    // Intentionally create a new revision. Setting new revision TRUE here may
    // not be necessary, it is done for clarity.
    $entity
      ->setNewRevision(TRUE);
  }
  else {

    // Attempt to set the 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());
    $entity
      ->enforceIsNew();
  }

  // We need to update the entity, so that the destination row IDs are
  // correct.
  $entity = $this
    ->updateEntity($entity, $row);
  $entity
    ->isDefaultRevision(TRUE);
  if ($entity instanceof EntityChangedInterface && $entity instanceof ContentEntityInterface) {

    // If we updated any untranslatable fields, update the timestamp for the
    // other translations.

    /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityChangedInterface $entity */
    foreach ($entity
      ->getTranslationLanguages() as $langcode => $language) {

      // If we updated an untranslated field, then set the changed time for
      // for all translations to match the current row that we are saving.
      // In this context, getChangedTime() should return the value we just
      // set in the updateEntity() call above.
      if ($entity
        ->getTranslation($langcode)
        ->hasTranslationChanges()) {
        $entity
          ->getTranslation($langcode)
          ->setChangedTime($entity
          ->getChangedTime());
      }
    }
  }
  return $entity;
}