Commit 84c5ed58 authored by heddn's avatar heddn Committed by heddn

Issue #3001828 by heddn, firfin: field type integeris not a recognized...

Issue #3001828 by heddn, firfin: field type integeris not a recognized reference type, or:How to entity_lookup a commerce_product
parent 8bde75f2
......@@ -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()));
}
}
}
......
......@@ -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));
}
......
......@@ -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 = '';
}
}
......
......@@ -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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment