You are here

public function ImportService::importEntityListData in Entity Share 8.3

Use data from the JSON:API to import content.

Parameters

array $entity_list_data: An array of data from a JSON:API endpoint.

Return value

int[] The list of entity IDs imported keyed by UUIDs.

Overrides ImportServiceInterface::importEntityListData

1 call to ImportService::importEntityListData()
ImportService::importFromUrl in modules/entity_share_client/src/Service/ImportService.php
Import the entities from a prepared JSON:API URL.

File

modules/entity_share_client/src/Service/ImportService.php, line 250

Class

ImportService
Class ImportService.

Namespace

Drupal\entity_share_client\Service

Code

public function importEntityListData(array $entity_list_data) {
  $imported_entity_ids = [];
  foreach (EntityShareUtility::prepareData($entity_list_data) as $entity_data) {
    foreach ($this->importProcessors[ImportProcessorInterface::STAGE_PREPARE_ENTITY_DATA] as $import_processor) {
      $import_processor
        ->prepareEntityData($this->runtimeImportContext, $entity_data);
    }
    foreach ($this->importProcessors[ImportProcessorInterface::STAGE_IS_ENTITY_IMPORTABLE] as $import_processor) {
      if (!$import_processor
        ->isEntityImportable($this->runtimeImportContext, $entity_data)) {

        // Skip the import process for this entity data.
        continue 2;
      }
    }
    foreach ($this->importProcessors[ImportProcessorInterface::STAGE_PREPARE_IMPORTABLE_ENTITY_DATA] as $import_processor) {
      $import_processor
        ->prepareImportableEntityData($this->runtimeImportContext, $entity_data);
    }
    $processed_entity = $this
      ->getProcessedEntity($entity_data);
    $imported_entity_ids[$processed_entity
      ->uuid()] = $processed_entity
      ->id();

    // Prevent infinite loop.
    // Check if we try to import an already imported entity translation.
    // We can't check this in the STAGE_IS_ENTITY_IMPORTABLE stage because we
    // need to obtain the entity ID to return it for entity reference fields.
    $processed_entity_langcode = $processed_entity
      ->language()
      ->getId();
    $processed_entity_uuid = $processed_entity
      ->uuid();
    if ($this->runtimeImportContext
      ->isEntityTranslationImported($processed_entity_langcode, $processed_entity_uuid)) {
      continue;
    }
    else {

      // Store data to prevent the entity of being re-imported.
      $this->runtimeImportContext
        ->addImportedEntity($processed_entity_langcode, $processed_entity_uuid);
    }
    foreach ($this->importProcessors[ImportProcessorInterface::STAGE_PROCESS_ENTITY] as $import_processor) {
      $import_processor
        ->processEntity($this->runtimeImportContext, $processed_entity, $entity_data);
    }
    $processed_entity
      ->save();
    foreach ($this->importProcessors[ImportProcessorInterface::STAGE_POST_ENTITY_SAVE] as $import_processor) {
      $import_processor
        ->postEntitySave($this->runtimeImportContext, $processed_entity);
    }
  }
  return $imported_entity_ids;
}