class MigrateUpdater in GatherContent 8.5
Class for handling import/update logic from GatherContent to Drupal.
Hierarchy
- class \Drupal\gathercontent_upload\Export\MigrateUpdater implements ContainerInjectionInterface
Expanded class hierarchy of MigrateUpdater
File
- gathercontent_upload/
src/ Export/ MigrateUpdater.php, line 16
Namespace
Drupal\gathercontent_upload\ExportView source
class MigrateUpdater implements ContainerInjectionInterface {
/**
* Migration service.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationService;
/**
* Database.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* DI GatherContent Client.
*
* @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migrationService
* @param \Drupal\Core\Database\Connection $database
*/
public function __construct(MigrationPluginManagerInterface $migrationService, Connection $database) {
$this->migrationService = $migrationService;
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.migration'), $container
->get('database'));
}
/**
* Update/create Migrate API's ID Mapping.
*
* @param array $context
* Batch context.
*/
public function updateIdMap(array $context = []) {
if (empty($context['results']['mappings'])) {
return;
}
foreach ($context['results']['mappings'] as $mapping) {
$this
->processMappings($mapping);
}
}
protected function processMappings(array $mapping) {
$migrationIds = $mapping['mapping']
->getMigrations();
foreach ($migrationIds as $migrationId) {
$this
->processMigration($migrationId, $mapping['gcIds']);
}
}
protected function processMigration(string $migrationId, array $gcIds) {
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = $this->migrationService
->createInstance($migrationId);
$source = $migration
->getSourcePlugin();
$source
->rewind();
while ($source
->valid()) {
$row = $source
->current();
$sourceId = $row
->getSourceIdValues();
$sourceId = $sourceId['id'];
if (empty($gcIds[$sourceId])) {
$source
->next();
continue;
}
$this
->processEntity($migration, $gcIds[$sourceId], $row);
$source
->next();
}
}
protected function processEntity(MigrationInterface $migration, array $entities, Row $row) {
$destinationConfiguration = $migration
->getDestinationConfiguration();
$plugin = explode(':', $destinationConfiguration['plugin']);
$idMap = $migration
->getIdMap();
/** @var \Drupal\Core\Entity\EntityInterface $entity */
foreach ($entities as $entity) {
if ($plugin[1] !== $entity
->getEntityTypeId()) {
continue;
}
$destinationIds = [
$entity
->id(),
];
if ($entity
->getEntityTypeId() === 'paragraph') {
$destinationIds[] = $entity
->getRevisionId();
}
$idMap
->saveIdMapping($row, $destinationIds);
$this
->processLanguages($migration, $entity, $row, $destinationIds);
}
}
protected function processLanguages(MigrationInterface $migration, EntityInterface $entity, Row $row, array $destinationIds) {
$idMap = $migration
->getIdMap();
$languages = $entity
->getTranslationLanguages();
$sourceId = $row
->getSourceIdValues();
$sourceId = $sourceId['id'];
foreach ($languages as $language) {
$result = $this->database
->select('gathercontent_entity_mapping')
->fields('gathercontent_entity_mapping', [
'entity_id',
'entity_type',
])
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->condition('langcode', $language
->getId())
->execute()
->fetchAll();
if (empty($result)) {
$this->database
->insert('gathercontent_entity_mapping')
->fields([
'entity_id' => $entity
->id(),
'entity_type' => $entity
->getEntityTypeId(),
'gc_id' => $sourceId,
'migration_id' => $migration
->id(),
'langcode' => $language
->getId(),
])
->execute();
}
if ($language
->isDefault()) {
continue;
}
$destinationIds[] = $language
->getId();
$idMap
->saveIdMapping($row, $destinationIds);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateUpdater:: |
protected | property | Database. | |
MigrateUpdater:: |
protected | property | Migration service. | |
MigrateUpdater:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
MigrateUpdater:: |
protected | function | ||
MigrateUpdater:: |
protected | function | ||
MigrateUpdater:: |
protected | function | ||
MigrateUpdater:: |
protected | function | ||
MigrateUpdater:: |
public | function | Update/create Migrate API's ID Mapping. | |
MigrateUpdater:: |
public | function | DI GatherContent Client. |