class Importer in GatherContent 8.4
Class for handling import/update logic from GatherContent to Drupal.
Hierarchy
- class \Drupal\gathercontent\Import\Importer implements ContainerInjectionInterface
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'
1 service uses Importer
File
- src/
Import/ Importer.php, line 19
Namespace
Drupal\gathercontent\ImportView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Importer:: |
protected | property | Drupal GatherContent Client. | |
Importer:: |
protected | property | The ContentProcessor fills the nodes with imported content. | |
Importer:: |
protected | property | ||
Importer:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
Importer:: |
public static | function | Don't forget to add a finished callback and the operations array. | |
Importer:: |
public | function | Getter GatherContentClient. | |
Importer:: |
public | function | Import a single GatherContent item to Drupal. | |
Importer:: |
public static | function | Decide whether a content type is translatable. | |
Importer:: |
protected | function | Update item's status based on status id. Upload new status to GC. | |
Importer:: |
public | function | Importer. |