media_acquiadam-8.x-1.46/src/Form/AcquiadamMigrationComplete.php
src/Form/AcquiadamMigrationComplete.php
<?php
declare(strict_types=1);
namespace Drupal\media_acquiadam\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Acquia DAM integration from a legacy to a more modern solution.
*/
class AcquiadamMigrationComplete extends ConfigFormBase {
/**
* The state storage service.
*
* @var \Drupal\Core\State\State
*/
protected $stateStorage;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* The migration service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Store module-wide used constant as class attribute.
*
* @var string
*/
public const MODULE_SETTINGS_NAME = 'media_acquiadam.migration_preferences';
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->stateStorage = $container->get('state');
$instance->dateFormatter = $container->get('date.formatter');
$instance->configFactory = $container->get('config.factory');
return $instance;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'media_acquiadam_migration_complete';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
self::MODULE_SETTINGS_NAME,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$timestamp = $this->stateStorage->get('media_acquiadam.migration_process_finished');
$this->messenger()->addWarning($this->t('The migration process was run earlier @duration-since ago
at @date-time and re-running again automatically is not supported.
<a href=":url">Refer the documentation</a>', [
'@duration-since' => $this->dateFormatter->formatTimeDiffSince($timestamp),
'@date-time' => $this->dateFormatter->format($timestamp),
// @todo update the URL to the actual documentation page.
':url' => '#',
]));
unset($form['actions']);
// Get the migration information from state.
$migrated_data = $this->stateStorage->get('media_acquiadam.migrated_data');
// Create a table to display the number of media items for each media type.
$form['table'] = [
'#type' => 'table',
'#header' => [
'media_type_label' => $this->t('Media type'),
'sync_method' => $this->t('Method for handling media'),
'target_source_type' => $this->t('Target source type'),
'migrated_asset_count' => $this->t('Number of assets migrated'),
],
'#caption' => $this->t('This table shows the summary of media items migrated for each media type.'),
'#empty' => $this->t('No assets migrated.'),
];
// If no media items have been migrated,
// display a message instead of the table.
if (!empty($migrated_data)) {
// Add the media types to the table.
foreach ($migrated_data as $info) {
$form['table']['#rows'][] = [
'media_type_label' => $info['media_type_label'],
'sync_method' => $info['sync_method'] === 'sync' ? 'Sync' : 'Embed',
'target_source_type' => \Drupal::service('plugin.manager.media.source')->createInstance($info['target_source_type'])->getPluginDefinition()['label'],
'migrated_asset_count' => $info['migrated_assets_count'],
];
}
}
return $form;
}
}
