3 namespace Drupal\migrate_tools\Controller;
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
7 use Drupal\Core\Entity\EntityHandlerInterface;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Logger\LoggerChannelInterface;
12 use Drupal\Core\Routing\CurrentRouteMatch;
13 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
14 use Drupal\migrate_plus\Entity\MigrationGroup;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
19 * Provides a listing of migration entities in a given group.
21 * @package Drupal\migrate_tools\Controller
23 * @ingroup migrate_tools
25 class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface {
28 * Default object for current_route_match service.
30 * @var \Drupal\Core\Routing\CurrentRouteMatch
32 protected $currentRouteMatch;
35 * Plugin manager for migration plugins.
37 * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
39 protected $migrationPluginManager;
44 * @var \Drupal\Core\Logger\LoggerChannelInterface
49 * Constructs a new EntityListBuilder object.
51 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
52 * The entity type definition.
53 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
54 * The entity storage class.
55 * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
56 * The current route match service.
57 * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
58 * The plugin manager for config entity-based migrations.
59 * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
62 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrentRouteMatch $current_route_match, MigrationPluginManagerInterface $migration_plugin_manager, LoggerChannelInterface $logger) {
63 parent::__construct($entity_type, $storage);
64 $this->currentRouteMatch = $current_route_match;
65 $this->migrationPluginManager = $migration_plugin_manager;
66 $this->logger = $logger;
72 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
75 $container->get('entity.manager')->getStorage($entity_type->id()),
76 $container->get('current_route_match'),
77 $container->get('plugin.manager.migration'),
78 $container->get('logger.channel.migrate_tools')
83 * Retrieve the migrations belonging to the appropriate group.
86 * An array of entity IDs.
88 protected function getEntityIds() {
89 $migration_group = $this->currentRouteMatch->getParameter('migration_group');
91 $query = $this->getStorage()->getQuery()
92 ->sort($this->entityType->getKey('id'));
94 $migration_groups = MigrationGroup::loadMultiple();
96 if (array_key_exists($migration_group, $migration_groups)) {
97 $query->condition('migration_group', $migration_group);
100 $query->notExists('migration_group');
102 // Only add the pager if a limit is specified.
104 $query->pager($this->limit);
106 return $query->execute();
110 * Builds the header row for the entity listing.
113 * A render array structure of header strings.
115 * @see \Drupal\Core\Entity\EntityListController::render()
117 public function buildHeader() {
118 $header['label'] = $this->t('Migration');
119 $header['machine_name'] = $this->t('Machine Name');
120 $header['status'] = $this->t('Status');
121 $header['total'] = $this->t('Total');
122 $header['imported'] = $this->t('Imported');
123 $header['unprocessed'] = $this->t('Unprocessed');
124 $header['messages'] = $this->t('Messages');
125 $header['last_imported'] = $this->t('Last Imported');
126 $header['operations'] = $this->t('Operations');
131 * Builds a row for a migration plugin.
133 * @param \Drupal\Core\Entity\EntityInterface $migration_entity
134 * The migration plugin for which to build the row.
137 * A render array of the table row for displaying the plugin information.
139 * @see \Drupal\Core\Entity\EntityListController::render()
141 public function buildRow(EntityInterface $migration_entity) {
143 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
144 $migration = $this->migrationPluginManager->createInstance($migration_entity->id());
145 $migration_group = $migration->get('migration_group');
146 if (!$migration_group) {
147 $migration_group = 'default';
149 $route_parameters = [
150 'migration_group' => $migration_group,
151 'migration' => $migration->id(),
156 '#title' => $migration->label(),
157 '#url' => Url::fromRoute("entity.migration.overview", $route_parameters),
160 $row['machine_name'] = $migration->id();
161 $row['status'] = $migration->getStatusLabel();
163 catch (PluginException $e) {
164 $this->logger->warning('Migration entity id %id is malformed: %orig', ['%id' => $migration_entity->id(), '%orig' => $e->getMessage()]);
170 $source_plugin = $migration->getSourcePlugin();
171 $row['total'] = $source_plugin->count();
172 $map = $migration->getIdMap();
173 $row['imported'] = $map->importedCount();
174 // -1 indicates uncountable sources.
175 if ($row['total'] == -1) {
176 $row['total'] = $this->t('N/A');
177 $row['unprocessed'] = $this->t('N/A');
180 $row['unprocessed'] = $row['total'] - $map->processedCount();
185 '#title' => $map->messageCount(),
186 '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters),
189 $migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
190 $last_imported = $migrate_last_imported_store->get($migration->id(), FALSE);
191 if ($last_imported) {
192 /** @var \Drupal\Core\Datetime\DateFormatter $date_formatter */
193 $date_formatter = \Drupal::service('date.formatter');
194 $row['last_imported'] = $date_formatter->format($last_imported / 1000,
195 'custom', 'Y-m-d H:i:s');
198 $row['last_imported'] = '';
201 $row['operations']['data'] = [
202 '#type' => 'dropbutton',
205 'title' => $this->t('Execute'),
206 'url' => Url::fromRoute('migrate_tools.execute', [
207 'migration_group' => $migration_group,
208 'migration' => $migration->id(),
214 catch (PluginException $e) {
216 $row['status'] = $this->t('No data found');
217 $row['total'] = $this->t('N/A');
218 $row['imported'] = $this->t('N/A');
219 $row['unprocessed'] = $this->t('N/A');
220 $row['messages'] = $this->t('N/A');
221 $row['last_imported'] = $this->t('N/A');
222 $row['operations'] = $this->t('N/A');
229 * Add group route parameter.
231 * @param \Drupal\Core\Url $url
232 * The URL associated with an operation.
233 * @param string $migration_group
234 * The migration's parent group.
236 protected function addGroupParameter(Url $url, $migration_group) {
237 if (!$migration_group) {
238 $migration_group = 'default';
240 $route_parameters = $url->getRouteParameters() + ['migration_group' => $migration_group];
241 $url->setRouteParameters($route_parameters);