You are here

public function ImportService::prepareImport in Entity Share 8.3

Prepare runtime import context and import processors.

Originally this method is meant to be protected. But as Batch API can't pass complex objects in batch's context or as batch operation's argument, and instead of creating a dedicated method for that, it has been put as a public method.

Parameters

\Drupal\entity_share_client\ImportContext $context: The import context.

Return value

bool TRUE if the import information can be gathered.

Overrides ImportServiceInterface::prepareImport

2 calls to ImportService::prepareImport()
ImportService::importChannel in modules/entity_share_client/src/Service/ImportService.php
Import all the entities on a channel.
ImportService::importEntities in modules/entity_share_client/src/Service/ImportService.php
Plugin annotation @SuppressWarnings(PHPMD . BooleanArgumentFlag);

File

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

Class

ImportService
Class ImportService.

Namespace

Drupal\entity_share_client\Service

Code

public function prepareImport(ImportContext $context) {
  $remote_id = $context
    ->getRemoteId();
  $channel_id = $context
    ->getChannelId();
  $import_config_id = $context
    ->getImportConfigId();
  $remote = NULL;
  $import_config = NULL;
  $log_variables = [];
  $log_variables['@remote_id'] = $remote_id;
  $log_variables['@channel_id'] = $channel_id;
  $log_variables['@import_config_id'] = $import_config_id;

  // Prepare import processors.
  if (is_null($import_config_id)) {
    $this->logger
      ->error('No import config ID provided.');
    $this->messenger
      ->addError($this
      ->t('No import config ID provided.'));
    return FALSE;
  }
  try {

    /** @var \Drupal\entity_share_client\Entity\ImportConfigInterface $import_config */
    $import_config = $this->entityTypeManager
      ->getStorage('import_config')
      ->load($import_config_id);
  } catch (\Exception $exception) {
    $this->logger
      ->error('Impossible to load the import config with the ID: @import_config_id', $log_variables);
    $this->messenger
      ->addError($this
      ->t('Impossible to load the import config with the ID: @import_config_id', $log_variables));
  }
  if (is_null($import_config)) {
    $this->logger
      ->error('Impossible to load the import config with the ID: @import_config_id', $log_variables);
    $this->messenger
      ->addError($this
      ->t('Impossible to load the import config with the ID: @import_config_id', $log_variables));
    return FALSE;
  }
  $this->importProcessors = $this->importConfigManipulator
    ->getImportProcessorsByStages($import_config);

  // Prepare runtimeImportContext.
  try {

    /** @var \Drupal\entity_share_client\Entity\RemoteInterface $remote */
    $remote = $this->entityTypeManager
      ->getStorage('remote')
      ->load($remote_id);
  } catch (\Exception $exception) {
    $this->logger
      ->error('Impossible to load the remote website with the ID: @remote_id', $log_variables);
    $this->messenger
      ->addError($this
      ->t('Impossible to load the remote website with the ID: @remote_id', $log_variables));
  }

  // Check that the remote exists.
  if (is_null($remote)) {
    return FALSE;
  }
  $this->runtimeImportContext
    ->setRemote($remote);

  // Check that the channel exists and that we can get the channel
  // information.
  $channels_info = $this->remoteManager
    ->getChannelsInfos($remote);
  if (!isset($channels_info[$channel_id])) {
    $this->logger
      ->error('Impossible to obtain the channel @channel_id on the remote website with the ID: @remote_id', $log_variables);
    $this->messenger
      ->addError($this
      ->t('Impossible to obtain the channel @channel_id on the remote website with the ID: @remote_id', $log_variables));
    return FALSE;
  }
  $this->runtimeImportContext
    ->setChannelId($channel_id);
  $this->runtimeImportContext
    ->setChannelLabel($channels_info[$channel_id]['label']);
  $this->runtimeImportContext
    ->setChannelUrl($channels_info[$channel_id]['url']);
  $this->runtimeImportContext
    ->setChannelUrlUuid($channels_info[$channel_id]['url_uuid']);
  $this->runtimeImportContext
    ->setChannelEntityType($channels_info[$channel_id]['channel_entity_type']);
  $this->runtimeImportContext
    ->setChannelBundle($channels_info[$channel_id]['channel_bundle']);
  $this->runtimeImportContext
    ->setChannelSearchConfiguration($channels_info[$channel_id]['search_configuration']);

  // Get field mappings.
  $this->runtimeImportContext
    ->setFieldMappings($this->remoteManager
    ->getfieldMappings($remote));
  $this->runtimeImportContext
    ->setImportService($this);
  return TRUE;
}