diff --git a/core/modules/field/migration_templates/d6_field.yml b/core/modules/field/migration_templates/d6_field.yml index 531bc13..aaffebc 100644 --- a/core/modules/field/migration_templates/d6_field.yml +++ b/core/modules/field/migration_templates/d6_field.yml @@ -4,6 +4,7 @@ migration_tags: - Drupal 6 class: Drupal\migrate_drupal\Plugin\migrate\CckMigration cck_plugin_method: processField +core: 6 source: plugin: d6_field constants: diff --git a/core/modules/field/migration_templates/d6_field_formatter_settings.yml b/core/modules/field/migration_templates/d6_field_formatter_settings.yml index 7496db3..9855f1c 100644 --- a/core/modules/field/migration_templates/d6_field_formatter_settings.yml +++ b/core/modules/field/migration_templates/d6_field_formatter_settings.yml @@ -4,6 +4,7 @@ migration_tags: - Drupal 6 class: Drupal\migrate_drupal\Plugin\migrate\CckMigration cck_plugin_method: processFieldFormatter +core: 6 source: plugin: d6_field_instance_per_view_mode constants: diff --git a/core/modules/field/migration_templates/d6_field_instance.yml b/core/modules/field/migration_templates/d6_field_instance.yml index b5035c2..4b48d8d 100644 --- a/core/modules/field/migration_templates/d6_field_instance.yml +++ b/core/modules/field/migration_templates/d6_field_instance.yml @@ -4,6 +4,7 @@ migration_tags: - Drupal 6 class: Drupal\migrate_drupal\Plugin\migrate\CckMigration cck_plugin_method: processFieldInstance +core: 6 source: plugin: d6_field_instance constants: diff --git a/core/modules/field/migration_templates/d6_field_instance_widget_settings.yml b/core/modules/field/migration_templates/d6_field_instance_widget_settings.yml index 46b7d75..df943de 100644 --- a/core/modules/field/migration_templates/d6_field_instance_widget_settings.yml +++ b/core/modules/field/migration_templates/d6_field_instance_widget_settings.yml @@ -4,6 +4,7 @@ migration_tags: - Drupal 6 class: Drupal\migrate_drupal\Plugin\migrate\CckMigration cck_plugin_method: processFieldWidget +core: 6 source: plugin: d6_field_instance_per_form_display constants: diff --git a/core/modules/field/migration_templates/d7_field.yml b/core/modules/field/migration_templates/d7_field.yml old mode 100644 new mode 100755 index 3b01f54..6017994 --- a/core/modules/field/migration_templates/d7_field.yml +++ b/core/modules/field/migration_templates/d7_field.yml @@ -2,6 +2,9 @@ id: d7_field label: Field configuration migration_tags: - Drupal 7 +class: Drupal\migrate_drupal\Plugin\migrate\CckMigration +cck_plugin_method: processField +core: 7 source: plugin: d7_field constants: @@ -13,7 +16,7 @@ process: langcode: 'constants/langcode' field_name: field_name type: - plugin: static_map + plugin: field_type source: type map: date: datetime @@ -30,8 +33,6 @@ process: number_decimal: decimal number_float: float phone: telephone - taxonomy_term_reference: entity_reference - text: text text_long: text_long text_with_summary: text_with_summary translatable: translatable diff --git a/core/modules/field/migration_templates/d7_field_formatter_settings.yml b/core/modules/field/migration_templates/d7_field_formatter_settings.yml index 2eb8ec8..3fc0272 100644 --- a/core/modules/field/migration_templates/d7_field_formatter_settings.yml +++ b/core/modules/field/migration_templates/d7_field_formatter_settings.yml @@ -2,6 +2,9 @@ id: d7_field_formatter_settings label: Field formatter configuration migration_tags: - Drupal 7 +class: Drupal\migrate_drupal\Plugin\migrate\CckMigration +cck_plugin_method: processFieldFormatter +core: 7 source: plugin: d7_field_instance_per_view_mode constants: diff --git a/core/modules/field/migration_templates/d7_field_instance.yml b/core/modules/field/migration_templates/d7_field_instance.yml index 62ec9ce..9ac1626 100644 --- a/core/modules/field/migration_templates/d7_field_instance.yml +++ b/core/modules/field/migration_templates/d7_field_instance.yml @@ -2,6 +2,9 @@ id: d7_field_instance label: Field instance configuration migration_tags: - Drupal 7 +class: Drupal\migrate_drupal\Plugin\migrate\CckMigration +cck_plugin_method: processFieldInstance +core: 7 source: plugin: d7_field_instance constants: diff --git a/core/modules/field/migration_templates/d7_field_instance_widget_settings.yml b/core/modules/field/migration_templates/d7_field_instance_widget_settings.yml index e710fbe..b4dff13 100644 --- a/core/modules/field/migration_templates/d7_field_instance_widget_settings.yml +++ b/core/modules/field/migration_templates/d7_field_instance_widget_settings.yml @@ -2,6 +2,9 @@ id: d7_field_instance_widget_settings label: Field instance widget configuration migration_tags: - Drupal 7 +class: Drupal\migrate_drupal\Plugin\migrate\CckMigration +cck_plugin_method: processFieldWidget +core: 7 source: plugin: d7_field_instance_per_form_display constants: diff --git a/core/modules/field/src/Plugin/migrate/process/FieldType.php b/core/modules/field/src/Plugin/migrate/process/FieldType.php new file mode 100644 index 0000000..e382304 --- /dev/null +++ b/core/modules/field/src/Plugin/migrate/process/FieldType.php @@ -0,0 +1,89 @@ +cckPluginManager = $cck_plugin_manager; + $this->migration = $migration; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + $migration = func_get_args()[4]; + if (!($migration instanceof MigrationInterface)) { + $migration = NULL; + } + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('plugin.manager.migrate.cckfield'), + $migration + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $field_type = is_array($value) ? $value[0] : $value; + + try { + return $this->cckPluginManager->createInstance($field_type, [], $this->migration)->getFieldType($row); + } + catch (PluginNotFoundException $e) { + return parent::transform($value, $migrate_executable, $row, $destination_property); + } + } + +} diff --git a/core/modules/field/src/Plugin/migrate/process/d6/FieldType.php b/core/modules/field/src/Plugin/migrate/process/d6/FieldType.php deleted file mode 100644 index 3511a3a..0000000 --- a/core/modules/field/src/Plugin/migrate/process/d6/FieldType.php +++ /dev/null @@ -1,71 +0,0 @@ -cckPluginManager = $cck_plugin_manager; - } - - /** - * {@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') - ); - } - - /** - * {@inheritdoc} - */ - public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { - list ($field_type, $widget_type) = $value; - - try { - return $this->cckPluginManager->createInstance($field_type) - ->getFieldType($row); - } - catch (PluginNotFoundException $e) { - return parent::transform($value, $migrate_executable, $row, $destination_property); - } - } - -} diff --git a/core/modules/file/src/Plugin/migrate/cckfield/d6/FileField.php b/core/modules/file/src/Plugin/migrate/cckfield/d6/FileField.php index 066d6e1..d63b9c2 100644 --- a/core/modules/file/src/Plugin/migrate/cckfield/d6/FileField.php +++ b/core/modules/file/src/Plugin/migrate/cckfield/d6/FileField.php @@ -8,7 +8,8 @@ /** * @MigrateCckField( - * id = "filefield" + * id = "filefield", + * core = {6} * ) */ class FileField extends CckFieldPluginBase { diff --git a/core/modules/file/src/Plugin/migrate/cckfield/d7/FileField.php b/core/modules/file/src/Plugin/migrate/cckfield/d7/FileField.php index 6e3780e..d0b1335 100644 --- a/core/modules/file/src/Plugin/migrate/cckfield/d7/FileField.php +++ b/core/modules/file/src/Plugin/migrate/cckfield/d7/FileField.php @@ -9,6 +9,7 @@ /** * @MigrateCckField( * id = "file", + * core = {7} * ) */ class FileField extends CckFieldPluginBase { diff --git a/core/modules/file/src/Plugin/migrate/cckfield/d7/ImageField.php b/core/modules/file/src/Plugin/migrate/cckfield/d7/ImageField.php index 7a82a15..7f47caa 100644 --- a/core/modules/file/src/Plugin/migrate/cckfield/d7/ImageField.php +++ b/core/modules/file/src/Plugin/migrate/cckfield/d7/ImageField.php @@ -7,7 +7,8 @@ /** * @MigrateCckField( - * id = "image" + * id = "image", + * core = {7} * ) */ class ImageField extends CckFieldPluginBase { diff --git a/core/modules/link/src/Plugin/migrate/cckfield/LinkField.php b/core/modules/link/src/Plugin/migrate/cckfield/LinkField.php index f3b5d14..47b3830 100644 --- a/core/modules/link/src/Plugin/migrate/cckfield/LinkField.php +++ b/core/modules/link/src/Plugin/migrate/cckfield/LinkField.php @@ -7,7 +7,11 @@ /** * @MigrateCckField( - * id = "link" + * id = "link", + * core = {6}, + * type_map = { + * "link_field" = "link" + * } * ) */ class LinkField extends CckFieldPluginBase { diff --git a/core/modules/migrate_drupal/migrate_drupal.services.yml b/core/modules/migrate_drupal/migrate_drupal.services.yml index 2807b5f..71a0b27 100644 --- a/core/modules/migrate_drupal/migrate_drupal.services.yml +++ b/core/modules/migrate_drupal/migrate_drupal.services.yml @@ -1,6 +1,6 @@ services: plugin.manager.migrate.cckfield: - class: Drupal\migrate\Plugin\MigratePluginManager + class: Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManager arguments: - cckfield - '@container.namespaces' diff --git a/core/modules/migrate_drupal/src/Annotation/MigrateCckField.php b/core/modules/migrate_drupal/src/Annotation/MigrateCckField.php index 39b545f..5e5a500 100644 --- a/core/modules/migrate_drupal/src/Annotation/MigrateCckField.php +++ b/core/modules/migrate_drupal/src/Annotation/MigrateCckField.php @@ -8,9 +8,10 @@ * Defines a cckfield plugin annotation object. * * cckfield plugins are variously responsible for handling the migration of - * CCK fields from Drupal 6 to Drupal 8. They are allowed to alter CCK-related - * migrations when migrations are being generated, and can compute destination - * field types for individual fields during the actual migration process. + * CCK fields from Drupal 6 to Drupal 8, and Field API fields from Drupal 7 + * to Drupal 8. They are allowed to alter CCK-related migrations when migrations + * are being generated, and can compute destination field types for individual + * fields during the actual migration process. * * Plugin Namespace: Plugin\migrate\cckfield * @@ -19,6 +20,17 @@ class MigrateCckField extends Plugin { /** + * @inheritdoc + */ + public function __construct($values) { + parent::__construct($values); + // Provide default value for core property, in case it's missing. + if (empty($this->definition['core'])) { + $this->definition['core'] = [6]; + } + } + + /** * The plugin ID. * * @var string @@ -32,4 +44,11 @@ class MigrateCckField extends Plugin { */ public $type_map = []; + /** + * The Drupal core version(s) this plugin applies to. + * + * @var int[] + */ + public $core = []; + } diff --git a/core/modules/migrate_drupal/src/Plugin/MigrateCckFieldPluginManager.php b/core/modules/migrate_drupal/src/Plugin/MigrateCckFieldPluginManager.php new file mode 100644 index 0000000..2679fa5 --- /dev/null +++ b/core/modules/migrate_drupal/src/Plugin/MigrateCckFieldPluginManager.php @@ -0,0 +1,49 @@ +getPluginDefinition()['core'])) { + $core = $migration->getPluginDefinition()['core']; + } + foreach ($this->getDefinitions() as $plugin_id => $definition) { + if (in_array($core, $definition['core'])) { + if (array_key_exists($field_type, $definition['type_map']) || $field_type === $plugin_id) { + return parent::createInstance($plugin_id, $configuration, $migration); + } + } + } + throw new PluginNotFoundException($field_type); + } + +} diff --git a/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/migrate_cckfield_plugin_manager_test.info.yml b/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/migrate_cckfield_plugin_manager_test.info.yml new file mode 100644 index 0000000..8449e1a --- /dev/null +++ b/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 diff --git a/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/src/Plugin/migrate/cckfield/D6FileField.php b/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/src/Plugin/migrate/cckfield/D6FileField.php new file mode 100644 index 0000000..5e58e62 --- /dev/null +++ b/core/modules/migrate_drupal/tests/modules/migrate_cckfield_plugin_manager_test/src/Plugin/migrate/cckfield/D6FileField.php @@ -0,0 +1,34 @@ +assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d6\\FileField', get_class($plugin_manager->createInstance('filefield', ['core' => 6]))); + + try { + // If this test passes, createInstance will raise a + // PluginNotFoundException and we'll never reach fail(). + $plugin_manager->createInstance('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('Drupal\\file\\Plugin\\migrate\\cckfield\\d7\\ImageField', get_class($plugin_manager->createInstance('image', ['core' => 7]))); + $this->assertIdentical('Drupal\\file\\Plugin\\migrate\\cckfield\\d7\\FileField', get_class($plugin_manager->createInstance('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('Drupal\\text\\Plugin\\migrate\\cckfield\\TextField', get_class($plugin_manager->createInstance('text', ['core' => 6]))); + $this->assertIdentical('Drupal\\text\\Plugin\\migrate\\cckfield\\TextField', get_class($plugin_manager->createInstance('text', ['core' => 7]))); + + // 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]))); + + try { + // If this test passes, createInstance will raise a + // PluginNotFoundException and we'll never reach fail(). + $plugin_manager->createInstance('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."); + } + } + +} diff --git a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php index d586efd..8fe1b14 100644 --- a/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D6NodeDeriver.php @@ -111,7 +111,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { $field_type = $info['type']; if ($this->cckPluginManager->hasDefinition($info['type'])) { if (!isset($this->cckPluginCache[$field_type])) { - $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, [], $migration); + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, ['core' => 6], $migration); } $this->cckPluginCache[$field_type] ->processCckFieldValues($migration, $field_name, $info); diff --git a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php index 3809c03..5790daf 100644 --- a/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php +++ b/core/modules/node/src/Plugin/migrate/D7NodeDeriver.php @@ -99,7 +99,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { $field_type = $info['type']; if ($this->cckPluginManager->hasDefinition($field_type)) { if (!isset($this->cckPluginCache[$field_type])) { - $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, [], $migration); + $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, ['core' => 7], $migration); } $this->cckPluginCache[$field_type] ->processCckFieldValues($migration, $field_name, $info); diff --git a/core/modules/taxonomy/src/Plugin/migrate/cckfield/TaxonomyTermReference.php b/core/modules/taxonomy/src/Plugin/migrate/cckfield/TaxonomyTermReference.php index 6971b54..c3035c4 100644 --- a/core/modules/taxonomy/src/Plugin/migrate/cckfield/TaxonomyTermReference.php +++ b/core/modules/taxonomy/src/Plugin/migrate/cckfield/TaxonomyTermReference.php @@ -7,7 +7,11 @@ /** * @MigrateCckField( - * id = "taxonomy_term_reference" + * id = "taxonomy_term_reference", + * type_map = { + * "taxonomy_term_reference" = "entity_reference" + * }, + * core = {6,7} * ) */ class TaxonomyTermReference extends CckFieldPluginBase { diff --git a/core/modules/text/src/Plugin/migrate/cckfield/TextField.php b/core/modules/text/src/Plugin/migrate/cckfield/TextField.php index 523ffca..cc4c1ec 100644 --- a/core/modules/text/src/Plugin/migrate/cckfield/TextField.php +++ b/core/modules/text/src/Plugin/migrate/cckfield/TextField.php @@ -8,7 +8,13 @@ /** * @MigrateCckField( - * id = "text" + * id = "text", + * type_map = { + * "text" = "text", + * "text_long" = "text_long", + * "text_with_summary" = "text_with_summary" + * }, + * core = {6,7} * ) */ class TextField extends CckFieldPluginBase { @@ -113,6 +119,7 @@ public function getFieldType(Row $row) { case 'text_textarea': return 'text_long'; default: + return parent::getFieldType($row); break; } }