reverted: --- b/core/modules/field/src/Plugin/migrate/process/FieldType.php +++ a/core/modules/field/src/Plugin/migrate/process/FieldType.php @@ -8,7 +8,7 @@ use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Plugin\migrate\process\StaticMap; use Drupal\migrate\Row; +use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface; -use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -19,11 +19,11 @@ class FieldType extends StaticMap implements ContainerFactoryPluginInterface { /** + * The cckfield plugin manager. - * The field plugin manager. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface */ + protected $cckPluginManager; - protected $fieldPluginManager; /** * The migration object. @@ -41,14 +41,14 @@ * The plugin ID. * @param mixed $plugin_definition * The plugin definition. + * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_plugin_manager + * The cckfield plugin manager. - * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_plugin_manager - * The field plugin manager. * @param \Drupal\migrate\Plugin\MigrationInterface $migration * The migration being run. */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateCckFieldPluginManagerInterface $cck_plugin_manager, MigrationInterface $migration = NULL) { - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateFieldPluginManagerInterface $field_plugin_manager, MigrationInterface $migration = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->cckPluginManager = $cck_plugin_manager; - $this->fieldPluginManager = $field_plugin_manager; $this->migration = $migration; } @@ -60,7 +60,7 @@ $configuration, $plugin_id, $plugin_definition, + $container->get('plugin.manager.migrate.cckfield'), - $container->get('plugin.manager.migrate.field'), $migration ); } @@ -72,8 +72,8 @@ $field_type = is_array($value) ? $value[0] : $value; try { + $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, [], $this->migration); + return $this->cckPluginManager->createInstance($plugin_id, [], $this->migration)->getFieldType($row); - $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, [], $this->migration); - return $this->fieldPluginManager->createInstance($plugin_id, [], $this->migration)->getFieldType($row); } catch (PluginNotFoundException $e) { return parent::transform($value, $migrate_executable, $row, $destination_property); diff -u b/core/modules/migrate_drupal/src/Plugin/MigrateFieldPluginManager.php b/core/modules/migrate_drupal/src/Plugin/MigrateFieldPluginManager.php --- b/core/modules/migrate_drupal/src/Plugin/MigrateFieldPluginManager.php +++ b/core/modules/migrate_drupal/src/Plugin/MigrateFieldPluginManager.php @@ -42,7 +42,8 @@ } } - foreach ($this->getDefinitions() as $plugin_id => $definition) { + $definitions = $this->getDefinitions(); + foreach ($definitions as $plugin_id => $definition) { if (in_array($core, $definition['core'])) { if (array_key_exists($field_type, $definition['type_map']) || $field_type === $plugin_id) { return $plugin_id; diff -u b/core/modules/migrate_drupal/src/Plugin/migrate/CckMigration.php b/core/modules/migrate_drupal/src/Plugin/migrate/CckMigration.php --- b/core/modules/migrate_drupal/src/Plugin/migrate/CckMigration.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/CckMigration.php @@ -2,6 +2,8 @@ namespace Drupal\migrate_drupal\Plugin\migrate; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Migration plugin class for migrations dealing with CCK field values. * @@ -17,2 +19,19 @@ + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('plugin.manager.migrate.cckfield'), + $container->get('plugin.manager.migration'), + $container->get('plugin.manager.migrate.source'), + $container->get('plugin.manager.migrate.process'), + $container->get('plugin.manager.migrate.destination'), + $container->get('plugin.manager.migrate.id_map') + ); + } + } reverted: --- /dev/null +++ a/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/migrate_cckfield_plugin_manager_test.info.yml @@ -0,0 +1,6 @@ +name: 'Migrate cck field plugin manager test' +type: module +description: 'Example module demonstrating the cck field plugin manager in the Migrate API.' +package: Testing +version: VERSION +core: 8.x reverted: --- /dev/null +++ a/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/src/Plugin/migrate/cckfield/D6FileField.php @@ -0,0 +1,29 @@ +container->get('plugin.manager.migrate.field'); + $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]))); - $configuration = ['core' => 6]; - $plugin_id = $plugin_manager->getPluginIdFromFieldType('filefield', $configuration); - $this->assertInstanceOf(FileField::class, $plugin_manager->createInstance($plugin_id, $configuration)); try { // If this test passes, getPluginIdFromFieldType will raise a unchanged: --- /dev/null +++ b/core/modules/migrate_drupal/tests/src/Kernel/MigrateFieldPluginManagerTest.php @@ -0,0 +1,61 @@ +container->get('plugin.manager.migrate.field'); + + $configuration = ['core' => 6]; + $plugin_id = $plugin_manager->getPluginIdFromFieldType('filefield', $configuration); + $this->assertInstanceOf(FileField::class, $plugin_manager->createInstance($plugin_id, $configuration)); + + try { + // If this test passes, getPluginIdFromFieldType will raise a + // PluginNotFoundException and we'll never reach fail(). + $plugin_manager->getPluginIdFromFieldType('filefield', ['core' => 7]); + $this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.'); + } + catch (PluginNotFoundException $e) { + $this->assertIdentical($e->getMessage(), "Plugin ID 'filefield' was not found."); + } + + $this->assertIdentical('image', $plugin_manager->getPluginIdFromFieldType('image', ['core' => 7])); + $this->assertIdentical('file', $plugin_manager->getPluginIdFromFieldType('file', ['core' => 7])); + $this->assertIdentical('d6_file', $plugin_manager->getPluginIdFromFieldType('file', ['core' => 6])); + + $this->assertIdentical('text', $plugin_manager->getPluginIdFromFieldType('text', ['core' => 6])); + $this->assertIdentical('text', $plugin_manager->getPluginIdFromFieldType('text', ['core' => 7])); + + // Test fallback when no core version is specified. + $this->assertIdentical('d6_no_core_version_specified', $plugin_manager->getPluginIdFromFieldType('d6_no_core_version_specified', ['core' => 6])); + + try { + // If this test passes, getPluginIdFromFieldType will raise a + // PluginNotFoundException and we'll never reach fail(). + $plugin_manager->getPluginIdFromFieldType('d6_no_core_version_specified', ['core' => 7]); + $this->fail('Expected Drupal\Component\Plugin\Exception\PluginNotFoundException.'); + } + catch (PluginNotFoundException $e) { + $this->assertIdentical($e->getMessage(), "Plugin ID 'd6_no_core_version_specified' was not found."); + } + } + +} reverted: --- b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php +++ a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @@ -8,7 +8,7 @@ use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\Plugin\MigrationDeriverTrait; +use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface; -use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -25,18 +25,18 @@ protected $basePluginId; /** + * Already-instantiated cckfield plugins, keyed by ID. - * Already-instantiated field plugins, keyed by ID. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[] - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[] */ + protected $cckPluginCache; - protected $fieldPluginCache; /** + * The CCK plugin manager. - * The field plugin manager. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface */ + protected $cckPluginManager; - protected $fieldPluginManager; /** * Whether or not to include translations. @@ -50,14 +50,14 @@ * * @param string $base_plugin_id * The base plugin ID for the plugin ID. + * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager + * The CCK plugin manager. - * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager - * The field plugin manager. * @param bool $translations * Whether or not to include translations. */ + public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager, $translations) { - public function __construct($base_plugin_id, MigrateFieldPluginManagerInterface $field_manager, $translations) { $this->basePluginId = $base_plugin_id; + $this->cckPluginManager = $cck_manager; - $this->fieldPluginManager = $field_manager; $this->includeTranslations = $translations; } @@ -68,7 +68,7 @@ // Translations don't make sense unless we have content_translation. return new static( $base_plugin_id, + $container->get('plugin.manager.migrate.cckfield'), - $container->get('plugin.manager.migrate.field'), $container->get('module_handler')->moduleExists('content_translation') ); } @@ -90,7 +90,7 @@ return $this->derivatives; } + // Read all CCK field instance definitions in the source database. - // Read all field instance definitions in the source database. $fields = array(); try { $source_plugin = static::getSourcePlugin('d6_field_instance'); @@ -102,7 +102,7 @@ } catch (RequirementsException $e) { // If checkRequirements() failed then the content module did not exist and + // we do not have any CCK fields. Therefore, $fields will be empty and - // we do not have any fields. Therefore, $fields will be empty and // below we'll create a migration just for the node properties. } @@ -130,12 +130,12 @@ foreach ($fields[$node_type] as $field_name => $info) { $field_type = $info['type']; try { + $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration); + if (!isset($this->cckPluginCache[$field_type])) { + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 6], $migration); - $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 6], $migration); - if (!isset($this->fieldPluginCache[$field_type])) { - $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 6], $migration); } + $this->cckPluginCache[$field_type] + ->processCckFieldValues($migration, $field_name, $info); - $this->fieldPluginCache[$field_type] - ->processFieldValues($migration, $field_name, $info); } catch (PluginNotFoundException $ex) { $migration->setProcessOfProperty($field_name, $field_name); reverted: --- b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php +++ a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @@ -8,7 +8,7 @@ use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\Plugin\MigrationDeriverTrait; +use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface; -use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -25,30 +25,30 @@ protected $basePluginId; /** + * Already-instantiated cckfield plugins, keyed by ID. - * Already-instantiated field plugins, keyed by ID. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[] - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[] */ + protected $cckPluginCache; - protected $fieldPluginCache; /** + * The CCK plugin manager. - * The field plugin manager. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface */ + protected $cckPluginManager; - protected $fieldPluginManager; /** * D7NodeDeriver constructor. * * @param string $base_plugin_id * The base plugin ID for the plugin ID. + * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager + * The CCK plugin manager. - * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager - * The field plugin manager. */ + public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager) { - public function __construct($base_plugin_id, MigrateFieldPluginManagerInterface $field_manager) { $this->basePluginId = $base_plugin_id; + $this->cckPluginManager = $cck_manager; - $this->fieldPluginManager = $field_manager; } /** @@ -57,7 +57,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) { return new static( $base_plugin_id, + $container->get('plugin.manager.migrate.cckfield') - $container->get('plugin.manager.migrate.field') ); } @@ -100,12 +100,12 @@ foreach ($fields[$node_type] as $field_name => $info) { $field_type = $info['type']; try { + $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); + if (!isset($this->cckPluginCache[$field_type])) { + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration); - $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); - if (!isset($this->fieldPluginCache[$field_type])) { - $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration); } + $this->cckPluginCache[$field_type] + ->processCckFieldValues($migration, $field_name, $info); - $this->fieldPluginCache[$field_type] - ->processFieldValues($migration, $field_name, $info); } catch (PluginNotFoundException $ex) { $migration->setProcessOfProperty($field_name, $field_name); reverted: --- b/core/modules/node/src/Plugin/migrate/source/d6/Node.php +++ a/core/modules/node/src/Plugin/migrate/source/d6/Node.php @@ -176,24 +176,24 @@ } /** + * Gets CCK field values for a node. - * Gets field values for a node. * * @param \Drupal\migrate\Row $node * The node. * * @return array + * CCK field values, keyed by field name. - * Field values, keyed by field name. */ protected function getFieldValues(Row $node) { $values = []; foreach ($this->getFieldInfo($node->getSourceProperty('type')) as $field => $info) { + $values[$field] = $this->getCckData($info, $node); - $values[$field] = $this->getFieldData($info, $node); } return $values; } /** + * Gets CCK field and instance definitions from the database. - * Gets field and instance definitions from the database. * * @param string $node_type * The node type for which to get field info. @@ -205,7 +205,7 @@ if (!isset($this->fieldInfo)) { $this->fieldInfo = []; + // Query the database directly for all CCK field info. - // Query the database directly for all field info. $query = $this->select('content_node_field_instance', 'cnfi'); $query->join('content_node_field', 'cnf', 'cnf.field_name = cnfi.field_name'); $query->fields('cnfi'); @@ -230,7 +230,7 @@ } /** + * Retrieves raw CCK field data for a node. - * Retrieves raw field data for a node. * * @param array $field * A field and instance definition from getFieldInfo(). @@ -240,7 +240,7 @@ * @return array * The field values, keyed by delta. */ + protected function getCckData(array $field, Row $node) { - protected function getFieldData(array $field, Row $node) { $field_table = 'content_' . $field['field_name']; $node_table = 'content_type_' . $node->getSourceProperty('type'); @@ -276,9 +276,10 @@ return $query // This call to isNotNull() is a kludge which relies on the convention + // that CCK field schemas usually define their most important + // column first. A better way would be to allow cckfield plugins to + // alter the query directly before it's run, but this will do for + // the time being. - // that field schemas usually define their most important column first. - // A better way would be to allow field plugins to alter the query - // directly before it's run, but this will do for the time being. ->isNotNull($field['field_name'] . '_' . $columns[0]) ->condition('nid', $node->getSourceProperty('nid')) ->condition('vid', $node->getSourceProperty('vid')) @@ -291,24 +292,6 @@ } /** - * Retrieves raw field data for a node. - * - * @deprecated in Drupal 8.2.x, to be removed in Drupal 9.0.x. Use - * getFieldData() instead. - * - * @param array $field - * A field and instance definition from getFieldInfo(). - * @param \Drupal\migrate\Row $node - * The node. - * - * @return array - * The field values, keyed by delta. - */ - protected function getCckData(array $field, Row $node) { - return $this->getFieldData($field, $node); - } - - /** * {@inheritdoc} */ public function getIds() { reverted: --- b/core/modules/taxonomy/src/Plugin/migrate/D7TaxonomyTermDeriver.php +++ a/core/modules/taxonomy/src/Plugin/migrate/D7TaxonomyTermDeriver.php @@ -7,15 +7,15 @@ use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\migrate\Exception\RequirementsException; +use Drupal\migrate\Plugin\Migration; -use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface; use Drupal\migrate\Plugin\MigrationDeriverTrait; +use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Deriver for Drupal 7 taxonomy term migrations based on vocabularies. */ class D7TaxonomyTermDeriver extends DeriverBase implements ContainerDeriverInterface { - use MigrationDeriverTrait; /** @@ -26,30 +26,30 @@ protected $basePluginId; /** + * Already-instantiated cckfield plugins, keyed by ID. - * Already-instantiated field plugins, keyed by ID. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldInterface[] - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface[] */ + protected $cckPluginCache; - protected $fieldPluginCache; /** + * The CCK plugin manager. - * The field plugin manager. * + * @var \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface - * @var \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface */ + protected $cckPluginManager; - protected $fieldPluginManager; /** * D7TaxonomyTermDeriver constructor. * * @param string $base_plugin_id * The base plugin ID for the plugin ID. + * @param \Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface $cck_manager + * The CCK plugin manager. - * @param \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface $field_manager - * The field plugin manager. */ + public function __construct($base_plugin_id, MigrateCckFieldPluginManagerInterface $cck_manager) { - public function __construct($base_plugin_id, MigrateFieldPluginManagerInterface $field_manager) { $this->basePluginId = $base_plugin_id; + $this->cckPluginManager = $cck_manager; - $this->fieldPluginManager = $field_manager; } /** @@ -58,7 +58,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) { return new static( $base_plugin_id, + $container->get('plugin.manager.migrate.cckfield') - $container->get('plugin.manager.migrate.field') ); } @@ -102,12 +102,12 @@ foreach ($fields[$bundle] as $field_name => $info) { $field_type = $info['type']; try { + $plugin_id = $this->cckPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); + if (!isset($this->cckPluginCache[$field_type])) { + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($plugin_id, ['core' => 7], $migration); - $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type, ['core' => 7], $migration); - if (!isset($this->fieldPluginCache[$field_type])) { - $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($plugin_id, ['core' => 7], $migration); } + $this->cckPluginCache[$field_type] + ->processCckFieldValues($migration, $field_name, $info); - $this->fieldPluginCache[$field_type] - ->processFieldValues($migration, $field_name, $info); } catch (PluginNotFoundException $ex) { $migration->setProcessOfProperty($field_name, $field_name); reverted: --- b/core/modules/user/src/Plugin/migrate/User.php +++ a/core/modules/user/src/Plugin/migrate/User.php @@ -3,12 +3,12 @@ namespace Drupal\user\Plugin\migrate; use Drupal\migrate\Exception\RequirementsException; +use Drupal\migrate_drupal\Plugin\migrate\CckMigration; -use Drupal\migrate_drupal\Plugin\migrate\FieldMigration; /** * Plugin class for Drupal 7 user migrations dealing with fields and profiles. */ +class User extends CckMigration { -class User extends FieldMigration { /** * {@inheritdoc} @@ -30,13 +30,13 @@ if (empty($field_type)) { continue; } + if ($this->cckPluginManager->hasDefinition($field_type)) { + if (!isset($this->cckPluginCache[$field_type])) { + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, [], $this); - if ($this->fieldPluginManager->hasDefinition($field_type)) { - if (!isset($this->fieldPluginCache[$field_type])) { - $this->fieldPluginCache[$field_type] = $this->fieldPluginManager->createInstance($field_type, [], $this); } $info = $row->getSource(); + $this->cckPluginCache[$field_type] + ->processCckFieldValues($this, $field_name, $info); - $this->fieldPluginCache[$field_type] - ->processFieldValues($this, $field_name, $info); } else { $this->process[$field_name] = $field_name;