media_acquiadam-8.x-1.46/src/Form/AcquiadamMigrationListForm.php
src/Form/AcquiadamMigrationListForm.php
<?php
namespace Drupal\media_acquiadam\Form;
use Drupal\acquia_dam\Entity\MediaSourceField;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\media_acquiadam\Traits\MigrationTrait;
/**
* The class defines the form for listing the media types.
*/
class AcquiadamMigrationListForm {
use StringTranslationTrait;
use MigrationTrait;
/**
* {@inheritdoc}
*/
public function buildForm($form, FormStateInterface $form_state) {
$stored_values = $this->getMigrationConfig();
$target_source_types = array_column($stored_values['legacy_dam_media_types'], 'target_source_type');
$modern_dam_media_types = array_values($this->getMediaTypesBySourceField(MediaSourceField::SOURCE_FIELD_NAME));
$form['wrapper']['info'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t('Based on the mapping provided on the previous page,
we recommend deleting the mapped media from Acquia DAM module. However,
this can be adjusted as needed, and there\'s also the option to update the media label from this page.'),
];
$form['wrapper']['modern_dam_media_types'] = [
'#type' => 'table',
'#header' => [
'modern_dam_info' => $this->t('Media type label and name'),
'delete' => $this->t('Delete'),
],
'#attributes' => [
'id' => 'modern_dam_deletion_table',
'style' => 'border: 1px solid #dee2e6; border-collapse: collapse;',
],
];
$media_counts = $this->getMediaCounts(array_keys($this->getMediaTypesBySourceField(MediaSourceField::SOURCE_FIELD_NAME)));
foreach ($modern_dam_media_types as $key => $modern_dam_media_type) {
$form['wrapper']['modern_dam_media_types'][] = [
'media_type_id' => [
'#type' => 'hidden',
'#value' => $modern_dam_media_type->id(),
'#wrapper_attributes' => ['style' => 'display: none;'],
],
'media_type_label' => [
'#type' => 'textfield',
'#default_value' => $stored_values['modern_dam_media_types'][$key]['media_type_label'] ?? $modern_dam_media_type->label(),
'#description' => $this->t('Machine name: <code>@media-type-machine-name</code>', [
'@media-type-machine-name' => $modern_dam_media_type->id(),
]),
'#states' => [
'disabled' => [
':input[name="modern_dam_media_types[' . $key . '][delete]"]' => ['checked' => TRUE],
],
],
'#maxlength' => 128,
'#size' => 20,
'#attributes' => [
'placeholder' => $this->t('e.g. @suggested_media_bundle_label', [
'@suggested_media_bundle_label' => $modern_dam_media_type->label(),
]),
],
'#disabled' => (bool) ($stored_values['modern_dam_media_types'][$key]['delete'] ?? $media_counts[$modern_dam_media_type->id()] ?? in_array($modern_dam_media_type->get('source'), $target_source_types)),
],
'delete' => [
'#type' => 'checkbox',
'#default_value' => (bool) (isset($media_counts[$modern_dam_media_type->id()]) && $media_counts[$modern_dam_media_type->id()] > 0 ? FALSE : $stored_values['modern_dam_media_types'][$key]['delete'] ?? in_array($modern_dam_media_type->get('source'), $target_source_types)),
'#attributes' => [
'onchange' => (in_array($modern_dam_media_type->get('source'), $target_source_types)) ?
'if (!this.checked && !confirm("It\'s recommended to delete \"' . $modern_dam_media_type->label() . '\" media type post migration. Are you sure you want to keep it?")) { this.checked = true; }' : '',
],
'#disabled' => ($media_counts[$modern_dam_media_type->id()] ?? 0) > 0,
],
];
}
return $form;
}
}
