Commit cea3af0f authored by alexpott's avatar alexpott

Issue #2787639 by hussainweb: MigrateCckFieldPluginManager mixes up its...

Issue #2787639 by hussainweb: MigrateCckFieldPluginManager mixes up its behaviour for creating and loading definitions
parent 06da5195
...@@ -72,7 +72,8 @@ public function transform($value, MigrateExecutableInterface $migrate_executable ...@@ -72,7 +72,8 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
$field_type = is_array($value) ? $value[0] : $value; $field_type = is_array($value) ? $value[0] : $value;
try { try {
return $this->cckPluginManager->createInstance($field_type, [], $this->migration)->getFieldType($row); $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, [], $this->migration);
return $this->cckPluginManager->createInstance($plugin_id, [], $this->migration)->getFieldType($row);
} }
catch (PluginNotFoundException $e) { catch (PluginNotFoundException $e) {
return parent::transform($value, $migrate_executable, $row, $destination_property); return parent::transform($value, $migrate_executable, $row, $destination_property);
......
...@@ -27,9 +27,22 @@ class MigrateCckFieldPluginManager extends MigratePluginManager { ...@@ -27,9 +27,22 @@ class MigrateCckFieldPluginManager extends MigratePluginManager {
const DEFAULT_CORE_VERSION = 6; const DEFAULT_CORE_VERSION = 6;
/** /**
* {@inheritdoc} * Get the plugin ID from the field type.
*
* @param string $field_type
* The field type being migrated.
* @param array $configuration
* (optioanl) An array of configuration relevant to the plugin instance.
* @param \Drupal\migrate\Plugin\MigrationInterface|null $migration
* (optional) The current migration instance.
*
* @return string
* The ID of the plugin for the field_type if available.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* If the plugin cannot be determined, such as if the field type is invalid.
*/ */
public function createInstance($field_type, array $configuration = array(), MigrationInterface $migration = NULL) { public function getPluginIdFromFieldType($field_type, array $configuration = [], MigrationInterface $migration = NULL) {
$core = static::DEFAULT_CORE_VERSION; $core = static::DEFAULT_CORE_VERSION;
if (!empty($configuration['core'])) { if (!empty($configuration['core'])) {
$core = $configuration['core']; $core = $configuration['core'];
...@@ -45,7 +58,7 @@ public function createInstance($field_type, array $configuration = array(), Migr ...@@ -45,7 +58,7 @@ public function createInstance($field_type, array $configuration = array(), Migr
foreach ($this->getDefinitions() as $plugin_id => $definition) { foreach ($this->getDefinitions() as $plugin_id => $definition) {
if (in_array($core, $definition['core'])) { if (in_array($core, $definition['core'])) {
if (array_key_exists($field_type, $definition['type_map']) || $field_type === $plugin_id) { if (array_key_exists($field_type, $definition['type_map']) || $field_type === $plugin_id) {
return parent::createInstance($plugin_id, $configuration, $migration); return $plugin_id;
} }
} }
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\migrate_drupal\Plugin\migrate; namespace Drupal\migrate_drupal\Plugin\migrate;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrateDestinationPluginManager; use Drupal\migrate\Plugin\MigrateDestinationPluginManager;
...@@ -106,12 +107,19 @@ public function getProcess() { ...@@ -106,12 +107,19 @@ public function getProcess() {
} }
foreach ($source_plugin as $row) { foreach ($source_plugin as $row) {
$field_type = $row->getSourceProperty('type'); $field_type = $row->getSourceProperty('type');
if (!isset($this->processedFieldTypes[$field_type]) && $this->cckPluginManager->hasDefinition($field_type)) { try {
$plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, [], $this);
}
catch (PluginNotFoundException $ex) {
continue;
}
if (!isset($this->processedFieldTypes[$field_type])) {
$this->processedFieldTypes[$field_type] = TRUE; $this->processedFieldTypes[$field_type] = TRUE;
// Allow the cckfield plugin to alter the migration as necessary so // Allow the cckfield plugin to alter the migration as necessary so
// that it knows how to handle fields of this type. // that it knows how to handle fields of this type.
if (!isset($this->cckPluginCache[$field_type])) { if (!isset($this->cckPluginCache[$field_type])) {
$this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, [], $this); $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, [], $this);
} }
call_user_func([$this->cckPluginCache[$field_type], $this->pluginDefinition['cck_plugin_method']], $this); call_user_func([$this->cckPluginCache[$field_type], $this->pluginDefinition['cck_plugin_method']], $this);
} }
......
...@@ -22,32 +22,33 @@ class MigrateCckFieldPluginManagerTest extends MigrateDrupalTestBase { ...@@ -22,32 +22,33 @@ class MigrateCckFieldPluginManagerTest extends MigrateDrupalTestBase {
public function testPluginSelection() { public function testPluginSelection() {
$plugin_manager = \Drupal::service('plugin.manager.migrate.cckfield'); $plugin_manager = \Drupal::service('plugin.manager.migrate.cckfield');
$this->assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d6\\FileField', get_class($plugin_manager->createInstance('filefield', ['core' => 6]))); $plugin_id = $plugin_manager->getPluginIdFromFieldType('filefield', ['core' => 6]);
$this->assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d6\\FileField', get_class($plugin_manager->createInstance($plugin_id, ['core' => 6])));
try { try {
// If this test passes, createInstance will raise a // If this test passes, getPluginIdFromFieldType will raise a
// PluginNotFoundException and we'll never reach fail(). // PluginNotFoundException and we'll never reach fail().
$plugin_manager->createInstance('filefield', ['core' => 7]); $plugin_manager->getPluginIdFromFieldType('filefield', ['core' => 7]);
$this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.'); $this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.');
} }
catch (PluginNotFoundException $e) { catch (PluginNotFoundException $e) {
$this->assertIdentical($e->getMessage(), "Plugin ID 'filefield' was not found."); $this->assertIdentical($e->getMessage(), "Plugin ID 'filefield' was not found.");
} }
$this->assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d7\\ImageField', get_class($plugin_manager->createInstance('image', ['core' => 7]))); $this->assertIdentical('image', $plugin_manager->getPluginIdFromFieldType('image', ['core' => 7]));
$this->assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d7\\FileField', get_class($plugin_manager->createInstance('file', ['core' => 7]))); $this->assertIdentical('file', $plugin_manager->getPluginIdFromFieldType('file', ['core' => 7]));
$this->assertIdentical('Drupal\\migrate_cckfield_plugin_manager_test\\Plugin\\migrate\\cckfield\\D6FileField', get_class($plugin_manager->createInstance('file', ['core' => 6]))); $this->assertIdentical('d6_file', $plugin_manager->getPluginIdFromFieldType('file', ['core' => 6]));
$this->assertIdentical('Drupal\\text\\Plugin\\migrate\\cckfield\\TextField', get_class($plugin_manager->createInstance('text', ['core' => 6]))); $this->assertIdentical('text', $plugin_manager->getPluginIdFromFieldType('text', ['core' => 6]));
$this->assertIdentical('Drupal\\text\\Plugin\\migrate\\cckfield\\TextField', get_class($plugin_manager->createInstance('text', ['core' => 7]))); $this->assertIdentical('text', $plugin_manager->getPluginIdFromFieldType('text', ['core' => 7]));
// Test fallback when no core version is specified. // Test fallback when no core version is specified.
$this->assertIdentical('Drupal\\migrate_cckfield_plugin_manager_test\\Plugin\\migrate\\cckfield\\D6NoCoreVersionSpecified', get_class($plugin_manager->createInstance('d6_no_core_version_specified', ['core' => 6]))); $this->assertIdentical('d6_no_core_version_specified', $plugin_manager->getPluginIdFromFieldType('d6_no_core_version_specified', ['core' => 6]));
try { try {
// If this test passes, createInstance will raise a // If this test passes, getPluginIdFromFieldType will raise a
// PluginNotFoundException and we'll never reach fail(). // PluginNotFoundException and we'll never reach fail().
$plugin_manager->createInstance('d6_no_core_version_specified', ['core' => 7]); $plugin_manager->getPluginIdFromFieldType('d6_no_core_version_specified', ['core' => 7]);
$this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.'); $this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.');
} }
catch (PluginNotFoundException $e) { catch (PluginNotFoundException $e) {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\node\Plugin\migrate; namespace Drupal\node\Plugin\migrate;
use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
...@@ -128,14 +129,15 @@ public function getDerivativeDefinitions($base_plugin_definition) { ...@@ -128,14 +129,15 @@ public function getDerivativeDefinitions($base_plugin_definition) {
if (isset($fields[$node_type])) { if (isset($fields[$node_type])) {
foreach ($fields[$node_type] as $field_name => $info) { foreach ($fields[$node_type] as $field_name => $info) {
$field_type = $info['type']; $field_type = $info['type'];
if ($this->cckPluginManager->hasDefinition($info['type'])) { try {
$plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration);
if (!isset($this->cckPluginCache[$field_type])) { if (!isset($this->cckPluginCache[$field_type])) {
$this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, ['core' => 6], $migration); $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 6], $migration);
} }
$this->cckPluginCache[$field_type] $this->cckPluginCache[$field_type]
->processCckFieldValues($migration, $field_name, $info); ->processCckFieldValues($migration, $field_name, $info);
} }
else { catch (PluginNotFoundException $ex) {
$migration->setProcessOfProperty($field_name, $field_name); $migration->setProcessOfProperty($field_name, $field_name);
} }
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\node\Plugin\migrate; namespace Drupal\node\Plugin\migrate;
use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
...@@ -98,14 +99,15 @@ public function getDerivativeDefinitions($base_plugin_definition) { ...@@ -98,14 +99,15 @@ public function getDerivativeDefinitions($base_plugin_definition) {
if (isset($fields[$node_type])) { if (isset($fields[$node_type])) {
foreach ($fields[$node_type] as $field_name => $info) { foreach ($fields[$node_type] as $field_name => $info) {
$field_type = $info['type']; $field_type = $info['type'];
if ($this->cckPluginManager->hasDefinition($field_type)) { try {
$plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration);
if (!isset($this->cckPluginCache[$field_type])) { if (!isset($this->cckPluginCache[$field_type])) {
$this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, ['core' => 7], $migration); $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration);
} }
$this->cckPluginCache[$field_type] $this->cckPluginCache[$field_type]
->processCckFieldValues($migration, $field_name, $info); ->processCckFieldValues($migration, $field_name, $info);
} }
else { catch (PluginNotFoundException $ex) {
$migration->setProcessOfProperty($field_name, $field_name); $migration->setProcessOfProperty($field_name, $field_name);
} }
} }
......
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