View source
<?php
namespace Drupal\migrate_tools\Controller;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\migrate_plus\Entity\MigrationGroup;
use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Datetime\DateFormatter;
class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface {
protected $currentRouteMatch;
protected $migrationConfigEntityPluginManager;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrentRouteMatch $current_route_match, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
parent::__construct($entity_type, $storage);
$this->currentRouteMatch = $current_route_match;
$this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.manager')
->getStorage($entity_type
->id()), $container
->get('current_route_match'), $container
->get('plugin.manager.config_entity_migration'));
}
protected function getEntityIds() {
$migration_group = $this->currentRouteMatch
->getParameter('migration_group');
$query = $this
->getStorage()
->getQuery()
->sort($this->entityType
->getKey('id'));
$migration_groups = MigrationGroup::loadMultiple();
if (array_key_exists($migration_group, $migration_groups)) {
$query
->condition('migration_group', $migration_group);
}
else {
$query
->notExists('migration_group');
}
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
public function buildHeader() {
$header['label'] = $this
->t('Migration');
$header['machine_name'] = $this
->t('Machine Name');
$header['status'] = $this
->t('Status');
$header['total'] = $this
->t('Total');
$header['imported'] = $this
->t('Imported');
$header['unprocessed'] = $this
->t('Unprocessed');
$header['messages'] = $this
->t('Messages');
$header['last_imported'] = $this
->t('Last Imported');
return $header;
}
public function buildRow(EntityInterface $migration_entity) {
$migration = $this->migrationConfigEntityPluginManager
->createInstance($migration_entity
->id());
$migration_group = $migration
->get('migration_group');
if (!$migration_group) {
$migration_group = 'default';
}
$route_parameters = array(
'migration_group' => $migration_group,
'migration' => $migration
->id(),
);
$row['label'] = array(
'data' => array(
'#type' => 'link',
'#title' => $migration
->label(),
'#url' => Url::fromRoute("entity.migration.overview", $route_parameters),
),
);
$row['machine_name'] = $migration
->id();
$row['status'] = $migration
->getStatusLabel();
$source_plugin = $migration
->getSourcePlugin();
$row['total'] = $source_plugin
->count();
$map = $migration
->getIdMap();
$row['imported'] = $map
->importedCount();
if ($row['total'] == -1) {
$row['total'] = $this
->t('N/A');
$row['unprocessed'] = $this
->t('N/A');
}
else {
$row['unprocessed'] = $row['total'] - $map
->processedCount();
}
$row['messages'] = array(
'data' => array(
'#type' => 'link',
'#title' => $map
->messageCount(),
'#url' => Url::fromRoute("migrate_tools.messages", $route_parameters),
),
);
$migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
$last_imported = $migrate_last_imported_store
->get($migration
->id(), FALSE);
if ($last_imported) {
$date_formatter = \Drupal::service('date.formatter');
$row['last_imported'] = $date_formatter
->format($last_imported / 1000, 'custom', 'Y-m-d H:i:s');
}
else {
$row['last_imported'] = '';
}
return $row;
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
$migration_group = $entity
->get('migration_group');
if (!$migration_group) {
$migration_group = 'default';
}
return $operations;
}
protected function addGroupParameter(Url $url, $migration_group) {
if (!$migration_group) {
$migration_group = 'default';
}
$route_parameters = $url
->getRouteParameters() + [
'migration_group' => $migration_group,
];
$url
->setRouteParameters($route_parameters);
}
}