content_workflow_bynder-1.0.0/content_workflow_bynder_upload/src/Export/MigrateUpdater.php
content_workflow_bynder_upload/src/Export/MigrateUpdater.php
<?php namespace Drupal\content_workflow_bynder_upload\Export; use Drupal\Core\Database\Connection; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Plugin\MigrationPluginManagerInterface; use Drupal\migrate\Row; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class for handling import/update logic from Content Workflow to Drupal. */ class MigrateUpdater implements ContainerInjectionInterface { /** * Migration service. * * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface */ protected $migrationService; /** * Database. * * @var \Drupal\Core\Database\Connection */ protected $database; /** * DI Content Workflow 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['cwbIds']); } } protected function processMigration(string $migrationId, array $cwbIds) { /** @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($cwbIds[$sourceId])) { $source->next(); continue; } $this->processEntity($migration, $cwbIds[$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('content_workflow_bynder_entity_mapping') ->fields('content_workflow_bynder_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('content_workflow_bynder_entity_mapping') ->fields([ 'entity_id' => $entity->id(), 'entity_type' => $entity->getEntityTypeId(), 'cwb_id' => $sourceId, 'migration_id' => $migration->id(), 'langcode' => $language->getId(), ]) ->execute(); } if ($language->isDefault()) { continue; } $destinationIds[] = $language->getId(); $idMap->saveIdMapping($row, $destinationIds); } } }