Skip to content

Commit ab34bc3

Browse files
authored
Fixed entity mapping for wordpress importer form (#521)
1 parent 478f209 commit ab34bc3

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

modules/stanford_wordpress_migrate/src/Form/ImporterStep2EntitySelectForm.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,17 @@ protected function getAllowedBundles($entity_type): array {
132132
* {@inheritdoc}
133133
*/
134134
public function submitForm(array &$form, FormStateInterface $form_state): void {
135+
$cached_values = $form_state->getTemporaryValue(['wizard']);
136+
135137
/** @var \Drupal\stanford_wordpress_migrate\WordPressMigrationInterface $migration */
136-
$migration = $form_state->getTemporaryValue([
137-
'wizard',
138-
'wordpress_migration',
139-
]);
140-
$media = array_filter($form_state->getValue('mapping'), fn($mapping) => $mapping['source'] && $mapping['destination']);
141-
$chosen_sources = array_map(fn($a) => $a['source'], $media);
142-
$chosen_destinations = array_map(fn($a) => $a['destination'], $media);
138+
$migration = $cached_values['wordpress_migration'];
139+
$entity_type = $cached_values['entity_type'];
140+
141+
$mappings = array_filter($form_state->getValue('mapping'), fn($mapping) => $mapping['source'] && $mapping['destination']);
142+
$chosen_sources = array_map(fn($a) => $a['source'], $mappings);
143+
$chosen_destinations = array_map(fn($a) => $a['destination'], $mappings);
143144

144-
$field_mapping = $migration->getConfigurationValue('media', []);
145+
$field_mapping = $migration->getConfigurationValue($entity_type, []);
145146

146147
// Remove any previously configured values that are no longer desired.
147148
foreach ($field_mapping as $source => $destinations) {
@@ -157,16 +158,16 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
157158
}
158159
}
159160
}
160-
$migration->setConfigurationValue('media', array_filter($field_mapping));
161+
$migration->setConfigurationValue($entity_type, array_filter($field_mapping));
161162

162-
foreach ($media as $mapping) {
163+
foreach ($mappings as $mapping) {
163164
$field_mapping = $migration->getConfigurationValue([
164-
'media',
165+
$entity_type,
165166
$mapping['source'],
166167
$mapping['destination'],
167168
], []);
168169
$migration->setConfigurationValue([
169-
'media',
170+
$entity_type,
170171
$mapping['source'],
171172
$mapping['destination'],
172173
], $field_mapping);

modules/stanford_wordpress_migrate/tests/src/Unit/Form/ImporterStep2EntitySelectFormTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,11 @@ public function testSubmitFormRemovesOldMappings(): void {
323323
$form_state = $this->createMock(FormStateInterface::class);
324324
$form_state->expects($this->once())
325325
->method('getTemporaryValue')
326-
->with(['wizard', 'wordpress_migration'])
327-
->willReturn($migration);
326+
->with(['wizard'])
327+
->willReturn([
328+
'wordpress_migration' => $migration,
329+
'entity_type' => 'media',
330+
]);
328331

329332
$form_state->expects($this->once())
330333
->method('getValue')
@@ -356,8 +359,11 @@ public function testSubmitFormNoMappings(): void {
356359
$form_state = $this->createMock(FormStateInterface::class);
357360
$form_state->expects($this->once())
358361
->method('getTemporaryValue')
359-
->with(['wizard', 'wordpress_migration'])
360-
->willReturn($migration);
362+
->with(['wizard'])
363+
->willReturn([
364+
'wordpress_migration' => $migration,
365+
'entity_type' => 'media',
366+
]);
361367

362368
$form_state->expects($this->once())
363369
->method('getValue')
@@ -391,8 +397,11 @@ public function testSubmitFormAddsNewMappings(): void {
391397
$form_state = $this->createMock(FormStateInterface::class);
392398
$form_state->expects($this->once())
393399
->method('getTemporaryValue')
394-
->with(['wizard', 'wordpress_migration'])
395-
->willReturn($migration);
400+
->with(['wizard'])
401+
->willReturn([
402+
'wordpress_migration' => $migration,
403+
'entity_type' => 'media',
404+
]);
396405

397406
$form_state->expects($this->once())
398407
->method('getValue')

0 commit comments

Comments
 (0)