You are here

class Importer in GatherContent 8.4

Class for handling import/update logic from GatherContent to Drupal.

Hierarchy

Expanded class hierarchy of Importer

3 files declare their use of Importer
ContentProcessor.php in src/Import/ContentProcess/ContentProcessor.php
gathercontent.drush.inc in ./gathercontent.drush.inc
Drush command to cli config import.
GcImportTestBase.php in tests/src/Kernel/GcImportTestBase.php
1 string reference to 'Importer'
gathercontent.services.yml in ./gathercontent.services.yml
gathercontent.services.yml
1 service uses Importer
gathercontent.importer in ./gathercontent.services.yml
Drupal\gathercontent\Import\Importer

File

src/Import/Importer.php, line 19

Namespace

Drupal\gathercontent\Import
View source
class Importer implements ContainerInjectionInterface {

  /**
   * Drupal GatherContent Client.
   *
   * @var \Drupal\gathercontent\DrupalGatherContentClient
   */
  protected $client;

  /**
   * The ContentProcessor fills the nodes with imported content.
   *
   * @var \Drupal\gathercontent\Import\ContentProcess\ContentProcessor
   */
  protected $contentProcessor;
  protected $eventDispatcher;

  /**
   * Importer.
   */
  public function __construct(GatherContentClientInterface $client, ContentProcessor $contentProcessor, EventDispatcherInterface $eventDispatcher) {
    $this->client = $client;
    $this->contentProcessor = $contentProcessor;
    $this->eventDispatcher = $eventDispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('gathercontent.client'), $container
      ->get('gathercontent.content_processor'), $container
      ->get('event_dispatcher'));
  }

  /**
   * Don't forget to add a finished callback and the operations array.
   */
  public static function getBasicImportBatch() {
    return [
      'title' => t('Importing'),
      'init_message' => t('Starting import'),
      'error_message' => t('An error occurred during processing'),
      'progress_message' => t('Processed @current out of @total.'),
      'progressive' => TRUE,
    ];
  }

  /**
   * Getter GatherContentClient.
   */
  public function getClient() {
    return $this->client;
  }

  /**
   * Import a single GatherContent item to Drupal.
   *
   * This function is a replacement for the old _gc_fetcher function.
   *
   * The caller (e.g. batch processes) should handle the thrown exceptions.
   *
   * @return int
   *   The ID of the imported entity.
   */
  public function import(Item $gc_item, ImportOptions $importOptions) {
    $this
      ->updateStatus($gc_item, $importOptions
      ->getNewStatus());
    $files = $this->client
      ->itemFilesGet($gc_item->id);

    // Let other modules determine the Node to update.

    /** @var \Drupal\gathercontent\Event\NodeToUpdateEvent $node_to_update_event */
    $node_to_update_event = $this->eventDispatcher
      ->dispatch(GatherContentEvents::NODE_TO_UPDATE, new NodeToUpdateEvent($gc_item, $files));
    $entity = $node_to_update_event
      ->getNode();

    // If no Node to update is set by other modules, determine the Node to
    // update or create a new Node.
    if (!$entity) {
      $entity = $this->contentProcessor
        ->createNode($gc_item, $importOptions, $files);
    }
    $this->eventDispatcher
      ->dispatch(GatherContentEvents::PRE_NODE_SAVE, new PreNodeSaveEvent($entity, $gc_item, $files));
    $entity
      ->save();
    $languages = $entity
      ->getTranslationLanguages();
    $translation_id = reset($languages)
      ->getId();
    $entity = $entity
      ->getTranslation($translation_id);
    MenuCreator::createMenu($entity, $importOptions
      ->getParentMenuItem());
    $this->eventDispatcher
      ->dispatch(GatherContentEvents::POST_NODE_SAVE, new PostNodeSaveEvent($entity, $gc_item, $files));
    return $entity
      ->id();
  }

  /**
   * Update item's status based on status id. Upload new status to GC.
   */
  protected function updateStatus(Item $item, $statusId) {
    if (!is_int($statusId)) {

      // User does not want to update status.
      return;
    }
    $status = $this->client
      ->projectStatusGet($item->projectId, $statusId);

    // Update only if status exists.
    if ($status !== NULL) {

      // Update status on GC.
      $this->client
        ->itemChooseStatusPost($item->id, $statusId);

      // Update status of item (will be uploaded to Drupal on import).
      $item->status = $status;
    }
  }

  /**
   * Decide whether a content type is translatable.
   */
  public static function isContentTypeTranslatable($contentType) {
    return \Drupal::moduleHandler()
      ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
      ->isEnabled('node', $contentType);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Importer::$client protected property Drupal GatherContent Client.
Importer::$contentProcessor protected property The ContentProcessor fills the nodes with imported content.
Importer::$eventDispatcher protected property
Importer::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Importer::getBasicImportBatch public static function Don't forget to add a finished callback and the operations array.
Importer::getClient public function Getter GatherContentClient.
Importer::import public function Import a single GatherContent item to Drupal.
Importer::isContentTypeTranslatable public static function Decide whether a content type is translatable.
Importer::updateStatus protected function Update item's status based on status id. Upload new status to GC.
Importer::__construct public function Importer.