Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\migrate_tools\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
7 use Drupal\Core\Database\Connection;
|
Chris@0
|
8 use Drupal\migrate\Plugin\MigrationInterface;
|
Chris@0
|
9 use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Returns responses for migrate_tools message routes.
|
Chris@0
|
14 */
|
Chris@0
|
15 class MessageController extends ControllerBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The database service.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\Database\Connection
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $database;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Plugin manager for migration plugins.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $migrationConfigEntityPluginManager;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 */
|
Chris@0
|
34 public static function create(ContainerInterface $container) {
|
Chris@0
|
35 return new static(
|
Chris@0
|
36 $container->get('database'),
|
Chris@0
|
37 $container->get('plugin.manager.config_entity_migration')
|
Chris@0
|
38 );
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a MessageController object.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param \Drupal\Core\Database\Connection $database
|
Chris@0
|
45 * A database connection.
|
Chris@0
|
46 * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
|
Chris@0
|
47 * The plugin manager for config entity-based migrations.
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __construct(Connection $database, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
|
Chris@0
|
50 $this->database = $database;
|
Chris@0
|
51 $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Gets an array of log level classes.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @return array
|
Chris@0
|
58 * An array of log level classes.
|
Chris@0
|
59 */
|
Chris@0
|
60 public static function getLogLevelClassMap() {
|
Chris@0
|
61 return [
|
Chris@0
|
62 MigrationInterface::MESSAGE_INFORMATIONAL => 'migrate-message-4',
|
Chris@0
|
63 MigrationInterface::MESSAGE_NOTICE => 'migrate-message-3',
|
Chris@0
|
64 MigrationInterface::MESSAGE_WARNING => 'migrate-message-2',
|
Chris@0
|
65 MigrationInterface::MESSAGE_ERROR => 'migrate-message-1',
|
Chris@0
|
66 ];
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Displays a listing of migration messages.
|
Chris@0
|
71 *
|
Chris@0
|
72 * Messages are truncated at 56 chars.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param string $migration_group
|
Chris@0
|
75 * Machine name of the migration's group.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @param string $migration
|
Chris@0
|
78 * Machine name of the migration.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return array
|
Chris@0
|
81 * A render array as expected by drupal_render().
|
Chris@0
|
82 */
|
Chris@0
|
83 public function overview($migration_group, $migration) {
|
Chris@0
|
84 $rows = [];
|
Chris@0
|
85 $classes = static::getLogLevelClassMap();
|
Chris@0
|
86 /** @var MigrationInterface $migration */
|
Chris@0
|
87 $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
|
Chris@0
|
88 $source_id_field_names = array_keys($migration->getSourcePlugin()->getIds());
|
Chris@0
|
89 $column_number = 1;
|
Chris@0
|
90 foreach ($source_id_field_names as $source_id_field_name) {
|
Chris@0
|
91 $header[] = [
|
Chris@0
|
92 'data' => $source_id_field_name,
|
Chris@0
|
93 'field' => 'sourceid' . $column_number++,
|
Chris@0
|
94 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
|
Chris@0
|
95 ];
|
Chris@0
|
96 }
|
Chris@0
|
97 $header[] = [
|
Chris@0
|
98 'data' => $this->t('Severity level'),
|
Chris@0
|
99 'field' => 'level',
|
Chris@0
|
100 'class' => [RESPONSIVE_PRIORITY_LOW],
|
Chris@0
|
101 ];
|
Chris@0
|
102 $header[] = [
|
Chris@0
|
103 'data' => $this->t('Message'),
|
Chris@0
|
104 'field' => 'message',
|
Chris@0
|
105 ];
|
Chris@0
|
106
|
Chris@0
|
107 $message_table = $migration->getIdMap()->messageTableName();
|
Chris@0
|
108 $map_table = $migration->getIdMap()->mapTableName();
|
Chris@0
|
109 $query = $this->database->select($message_table, 'msg')
|
Chris@0
|
110 ->extend('\Drupal\Core\Database\Query\PagerSelectExtender')
|
Chris@0
|
111 ->extend('\Drupal\Core\Database\Query\TableSortExtender');
|
Chris@0
|
112 $query->innerJoin($map_table, 'map', 'msg.source_ids_hash=map.source_ids_hash');
|
Chris@0
|
113 $query->fields('msg');
|
Chris@0
|
114 $query->fields('map');
|
Chris@0
|
115 $result = $query
|
Chris@0
|
116 ->limit(50)
|
Chris@0
|
117 ->orderByHeader($header)
|
Chris@0
|
118 ->execute();
|
Chris@0
|
119
|
Chris@0
|
120 foreach ($result as $message_row) {
|
Chris@0
|
121 $column_number = 1;
|
Chris@0
|
122 foreach ($source_id_field_names as $source_id_field_name) {
|
Chris@0
|
123 $column_name = 'sourceid' . $column_number++;
|
Chris@0
|
124 $row[$column_name] = $message_row->$column_name;
|
Chris@0
|
125 }
|
Chris@0
|
126 $row['level'] = $message_row->level;
|
Chris@0
|
127 $row['message'] = $message_row->message;
|
Chris@0
|
128 $row['class'] = [Html::getClass('migrate-message-' . $message_row->level), $classes[$message_row->level]];
|
Chris@0
|
129 $rows[] = $row;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 $build['message_table'] = [
|
Chris@0
|
133 '#type' => 'table',
|
Chris@0
|
134 '#header' => $header,
|
Chris@0
|
135 '#rows' => $rows,
|
Chris@0
|
136 '#attributes' => ['id' => $message_table, 'class' => [$message_table]],
|
Chris@0
|
137 '#empty' => $this->t('No messages for this migration.'),
|
Chris@0
|
138 ];
|
Chris@0
|
139 $build['message_pager'] = ['#type' => 'pager'];
|
Chris@0
|
140
|
Chris@0
|
141 return $build;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 }
|