diff --git a/modules/feeds_migrate_ui/src/Form/MigrationMappingFormBase.php b/modules/feeds_migrate_ui/src/Form/MigrationMappingFormBase.php index 01e5bd2eef61fddbb8e6b5005e64aad7304a7a9d..383412e3f8d8d8d09768cd1cf21fd0dd7874aaed 100644 --- a/modules/feeds_migrate_ui/src/Form/MigrationMappingFormBase.php +++ b/modules/feeds_migrate_ui/src/Form/MigrationMappingFormBase.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\SubformState; +use Drupal\Core\Security\TrustedCallbackInterface; use Drupal\Core\Url; use Drupal\feeds_migrate\MappingFieldFormManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -15,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * * @package Drupal\feeds_migrate\Form */ -class MigrationMappingFormBase extends EntityForm { +class MigrationMappingFormBase extends EntityForm implements TrustedCallbackInterface { const CUSTOM_DESTINATION_KEY = '_custom'; @@ -80,6 +81,13 @@ class MigrationMappingFormBase extends EntityForm { ); } + /** + * {@inheritdoc} + */ + public static function trustedCallbacks(): array { + return ['postRender']; + } + /** * Gets the label for the destination field - if any. * @@ -157,6 +165,8 @@ class MigrationMappingFormBase extends EntityForm { 'effect' => 'fade', 'progress' => 'throbber', ], + '#post_render' => [[$this, 'postRender']], + '#description' => $this->t('Note: fields that are disabled in the selection list are already mapped.'), ]; $form['general']['destination_key'] = [ @@ -201,6 +211,23 @@ class MigrationMappingFormBase extends EntityForm { return $form; } + /** + * Post render callback: disables already mapped destination fields. + */ + public function postRender($rendered, $element) { + // Disable already mapped fields. + $mappings = $this->entity->getMappings(); + if (empty($mappings)) { + return $rendered; + } + foreach (array_keys($mappings) as $destination_field_id) { + $search[] = '<option value="' . $destination_field_id . '">'; + $replace[] = '<option value="' . $destination_field_id . '" disabled="disabled">'; + } + $rendered = str_replace($search, $replace, $rendered); + return $rendered; + } + /** * Overrides Drupal\Core\Entity\EntityFormController::actions(). * @@ -380,7 +407,10 @@ class MigrationMappingFormBase extends EntityForm { /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */ $fields = $this->entity->getDestinationFields(); foreach ($fields as $field_name => $field) { - $options[$field->getName()] = $field->getLabel(); + $options[$field->getName()] = $this->t('@label (@id)', [ + '@label' => $field->getLabel(), + '@id' => $field->getName(), + ]); } return $options;