Mercurial > hg > isophonics-drupal-site
comparison modules/contrib/migrate_tools/src/Controller/MigrationListBuilder.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\migrate_tools\Controller; | |
4 | |
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\Plugin\MigrationConfigEntityPluginManager; | |
12 use Drupal\Core\Url; | |
13 use Symfony\Component\DependencyInjection\ContainerInterface; | |
14 use Drupal\Core\Datetime\DateFormatter; | |
15 | |
16 /** | |
17 * Provides a listing of migration entities in a given group. | |
18 * | |
19 * @package Drupal\migrate_tools\Controller | |
20 * | |
21 * @ingroup migrate_tools | |
22 */ | |
23 class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface { | |
24 | |
25 /** | |
26 * Default object for current_route_match service. | |
27 * | |
28 * @var \Drupal\Core\Routing\CurrentRouteMatch | |
29 */ | |
30 protected $currentRouteMatch; | |
31 | |
32 /** | |
33 * Plugin manager for migration plugins. | |
34 * | |
35 * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager | |
36 */ | |
37 protected $migrationConfigEntityPluginManager; | |
38 | |
39 /** | |
40 * Constructs a new EntityListBuilder object. | |
41 * | |
42 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type | |
43 * The entity type definition. | |
44 * @param \Drupal\Core\Entity\EntityStorageInterface $storage | |
45 * The entity storage class. | |
46 * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match | |
47 * The current route match service. | |
48 * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager | |
49 * The plugin manager for config entity-based migrations. | |
50 */ | |
51 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrentRouteMatch $current_route_match, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) { | |
52 parent::__construct($entity_type, $storage); | |
53 $this->currentRouteMatch = $current_route_match; | |
54 $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager; | |
55 } | |
56 | |
57 /** | |
58 * {@inheritdoc} | |
59 */ | |
60 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { | |
61 return new static( | |
62 $entity_type, | |
63 $container->get('entity.manager')->getStorage($entity_type->id()), | |
64 $container->get('current_route_match'), | |
65 $container->get('plugin.manager.config_entity_migration') | |
66 ); | |
67 } | |
68 | |
69 /** | |
70 * Builds the header row for the entity listing. | |
71 * | |
72 * @return array | |
73 * A render array structure of header strings. | |
74 * | |
75 * @see Drupal\Core\Entity\EntityListController::render() | |
76 */ | |
77 public function buildHeader() { | |
78 $header['label'] = $this->t('Migration'); | |
79 $header['machine_name'] = $this->t('Machine Name'); | |
80 $header['status'] = $this->t('Status'); | |
81 $header['total'] = $this->t('Total'); | |
82 $header['imported'] = $this->t('Imported'); | |
83 $header['unprocessed'] = $this->t('Unprocessed'); | |
84 $header['messages'] = $this->t('Messages'); | |
85 $header['last_imported'] = $this->t('Last Imported'); | |
86 return $header; // + parent::buildHeader(); | |
87 } | |
88 | |
89 /** | |
90 * Builds a row for a migration plugin. | |
91 * | |
92 * @param \Drupal\Core\Entity\EntityInterface $migration | |
93 * The migration plugin for which to build the row. | |
94 * | |
95 * @return array | |
96 * A render array of the table row for displaying the plugin information. | |
97 * | |
98 * @see Drupal\Core\Entity\EntityListController::render() | |
99 */ | |
100 public function buildRow(EntityInterface $migration_entity) { | |
101 $migration = $this->migrationConfigEntityPluginManager->createInstance($migration_entity->id()); | |
102 $row['label'] = $migration->label(); | |
103 $row['machine_name'] = $migration->id(); | |
104 $row['status'] = $migration->getStatusLabel(); | |
105 | |
106 // Derive the stats. | |
107 $source_plugin = $migration->getSourcePlugin(); | |
108 $row['total'] = $source_plugin->count(); | |
109 $map = $migration->getIdMap(); | |
110 $row['imported'] = $map->importedCount(); | |
111 // -1 indicates uncountable sources. | |
112 if ($row['total'] == -1) { | |
113 $row['total'] = $this->t('N/A'); | |
114 $row['unprocessed'] = $this->t('N/A'); | |
115 } | |
116 else { | |
117 $row['unprocessed'] = $row['total'] - $map->processedCount(); | |
118 } | |
119 $migration_group = $migration->get('migration_group'); | |
120 if (!$migration_group) { | |
121 $migration_group = 'default'; | |
122 } | |
123 $route_parameters = array( | |
124 'migration_group' => $migration_group, | |
125 'migration' => $migration->id() | |
126 ); | |
127 $row['messages'] = array( | |
128 'data' => array( | |
129 '#type' => 'link', | |
130 '#title' => $map->messageCount(), | |
131 '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters), | |
132 ), | |
133 ); | |
134 $migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported'); | |
135 $last_imported = $migrate_last_imported_store->get($migration->id(), FALSE); | |
136 if ($last_imported) { | |
137 /** @var DateFormatter $date_formatter */ | |
138 $date_formatter = \Drupal::service('date.formatter'); | |
139 $row['last_imported'] = $date_formatter->format($last_imported / 1000, | |
140 'custom', 'Y-m-d H:i:s'); | |
141 } | |
142 else { | |
143 $row['last_imported'] = ''; | |
144 } | |
145 return $row + parent::buildRow($migration_entity); | |
146 } | |
147 | |
148 /** | |
149 * {@inheritdoc} | |
150 */ | |
151 public function getDefaultOperations(EntityInterface $entity) { | |
152 $operations = parent::getDefaultOperations($entity); | |
153 $migration_group = $entity->get('migration_group'); | |
154 if (!$migration_group) { | |
155 $migration_group = 'default'; | |
156 } | |
157 // $this->addGroupParameter($operations['edit']['url'], $migration_group); | |
158 // $this->addGroupParameter($operations['delete']['url'], $migration_group); | |
159 return $operations; | |
160 } | |
161 | |
162 /** | |
163 * @param \Drupal\Core\Url $url | |
164 * The URL associated with an operation. | |
165 * | |
166 * @param $migration_group | |
167 * The migration's parent group. | |
168 */ | |
169 protected function addGroupParameter(Url $url, $migration_group) { | |
170 if (!$migration_group) { | |
171 $migration_group = 'default'; | |
172 } | |
173 $route_parameters = $url->getRouteParameters() + ['migration_group' => $migration_group]; | |
174 $url->setRouteParameters($route_parameters); | |
175 } | |
176 | |
177 } |