annotate modules/contrib/migrate_tools/src/Controller/MigrationListBuilder.php @ 5:c69a71b4f40f

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