Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Controller / MigrationListBuilder.php
1 <?php
2
3 namespace Drupal\migrate_tools\Controller;
4
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;
15 use Drupal\Core\Url;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * Provides a listing of migration entities in a given group.
20  *
21  * @package Drupal\migrate_tools\Controller
22  *
23  * @ingroup migrate_tools
24  */
25 class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface {
26
27   /**
28    * Default object for current_route_match service.
29    *
30    * @var \Drupal\Core\Routing\CurrentRouteMatch
31    */
32   protected $currentRouteMatch;
33
34   /**
35    * Plugin manager for migration plugins.
36    *
37    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
38    */
39   protected $migrationPluginManager;
40
41   /**
42    * The logger service.
43    *
44    * @var \Drupal\Core\Logger\LoggerChannelInterface
45    */
46   protected $logger;
47
48   /**
49    * Constructs a new EntityListBuilder object.
50    *
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
60    *   The logger service.
61    */
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;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
73     return new static(
74       $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')
79     );
80   }
81
82   /**
83    * Retrieve the migrations belonging to the appropriate group.
84    *
85    * @return array
86    *   An array of entity IDs.
87    */
88   protected function getEntityIds() {
89     $migration_group = $this->currentRouteMatch->getParameter('migration_group');
90
91     $query = $this->getStorage()->getQuery()
92       ->sort($this->entityType->getKey('id'));
93
94     $migration_groups = MigrationGroup::loadMultiple();
95
96     if (array_key_exists($migration_group, $migration_groups)) {
97       $query->condition('migration_group', $migration_group);
98     }
99     else {
100       $query->notExists('migration_group');
101     }
102     // Only add the pager if a limit is specified.
103     if ($this->limit) {
104       $query->pager($this->limit);
105     }
106     return $query->execute();
107   }
108
109   /**
110    * Builds the header row for the entity listing.
111    *
112    * @return array
113    *   A render array structure of header strings.
114    *
115    * @see \Drupal\Core\Entity\EntityListController::render()
116    */
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');
127     return $header;
128   }
129
130   /**
131    * Builds a row for a migration plugin.
132    *
133    * @param \Drupal\Core\Entity\EntityInterface $migration_entity
134    *   The migration plugin for which to build the row.
135    *
136    * @return array|null
137    *   A render array of the table row for displaying the plugin information.
138    *
139    * @see \Drupal\Core\Entity\EntityListController::render()
140    */
141   public function buildRow(EntityInterface $migration_entity) {
142     try {
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';
148       }
149       $route_parameters = [
150         'migration_group' => $migration_group,
151         'migration' => $migration->id(),
152       ];
153       $row['label'] = [
154         'data' => [
155           '#type' => 'link',
156           '#title' => $migration->label(),
157           '#url' => Url::fromRoute("entity.migration.overview", $route_parameters),
158         ],
159       ];
160       $row['machine_name'] = $migration->id();
161       $row['status'] = $migration->getStatusLabel();
162     }
163     catch (PluginException $e) {
164       $this->logger->warning('Migration entity id %id is malformed: %orig', ['%id' => $migration_entity->id(), '%orig' => $e->getMessage()]);
165       return NULL;
166     }
167
168     try {
169       // Derive the stats.
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');
178       }
179       else {
180         $row['unprocessed'] = $row['total'] - $map->processedCount();
181       }
182       $row['messages'] = [
183         'data' => [
184           '#type' => 'link',
185           '#title' => $map->messageCount(),
186           '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters),
187         ],
188       ];
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');
196       }
197       else {
198         $row['last_imported'] = '';
199       }
200
201       $row['operations']['data'] = [
202         '#type' => 'dropbutton',
203         '#links' => [
204           'simple_form' => [
205             'title' => $this->t('Execute'),
206             'url' => Url::fromRoute('migrate_tools.execute', [
207               'migration_group' => $migration_group,
208               'migration' => $migration->id(),
209             ]),
210           ],
211         ],
212       ];
213     }
214     catch (PluginException $e) {
215       // Derive the stats.
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');
223     }
224
225     return $row;
226   }
227
228   /**
229    * Add group route parameter.
230    *
231    * @param \Drupal\Core\Url $url
232    *   The URL associated with an operation.
233    * @param string $migration_group
234    *   The migration's parent group.
235    */
236   protected function addGroupParameter(Url $url, $migration_group) {
237     if (!$migration_group) {
238       $migration_group = 'default';
239     }
240     $route_parameters = $url->getRouteParameters() + ['migration_group' => $migration_group];
241     $url->setRouteParameters($route_parameters);
242   }
243
244 }