Importer.php in GatherContent 8.4
File
src/Import/Importer.php
View source
<?php
namespace Drupal\gathercontent\Import;
use Cheppers\GatherContent\DataTypes\Item;
use Cheppers\GatherContent\GatherContentClientInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\gathercontent\Event\GatherContentEvents;
use Drupal\gathercontent\Event\NodeToUpdateEvent;
use Drupal\gathercontent\Event\PostNodeSaveEvent;
use Drupal\gathercontent\Event\PreNodeSaveEvent;
use Drupal\gathercontent\Import\ContentProcess\ContentProcessor;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Importer implements ContainerInjectionInterface {
protected $client;
protected $contentProcessor;
protected $eventDispatcher;
public function __construct(GatherContentClientInterface $client, ContentProcessor $contentProcessor, EventDispatcherInterface $eventDispatcher) {
$this->client = $client;
$this->contentProcessor = $contentProcessor;
$this->eventDispatcher = $eventDispatcher;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('gathercontent.client'), $container
->get('gathercontent.content_processor'), $container
->get('event_dispatcher'));
}
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,
];
}
public function getClient() {
return $this->client;
}
public function import(Item $gc_item, ImportOptions $importOptions) {
$this
->updateStatus($gc_item, $importOptions
->getNewStatus());
$files = $this->client
->itemFilesGet($gc_item->id);
$node_to_update_event = $this->eventDispatcher
->dispatch(GatherContentEvents::NODE_TO_UPDATE, new NodeToUpdateEvent($gc_item, $files));
$entity = $node_to_update_event
->getNode();
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();
}
protected function updateStatus(Item $item, $statusId) {
if (!is_int($statusId)) {
return;
}
$status = $this->client
->projectStatusGet($item->projectId, $statusId);
if ($status !== NULL) {
$this->client
->itemChooseStatusPost($item->id, $statusId);
$item->status = $status;
}
}
public static function isContentTypeTranslatable($contentType) {
return \Drupal::moduleHandler()
->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
->isEnabled('node', $contentType);
}
}
Classes
Name |
Description |
Importer |
Class for handling import/update logic from GatherContent to Drupal. |