diff --git a/src/Plugin/migrate/process/EntityLookup.php b/src/Plugin/migrate/process/EntityLookup.php index 2ae0d07c1de2a438c830da55412a9c6618b2e7da..58dfc343913a1f6131eaf02b8b1130feb2caca01 100644 --- a/src/Plugin/migrate/process/EntityLookup.php +++ b/src/Plugin/migrate/process/EntityLookup.php @@ -223,8 +223,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn break; default: - throw new MigrateException('Destination field type ' . - $fieldConfig->getType() . 'is not a recognized reference type.'); + throw new MigrateException(sprintf('Destination field type %s is not a recognized reference type.', $fieldConfig->getType())); } } } diff --git a/src/Plugin/migrate/process/Merge.php b/src/Plugin/migrate/process/Merge.php index 463595e9d7b31c13bd6e7e4dc3a799d28ac1dff4..d6b32bbc0cbbf4a6979d0c523d9492de006d3f49 100644 --- a/src/Plugin/migrate/process/Merge.php +++ b/src/Plugin/migrate/process/Merge.php @@ -56,7 +56,7 @@ class Merge extends ProcessPluginBase { throw new MigrateException(sprintf('Merge process failed for destination property (%s): input is not an array.', $destination_property)); } $new_value = []; - foreach($value as $i => $item) { + foreach ($value as $i => $item) { if (!is_array($item)) { throw new MigrateException(sprintf('Merge process failed for destination property (%s): index (%s) in the source value is not an array that can be merged.', $destination_property, $i)); } diff --git a/src/Plugin/migrate_plus/data_parser/Json.php b/src/Plugin/migrate_plus/data_parser/Json.php index c1220bc5f4e60d9836fbdaac4cabce4947e70d4f..7ef4467d00b1ac7e963b4d15472e143d4cbfc40b 100755 --- a/src/Plugin/migrate_plus/data_parser/Json.php +++ b/src/Plugin/migrate_plus/data_parser/Json.php @@ -114,8 +114,8 @@ class Json extends DataParserPluginBase implements ContainerFactoryPluginInterfa foreach ($field_selectors as $field_selector) { if (is_array($field_data) && array_key_exists($field_selector, $field_data)) { $field_data = $field_data[$field_selector]; - } - else { + } + else { $field_data = ''; } } diff --git a/tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php b/tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php index 3d46496de479d92ba2507ca6506dcbad65733551..0240c1049df2efd68df2f5bdcc3e2082eee53ccf 100644 --- a/tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php +++ b/tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php @@ -5,11 +5,14 @@ namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; use Drupal\KernelTests\KernelTestBase; use Drupal\migrate\MigrateExecutable; use Drupal\migrate\MigrateMessageInterface; use Drupal\node\Entity\NodeType; +use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; /** @@ -107,6 +110,18 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ); + // Create a non-reference field. + FieldStorageConfig::create([ + 'field_name' => 'field_integer', + 'type' => 'integer', + 'entity_type' => 'node', + ])->save(); + FieldConfig::create([ + 'field_name' => 'field_integer', + 'entity_type' => 'node', + 'bundle' => $this->bundle, + ])->save(); + $this->migrationPluginManager = \Drupal::service('plugin.manager.migration'); } @@ -203,6 +218,99 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa } } + /** + * Test lookup without a reference field. + */ + public function testNonReferenceField() { + $values = [ + 'name' => 'Apples', + 'vid' => $this->vocabulary, + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + ]; + $this->createTestData('taxonomy_term', $values); + + // Not enough context is provided for a non reference field, so error out. + $definition = [ + 'source' => [ + 'plugin' => 'embedded_data', + 'data_rows' => [ + [ + 'id' => 1, + 'title' => 'content item 1', + 'term' => 'Apples', + ], + ], + 'ids' => [ + 'id' => ['type' => 'integer'], + ], + ], + 'process' => [ + 'id' => 'id', + 'type' => [ + 'plugin' => 'default_value', + 'default_value' => $this->bundle, + ], + 'title' => 'title', + 'field_integer' => [ + 'plugin' => 'entity_generate', + 'source' => 'term', + ], + ], + 'destination' => [ + 'plugin' => 'entity:node', + ], + ]; + /** @var \Drupal\migrate\Plugin\Migration $migration */ + $migration = $this->migrationPluginManager->createStubMigration($definition); + $migrationExecutable = (new MigrateExecutable($migration, $this)); + $migrationExecutable->import(); + $this->assertEquals('Destination field type integer is not a recognized reference type.', $migration->getIdMap()->getMessageIterator()->fetch()->message); + $this->assertSame(1, $migration->getIdMap()->messageCount()); + + // Enough context is provided so this should work. + $definition = [ + 'source' => [ + 'plugin' => 'embedded_data', + 'data_rows' => [ + [ + 'id' => 1, + 'title' => 'content item 1', + 'term' => 'Apples', + ], + ], + 'ids' => [ + 'id' => ['type' => 'integer'], + ], + ], + 'process' => [ + 'id' => 'id', + 'type' => [ + 'plugin' => 'default_value', + 'default_value' => $this->bundle, + ], + 'title' => 'title', + 'field_integer' => [ + 'plugin' => 'entity_generate', + 'source' => 'term', + 'value_key' => 'name', + 'bundle_key' => 'vid', + 'bundle' => $this->vocabulary, + 'entity_type' => 'taxonomy_term', + ], + ], + 'destination' => [ + 'plugin' => 'entity:node', + ], + ]; + /** @var \Drupal\migrate\Plugin\Migration $migration */ + $migration = $this->migrationPluginManager->createStubMigration($definition); + $migrationExecutable = (new MigrateExecutable($migration, $this)); + $migrationExecutable->import(); + $this->assertEmpty($migration->getIdMap()->messageCount()); + $term = Term::load(1); + $this->assertEquals('Apples', $term->label()); + } + /** * Provides multiple migration definitions for "transform" test. */ @@ -775,6 +883,9 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa * The storage manager to create. * @param array $values * The values to use when creating the entity. + * + * @return string|int + * The entity identifier. */ private function createTestData($storageName, array $values) { /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ @@ -783,6 +894,7 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa ->getStorage($storageName); $entity = $storage->create($values); $entity->save(); + return $entity->id(); } }