You are here

public function Importer::importContent in Tome 8

Imports a content entity from the source storage.

Parameters

string $entity_type_id: The entity type ID.

string $uuid: The entity UUID.

string $langcode: (optional) The langcode, if this is a translation.

Overrides ImporterInterface::importContent

File

modules/tome_sync/src/Importer.php, line 164

Class

Importer
Handles importing of content and file entities.

Namespace

Drupal\tome_sync

Code

public function importContent($entity_type_id, $uuid, $langcode = NULL) {
  $this
    ->switchToAdmin();
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $results = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadByProperties([
    $entity_type
      ->getKey('uuid') => $uuid,
  ]);
  $this
    ->isImporting(TRUE);
  $imported_entity = NULL;
  if ($langcode && $results) {
    $original_entity = reset($results);
    if ($original_entity instanceof ContentEntityInterface) {
      $translation = $this
        ->loadEntityFromStorage($entity_type, $uuid, $langcode);
      if ($original_entity
        ->hasTranslation($langcode)) {
        $original_translation = $original_entity
          ->getTranslation($langcode);
        $this
          ->copyFieldValues($translation, $original_translation);
        $original_translation
          ->save();
      }
      else {
        $original_entity
          ->addTranslation($langcode, $translation
          ->toArray());
        $original_entity
          ->save();
      }
      $imported_entity = $original_entity
        ->getTranslation($langcode);
    }
  }
  else {
    $entity = $this
      ->loadEntityFromStorage($entity_type, $uuid);
    if (!empty($results)) {
      $original_entity = reset($results);
      $this
        ->copyFieldValues($entity, $original_entity);
      $original_entity
        ->save();
      $imported_entity = $original_entity;
    }
    else {
      $entity
        ->enforceIsNew();
      $entity
        ->save();
      $imported_entity = $entity;
    }
  }
  $this
    ->isImporting(FALSE);
  if (isset($imported_entity)) {
    $event = new ContentCrudEvent($imported_entity);
    $this->eventDispatcher
      ->dispatch(TomeSyncEvents::IMPORT_CONTENT, $event);
  }
  $this
    ->switchBack();
}