MigrationController.php in Workbench Moderation to Content Moderation 8.2
File
src/MigrationController.php
View source
<?php
namespace Drupal\wbm2cm;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class MigrationController {
use StringTranslationTrait;
protected $migrationManager;
protected $eventDispatcher;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $entityFieldManager;
public function __construct(MigrationPluginManagerInterface $migration_manager, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $translation) {
$this->migrationManager = $migration_manager;
$this->eventDispatcher = $event_dispatcher;
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this
->setStringTranslation($translation);
}
protected function execute(MigrationInterface $migration) {
$executable = new MigrateExecutable($migration, new MigrateMessage(), $this->eventDispatcher);
$executable
->import();
}
public function executeStep($which) {
$migrations = $this->migrationManager
->createInstances("wbm2cm_{$which}");
array_walk($migrations, [
$this,
'execute',
]);
return $migrations;
}
public function executeStepWithCounts($which) {
$counts = [];
foreach ($this
->executeStep($which) as $migration) {
$entity_type = $migration
->getDerivativeId();
$counts[$entity_type] = $migration
->getIdMap()
->importedCount();
}
return $counts;
}
public function executeStepWithMessages($which) {
$messages = [];
foreach ($this
->executeStepWithCounts($which) as $entity_type => $count) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type);
$messages[] = $this
->t('Processed @count @items.', [
'@count' => $count,
'@items' => $this
->formatPlural($count, $entity_type
->getSingularLabel(), $entity_type
->getPluralLabel()),
]);
}
return array_map('strval', $messages);
}
public function getOverriddenFields() {
$overridden = [];
$bundle_info = $this->entityTypeBundleInfo
->getAllBundleInfo();
foreach ($bundle_info as $entity_type_id => $bundles) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
continue;
}
foreach (array_keys($bundles) as $bundle) {
$fields = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
if (isset($fields['moderation_state']) && $fields['moderation_state'] instanceof BaseFieldOverride) {
array_push($overridden, $fields['moderation_state']);
}
}
}
return $overridden;
}
}