3 namespace Drupal\migrate_tools\Controller;
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityHandlerInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Routing\CurrentRouteMatch;
11 use Drupal\migrate_plus\Entity\MigrationGroup;
12 use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Drupal\Core\Datetime\DateFormatter;
18 * Provides a listing of migration entities in a given group.
20 * @package Drupal\migrate_tools\Controller
22 * @ingroup migrate_tools
24 class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface {
27 * Default object for current_route_match service.
29 * @var \Drupal\Core\Routing\CurrentRouteMatch
31 protected $currentRouteMatch;
34 * Plugin manager for migration plugins.
36 * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
38 protected $migrationConfigEntityPluginManager;
41 * Constructs a new EntityListBuilder object.
43 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
44 * The entity type definition.
45 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
46 * The entity storage class.
47 * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
48 * The current route match service.
49 * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
50 * The plugin manager for config entity-based migrations.
52 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrentRouteMatch $current_route_match, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
53 parent::__construct($entity_type, $storage);
54 $this->currentRouteMatch = $current_route_match;
55 $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
61 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
64 $container->get('entity.manager')->getStorage($entity_type->id()),
65 $container->get('current_route_match'),
66 $container->get('plugin.manager.config_entity_migration')
71 * Retrieve the migrations belonging to the appropriate group.
74 * An array of entity IDs.
76 protected function getEntityIds() {
77 $migration_group = $this->currentRouteMatch->getParameter('migration_group');
79 $query = $this->getStorage()->getQuery()
80 ->sort($this->entityType->getKey('id'));
82 $migration_groups = MigrationGroup::loadMultiple();
84 if (array_key_exists($migration_group, $migration_groups)) {
85 $query->condition('migration_group', $migration_group);
88 $query->notExists('migration_group');
90 // Only add the pager if a limit is specified.
92 $query->pager($this->limit);
94 return $query->execute();
98 * Builds the header row for the entity listing.
101 * A render array structure of header strings.
103 * @see Drupal\Core\Entity\EntityListController::render()
105 public function buildHeader() {
106 $header['label'] = $this->t('Migration');
107 $header['machine_name'] = $this->t('Machine Name');
108 $header['status'] = $this->t('Status');
109 $header['total'] = $this->t('Total');
110 $header['imported'] = $this->t('Imported');
111 $header['unprocessed'] = $this->t('Unprocessed');
112 $header['messages'] = $this->t('Messages');
113 $header['last_imported'] = $this->t('Last Imported');
114 return $header; // + parent::buildHeader();
118 * Builds a row for a migration plugin.
120 * @param \Drupal\Core\Entity\EntityInterface $migration_entity
121 * The migration plugin for which to build the row.
124 * A render array of the table row for displaying the plugin information.
126 * @see Drupal\Core\Entity\EntityListController::render()
128 public function buildRow(EntityInterface $migration_entity) {
129 $migration = $this->migrationConfigEntityPluginManager->createInstance($migration_entity->id());
130 $migration_group = $migration->get('migration_group');
131 if (!$migration_group) {
132 $migration_group = 'default';
134 $route_parameters = array(
135 'migration_group' => $migration_group,
136 'migration' => $migration->id(),
138 $row['label'] = array(
141 '#title' => $migration->label(),
142 '#url' => Url::fromRoute("entity.migration.overview", $route_parameters),
145 $row['machine_name'] = $migration->id();
146 $row['status'] = $migration->getStatusLabel();
149 $source_plugin = $migration->getSourcePlugin();
150 $row['total'] = $source_plugin->count();
151 $map = $migration->getIdMap();
152 $row['imported'] = $map->importedCount();
153 // -1 indicates uncountable sources.
154 if ($row['total'] == -1) {
155 $row['total'] = $this->t('N/A');
156 $row['unprocessed'] = $this->t('N/A');
159 $row['unprocessed'] = $row['total'] - $map->processedCount();
161 $row['messages'] = array(
164 '#title' => $map->messageCount(),
165 '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters),
168 $migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
169 $last_imported = $migrate_last_imported_store->get($migration->id(), FALSE);
170 if ($last_imported) {
171 /** @var DateFormatter $date_formatter */
172 $date_formatter = \Drupal::service('date.formatter');
173 $row['last_imported'] = $date_formatter->format($last_imported / 1000,
174 'custom', 'Y-m-d H:i:s');
177 $row['last_imported'] = '';
179 return $row; // + parent::buildRow($migration_entity);
185 public function getDefaultOperations(EntityInterface $entity) {
186 $operations = parent::getDefaultOperations($entity);
187 $migration_group = $entity->get('migration_group');
188 if (!$migration_group) {
189 $migration_group = 'default';
191 // $this->addGroupParameter($operations['edit']['url'], $migration_group);
192 // $this->addGroupParameter($operations['delete']['url'], $migration_group);
197 * @param \Drupal\Core\Url $url
198 * The URL associated with an operation.
200 * @param $migration_group
201 * The migration's parent group.
203 protected function addGroupParameter(Url $url, $migration_group) {
204 if (!$migration_group) {
205 $migration_group = 'default';
207 $route_parameters = $url->getRouteParameters() + ['migration_group' => $migration_group];
208 $url->setRouteParameters($route_parameters);