You are here

final public function EntityProcessorBase::import in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::import()
  2. 3.x src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::import()

Create or update entity with data :.

Parameters

array $dataToImport: The data to import.

File

src/Processors/Entity/EntityProcessorBase.php, line 197

Class

EntityProcessorBase
The entity processor base.

Namespace

Drupal\content_synchronizer\Processors\Entity

Code

public final function import(array $dataToImport) {
  $gid = $dataToImport[ExportEntityWriter::FIELD_GID];
  $uuid = $dataToImport[ExportEntityWriter::FIELD_UUID];

  // If the entity has already been imported then we don't have to do it again.
  $import = ImportProcessor::getCurrentImportProcessor()
    ->getImport();
  if ($import
    ->gidHasAlreadyBeenImported($gid)) {
    return $this
      ->getGlobalReferenceManager()
      ->getEntityByGid($gid);
  }

  // Tag as importing.
  ImportProcessor::getCurrentImportProcessor()
    ->getImport()
    ->tagHasImporting($gid);

  // Get the previous entity by gid.
  if ($existingEntity = $this
    ->getGlobalReferenceManager()
    ->getExistingEntityByGidAndUuid($gid, $uuid)) {
    if ($existingEntity) {
      $backup = clone $existingEntity;
    }

    /** @var \Drupal\Core\Entity\Entity $entity */
    if ($entity = $this
      ->getEntityToImport($dataToImport, $existingEntity)) {
      $this
        ->setChangedTime($entity, $dataToImport);
      $this
        ->getEntityPublisher()
        ->saveEntity($entity, $gid, $backup, $dataToImport);
    }
  }
  else {

    /** @var \Drupal\Core\Entity\Entity $entity */
    if ($entity = $this
      ->getEntityToImport($dataToImport, NULL)) {
      $this
        ->checkBundle($entity, TRUE);
      $this
        ->setChangedTime($entity, $dataToImport);
      $this
        ->getEntityPublisher()
        ->saveEntity($entity, $gid, NULL, $dataToImport);
      $this
        ->getGlobalReferenceManager()
        ->createGlobalEntityByImportingEntityAndGid($entity, $gid);
    }
  }
  if ($entity) {

    // Tag as imported.
    ImportProcessor::getCurrentImportProcessor()
      ->getImport()
      ->tagHasImported($gid);
    $this
      ->onEntityImported($gid, $entity);
  }
  return $entity;
}