Skip to content
Snippets Groups Projects
Commit 6fc770ab authored by Youri van Koppen's avatar Youri van Koppen
Browse files

Issue #3377671: Disable already mapped fields on the mapping add form.

parent c2834437
No related branches found
No related tags found
1 merge request!20Issue #3377671: Disable already mapped fields on mapping add form
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment