View source
<?php
namespace Drupal\migrate_tools\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate_plus\Entity\MigrationGroupInterface;
use Drupal\migrate_plus\Entity\MigrationInterface as MigratePlusMigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MessageController extends ControllerBase {
protected $database;
protected $migrationPluginManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('plugin.manager.migration'));
}
public function __construct(Connection $database, MigrationPluginManagerInterface $migration_plugin_manager) {
$this->database = $database;
$this->migrationPluginManager = $migration_plugin_manager;
}
public static function getLogLevelClassMap() {
return [
MigrationInterface::MESSAGE_INFORMATIONAL => 'migrate-message-4',
MigrationInterface::MESSAGE_NOTICE => 'migrate-message-3',
MigrationInterface::MESSAGE_WARNING => 'migrate-message-2',
MigrationInterface::MESSAGE_ERROR => 'migrate-message-1',
];
}
public function overview(MigrationGroupInterface $migration_group, MigratePlusMigrationInterface $migration) {
$rows = [];
$classes = static::getLogLevelClassMap();
$migration_plugin = $this->migrationPluginManager
->createInstance($migration
->id(), $migration
->toArray());
$source_id_field_names = array_keys($migration_plugin
->getSourcePlugin()
->getIds());
$column_number = 1;
foreach ($source_id_field_names as $source_id_field_name) {
$header[] = [
'data' => $source_id_field_name,
'field' => 'sourceid' . $column_number++,
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
];
}
$header[] = [
'data' => $this
->t('Severity level'),
'field' => 'level',
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
$header[] = [
'data' => $this
->t('Message'),
'field' => 'message',
];
$result = [];
$message_table = $migration_plugin
->getIdMap()
->messageTableName();
if ($this->database
->schema()
->tableExists($message_table)) {
$map_table = $migration_plugin
->getIdMap()
->mapTableName();
$query = $this->database
->select($message_table, 'msg')
->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
$query
->innerJoin($map_table, 'map', 'msg.source_ids_hash=map.source_ids_hash');
$query
->fields('msg');
$query
->fields('map');
$result = $query
->limit(50)
->orderByHeader($header)
->execute();
}
foreach ($result as $message_row) {
$column_number = 1;
foreach ($source_id_field_names as $source_id_field_name) {
$column_name = 'sourceid' . $column_number++;
$row[$column_name] = $message_row->{$column_name};
}
$row['level'] = $message_row->level;
$row['message'] = $message_row->message;
$row['class'] = [
Html::getClass('migrate-message-' . $message_row->level),
$classes[$message_row->level],
];
$rows[] = $row;
}
$build['message_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'id' => $message_table,
'class' => [
$message_table,
],
],
'#empty' => $this
->t('No messages for this migration.'),
];
$build['message_pager'] = [
'#type' => 'pager',
];
return $build;
}
public function title(MigrationGroupInterface $migration_group, MigratePlusMigrationInterface $migration) {
return $this
->t('Messages of %migration', [
'%migration' => $migration
->label(),
]);
}
}