Commit d452202e authored by jian he's avatar jian he Committed by heddn
Browse files

Issue #2975266 by jian he, nicholasThompson, sonnykt, k4v, Merkator, heddn:...

Issue #2975266 by jian he, nicholasThompson, sonnykt, k4v, Merkator, heddn: EntityGenerate: Provide multiple values will not process correctly
parent 3c13a86e
......@@ -2,6 +2,7 @@
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\migrate\MigrateExecutableInterface;
......@@ -53,11 +54,11 @@ class EntityGenerate extends EntityLookup {
protected $row;
/**
* The MigrateExecutable instance.
* The MigratePluginManager instance.
*
* @var \Drupal\migrate\MigrateExecutable
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
*/
protected $migrateExecutable;
protected $processPluginManager;
/**
* The get process plugin instance.
......@@ -81,14 +82,12 @@ class EntityGenerate extends EntityLookup {
* The $entityManager instance.
* @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
* The $selectionPluginManager instance.
* @param \Drupal\migrate\Plugin\MigratePluginManager $migratePluginManager
* @param \Drupal\migrate\Plugin\MigratePluginManager $processPluginManager
* The MigratePluginManager instance.
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $migratePluginManager) {
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $processPluginManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $entityManager, $selectionPluginManager);
if (isset($configuration['values'])) {
$this->getProcessPlugin = $migratePluginManager->createInstance('get', ['source' => $configuration['values']]);
}
$this->processPluginManager = $processPluginManager;
}
/**
......@@ -168,8 +167,16 @@ class EntityGenerate extends EntityLookup {
// Gather any additional properties/fields.
if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
foreach ($this->configuration['values'] as $key => $property) {
$source_value = $this->getProcessPlugin->transform(NULL, $this->migrateExecutable, $this->row, $property);
$entity_values[$key] = $source_value;
// TODO: Remove this logic once 8.6 is no longer supported.
// See https://www.drupal.org/project/migrate_plus/issues/3043199
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
$source_value = $this->row->get($property);
}
else {
$getProcessPlugin = $this->processPluginManager->createInstance('get', ['source' => $property]);
$source_value = $getProcessPlugin->transform(NULL, $this->migrateExecutable, $this->row, $property);
}
NestedArray::setValue($entity_values, explode(Row::PROPERTY_SEPARATOR, $key), $source_value, TRUE);
}
}
......
......@@ -552,6 +552,96 @@ class EntityGenerateTest extends KernelTestBase implements MigrateMessageInterfa
],
],
],
'provide multiple values' => [
'definition' => [
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'id' => 1,
'title' => 'content item 1',
'term' => 'Apples',
],
[
'id' => 2,
'title' => 'content item 2',
'term' => 'Bananas',
],
[
'id' => 3,
'title' => 'content item 3',
'term' => 'Grapes',
],
],
'ids' => [
'id' => ['type' => 'integer'],
],
'constants' => [
'foo' => 'bar',
],
],
'process' => [
'id' => 'id',
'type' => [
'plugin' => 'default_value',
'default_value' => $this->bundle,
],
'title' => 'title',
'term_upper' => [
'plugin' => 'callback',
'source' => 'term',
'callable' => 'strtoupper',
],
$this->fieldName => [
'plugin' => 'entity_generate',
'source' => 'term',
'values' => [
'name' => '@term_upper',
'description' => 'constants/foo',
],
],
],
'destination' => [
'plugin' => 'entity:node',
],
],
'expected' => [
'row 1' => [
'id' => 1,
'title' => 'content item 1',
$this->fieldName => [
'tid' => 2,
'name' => 'APPLES',
'description' => 'bar',
],
],
'row 2' => [
'id' => 2,
'title' => 'content item 2',
$this->fieldName => [
'tid' => 3,
'name' => 'BANANAS',
'description' => 'bar',
],
],
'row 3' => [
'id' => 3,
'title' => 'content item 3',
$this->fieldName => [
'tid' => 1,
'name' => 'Grapes',
'description' => NULL,
],
],
],
'pre seed' => [
'taxonomy_term' => [
'name' => 'Grapes',
'vid' => $this->vocabulary,
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
],
],
],
'lookup single existing term returns correct term' => [
'definition' => [
'source' => [
......
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