function MigrateMessageController::details

Same name in other branches
  1. 11.x core/modules/migrate/src/Controller/MigrateMessageController.php \Drupal\migrate\Controller\MigrateMessageController::details()

Displays a listing of migration messages for the given migration ID.

Parameters

string $migration_id: A migration ID.

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

array A render array.

1 string reference to 'MigrateMessageController::details'
migrate.routing.yml in core/modules/migrate/migrate.routing.yml
core/modules/migrate/migrate.routing.yml

File

core/modules/migrate/src/Controller/MigrateMessageController.php, line 117

Class

MigrateMessageController
Provides controller methods for the Message form.

Namespace

Drupal\migrate\Controller

Code

public function details(string $migration_id, Request $request) : array {
    
    /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
    $migration = $this->migrationPluginManager
        ->createInstance($migration_id);
    if (!$migration) {
        throw new NotFoundHttpException();
    }
    // Get the map and message table names.
    $map_table = $migration->getIdMap()
        ->mapTableName();
    $message_table = $migration->getIdMap()
        ->messageTableName();
    // If the map table does not exist then do not continue.
    if (!$this->database
        ->schema()
        ->tableExists($map_table)) {
        throw new NotFoundHttpException();
    }
    // If there is a map table but no message table display an error.
    if (!$this->database
        ->schema()
        ->tableExists($message_table)) {
        $this->messenger()
            ->addError($this->t('The message table is missing for this migration.'));
        return [];
    }
    // Create the column header names.
    $header = [];
    $source_plugin = $migration->getSourcePlugin();
    // Create the column header names from the source plugin fields() method.
    // Fallback to the source_id name when the source ID is missing from
    // fields() method.
    try {
        $fields = $source_plugin->fields();
    } catch (DatabaseConnectionRefusedException|DatabaseNotFoundException|RequirementsException|\PDOException $e) {
    }
    $source_id_field_names = array_keys($source_plugin->getIds());
    $count = 1;
    foreach ($source_id_field_names as $source_id_field_name) {
        $display_name = preg_replace([
            '/^[Tt]he /',
            '/\\.$/',
        ], '', $fields[$source_id_field_name] ?? $source_id_field_name);
        $header[] = [
            'data' => ucfirst($display_name),
            'field' => 'sourceid' . $count++,
            'class' => [
                RESPONSIVE_PRIORITY_MEDIUM,
            ],
        ];
    }
    $header[] = [
        'data' => $this->t('Severity level'),
        'field' => 'level',
        'class' => [
            RESPONSIVE_PRIORITY_LOW,
        ],
    ];
    $header[] = [
        'data' => $this->t('Message'),
        'field' => 'message',
    ];
    $levels = [
        MigrationInterface::MESSAGE_ERROR => $this->t('Error'),
        MigrationInterface::MESSAGE_WARNING => $this->t('Warning'),
        MigrationInterface::MESSAGE_NOTICE => $this->t('Notice'),
        MigrationInterface::MESSAGE_INFORMATIONAL => $this->t('Info'),
    ];
    // Gets each message row and the source ID(s) for that message.
    $query = $this->database
        ->select($message_table, 'msg')
        ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
        ->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
    // Not all messages have a matching row in the map table.
    $query->leftJoin($map_table, 'map', 'msg.source_ids_hash = map.source_ids_hash');
    $query->fields('msg');
    $query->fields('map');
    $filter = $this->buildFilterQuery($request);
    if (!empty($filter['where'])) {
        $query->where($filter['where'], $filter['args']);
    }
    $result = $query->limit(50)
        ->orderByHeader($header)
        ->execute();
    // Build the rows to display.
    $rows = [];
    $add_explanation = FALSE;
    $num_ids = count($source_id_field_names);
    foreach ($result as $message_row) {
        $new_row = [];
        for ($count = 1; $count <= $num_ids; $count++) {
            $map_key = 'sourceid' . $count;
            $new_row[$map_key] = $message_row->{$map_key} ?? NULL;
            if (empty($new_row[$map_key])) {
                $new_row[$map_key] = $this->t('Not available');
                $add_explanation = TRUE;
            }
        }
        $new_row['level'] = $levels[$message_row->level];
        $new_row['message'] = $message_row->message;
        $rows[] = $new_row;
    }
    // Build the complete form.
    $build['message_filter_form'] = $this->formBuilder
        ->getForm('Drupal\\migrate\\Form\\MessageForm');
    $build['message_table'] = [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => $this->t('No messages for this migration.'),
        '#attributes' => [
            'id' => 'admin-migrate-msg',
            'class' => [
                'admin-migrate-msg',
            ],
        ],
    ];
    $build['message_pager'] = [
        '#type' => 'pager',
    ];
    if ($add_explanation) {
        $build['explanation'] = [
            '#type' => 'item',
            '#markup' => $this->t("When there is an error processing a row, the migration system saves the error message but not the source ID(s) of the row. That is why some messages in this table have 'Not available' in the source ID column(s)."),
        ];
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.