diff --git a/core/modules/datetime/src/Plugin/migrate/field/DateField.php b/core/modules/datetime/src/Plugin/migrate/field/DateField.php index 238d615520..a50063bfd5 100644 --- a/core/modules/datetime/src/Plugin/migrate/field/DateField.php +++ b/core/modules/datetime/src/Plugin/migrate/field/DateField.php @@ -101,8 +101,19 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field * {@inheritdoc} */ public function transformFieldStorageSettings(Row $row) { - $settings['datetime_type'] = 'datetime'; - return $settings; + if ($row->get('plugin') == 'd6_field') { + return []; + } + $value = $row->getSourceProperty('settings'); + $collected_date_attributes = is_numeric(array_keys($value['granularity'])[0]) + ? $value['granularity'] + : array_keys(array_filter($value['granularity'])); + + $value['datetime_type'] = 'datetime'; + if (empty(array_intersect($collected_date_attributes, ['hour', 'minute', 'second']))) { + $value['datetime_type'] = 'date'; + } + return $value; } } diff --git a/core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php b/core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php index b6c21b2139..b13e531618 100644 --- a/core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php +++ b/core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php @@ -2,6 +2,7 @@ namespace Drupal\field\Plugin\migrate\field\d7; +use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase; /** @@ -30,4 +31,50 @@ public function getFieldFormatterMap() { ]; } + /** + * {@inheritdoc} + */ + public function transformFieldInstanceSettings(Row $row) { + // Get entityreference handler settings from source field configuration. + $field_definition = $row->get('field_definition'); + $field_data = unserialize($field_definition['data']); + + $field_settings = $field_data['settings']; + $instance_settings['handler'] = 'default:' . $field_settings['target_type']; + // Transform the sort settings to D8 structure. + $sort = [ + 'field' => '_none', + 'direction' => 'ASC', + ]; + if (!empty(array_filter($field_settings['handler_settings']['sort']))) { + if ($field_settings['handler_settings']['sort']['type'] == "property") { + $sort = [ + 'field' => $field_settings['handler_settings']['sort']['property'], + 'direction' => $field_settings['handler_settings']['sort']['direction'], + ]; + } + elseif ($field_settings['handler_settings']['sort']['type'] == "field") { + $sort = [ + 'field' => $field_settings['handler_settings']['sort']['field'], + 'direction' => $field_settings['handler_settings']['sort']['direction'], + ]; + } + } + if (empty($field_settings['handler_settings']['target_bundles'])) { + $field_settings['handler_settings']['target_bundles'] = NULL; + } + $field_settings['handler_settings']['sort'] = $sort; + $instance_settings['handler_settings'] = $field_settings['handler_settings']; + + return $instance_settings; + } + + /** + * {@inheritdoc} + */ + public function transformFieldStorageSettings(Row $row) { + $settings['target_type'] = $row->get('settings/target_type'); + return $settings; + } + } diff --git a/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php index f132474497..4430b1ef7b 100644 --- a/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php +++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldInstanceSettings.php @@ -2,105 +2,76 @@ namespace Drupal\field\Plugin\migrate\process\d7; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; +use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @MigrateProcessPlugin( * id = "d7_field_instance_settings" * ) */ -class FieldInstanceSettings extends ProcessPluginBase { +class FieldInstanceSettings extends ProcessPluginBase implements ContainerFactoryPluginInterface { /** - * {@inheritdoc} + * The field plugin manager. + * + * @var \Drupal\Component\Plugin\PluginManagerInterface */ - public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { - list($instance_settings, $widget_settings, $field_definition) = $value; - $widget_type = $widget_settings['type']; + protected $fieldPluginManager; - $field_data = unserialize($field_definition['data']); + /** + * Constructs a FieldSettings plugin. + * + * @param array $configuration + * The plugin configuration. + * @param string $plugin_id + * The plugin ID. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Component\Plugin\PluginManagerInterface $field_plugin_manager + * The field plugin manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $field_plugin_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->fieldPluginManager = $field_plugin_manager; + } - // Get taxonomy term reference handler settings from allowed values. - if ($row->getSourceProperty('type') == 'taxonomy_term_reference') { - $instance_settings['handler_settings']['sort'] = [ - 'field' => '_none', - ]; - $allowed_values = $row->get('@allowed_values'); - foreach ($allowed_values as $allowed_value) { - foreach ($allowed_value as $vocabulary) { - $instance_settings['handler_settings']['target_bundles'][$vocabulary] = $vocabulary; - } - } - } + /** + * {@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.field') + ); + } - // Get entityreference handler settings from source field configuration. - if ($row->getSourceProperty('type') == "entityreference") { - $field_settings = $field_data['settings']; - $instance_settings['handler'] = 'default:' . $field_settings['target_type']; - // Transform the sort settings to D8 structure. - $sort = [ - 'field' => '_none', - 'direction' => 'ASC', - ]; - if (!empty(array_filter($field_settings['handler_settings']['sort']))) { - if ($field_settings['handler_settings']['sort']['type'] == "property") { - $sort = [ - 'field' => $field_settings['handler_settings']['sort']['property'], - 'direction' => $field_settings['handler_settings']['sort']['direction'], - ]; - } - elseif ($field_settings['handler_settings']['sort']['type'] == "field") { - $sort = [ - 'field' => $field_settings['handler_settings']['sort']['field'], - 'direction' => $field_settings['handler_settings']['sort']['direction'], - ]; - } - } - if (empty($field_settings['handler_settings']['target_bundles'])) { - $field_settings['handler_settings']['target_bundles'] = NULL; - } - $field_settings['handler_settings']['sort'] = $sort; - $instance_settings['handler_settings'] = $field_settings['handler_settings']; + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $original_field_type = $row->getSourceProperty('type'); + if ($original_field_type == 'text') { + $original_field_type = 'd7_' . $original_field_type; } - - if ($row->getSourceProperty('type') == 'node_reference') { - $instance_settings['handler'] = 'default:node'; - - $instance_settings['handler_settings'] = [ - 'sort' => [ - 'field' => '_none', - 'direction' => 'ASC', - ], - 'target_bundles' => array_filter($field_data['settings']['referenceable_types'] ?? []), - ]; + try { + return $this->fieldPluginManager->createInstance($original_field_type, ['core' => 7]) + ->transformFieldInstanceSettings($row); + } + catch (PluginNotFoundException $e) { } - if ($row->getSourceProperty('type') == 'user_reference') { - $instance_settings['handler'] = 'default:user'; - - $instance_settings['handler_settings'] = [ - 'include_anonymous' => TRUE, - 'filter' => [ - 'type' => '_none', - ], - 'sort' => [ - 'field' => '_none', - 'direction' => 'ASC', - ], - 'auto_create' => FALSE, - ]; + [$instance_settings, $widget_settings, $field_definition] = $value; + $widget_type = $widget_settings['type']; - if ($row->hasSourceProperty('roles')) { - $instance_settings['handler_settings']['filter']['type'] = 'role'; - foreach ($row->get('roles') as $role) { - $instance_settings['handler_settings']['filter']['role'] = [ - $role['name'] => $role['name'], - ]; - } - } - } + $field_data = unserialize($field_definition['data']); // Get the labels for the list_boolean type. if ($row->getSourceProperty('type') === 'list_boolean') { diff --git a/core/modules/field/src/Plugin/migrate/process/d7/FieldSettings.php b/core/modules/field/src/Plugin/migrate/process/d7/FieldSettings.php index a18e4fac99..f5a9554452 100644 --- a/core/modules/field/src/Plugin/migrate/process/d7/FieldSettings.php +++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldSettings.php @@ -2,32 +2,77 @@ namespace Drupal\field\Plugin\migrate\process\d7; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; +use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @MigrateProcessPlugin( * id = "d7_field_settings" * ) */ -class FieldSettings extends ProcessPluginBase { +class FieldSettings extends ProcessPluginBase implements ContainerFactoryPluginInterface { + + /** + * The field plugin manager. + * + * @var \Drupal\Component\Plugin\PluginManagerInterface + */ + protected $fieldPluginManager; + + /** + * Constructs a FieldSettings plugin. + * + * @param array $configuration + * The plugin configuration. + * @param string $plugin_id + * The plugin ID. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Component\Plugin\PluginManagerInterface $field_plugin_manager + * The field plugin manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $field_plugin_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->fieldPluginManager = $field_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.field') + ); + } /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $original_field_type = $row->getSourceProperty('type'); + if ($original_field_type == 'text') { + $original_field_type = 'd7_' . $original_field_type; + } + try { + return $this->fieldPluginManager->createInstance($original_field_type, ['core' => 7]) + ->transformFieldStorageSettings($row); + } + catch (PluginNotFoundException $e) { + // + } + $value = $row->getSourceProperty('settings'); switch ($row->getSourceProperty('type')) { - case 'image': - if (!is_array($value['default_image'])) { - $value['default_image'] = ['uuid' => '']; - } - break; - case 'date': - case 'datetime': case 'datestamp': $collected_date_attributes = is_numeric(array_keys($value['granularity'])[0]) ? $value['granularity'] @@ -37,14 +82,6 @@ public function transform($value, MigrateExecutableInterface $migrate_executable } break; - case 'taxonomy_term_reference': - $value['target_type'] = 'taxonomy_term'; - break; - - case 'user_reference': - $value['target_type'] = 'user'; - break; - default: break; } diff --git a/core/modules/image/src/Plugin/migrate/field/d7/ImageField.php b/core/modules/image/src/Plugin/migrate/field/d7/ImageField.php index 1d82925718..ae3e0b1f1d 100644 --- a/core/modules/image/src/Plugin/migrate/field/d7/ImageField.php +++ b/core/modules/image/src/Plugin/migrate/field/d7/ImageField.php @@ -3,6 +3,7 @@ namespace Drupal\image\Plugin\migrate\field\d7; use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase; /** @@ -32,5 +33,15 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field ]; $migration->mergeProcessOfProperty($field_name, $process); } + /** + * {@inheritdoc} + */ + public function transformFieldStorageSettings(Row $row) { + $value = $row->getSourceProperty('settings'); + if (!is_array($value['default_image'])) { + $value['default_image'] = ['uuid' => '']; + } + return $value; + } } diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/NodeReference.php b/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/NodeReference.php index 91d3e219d3..8c4d8adc5f 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/NodeReference.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/NodeReference.php @@ -2,6 +2,7 @@ namespace Drupal\migrate_drupal\Plugin\migrate\field\d7; +use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\field\ReferenceBase; /** @@ -53,4 +54,29 @@ public function getFieldFormatterMap() { ]; } + /** + * {@inheritdoc} + */ + public function transformFieldInstanceSettings(Row $row) { + $instance_settings['handler'] = 'default:node'; + + $instance_settings['handler_settings'] = [ + 'sort' => [ + 'field' => '_none', + 'direction' => 'ASC', + ], + 'target_bundles' => array_filter($field_data['settings']['referenceable_types'] ?? []), + ]; + return $instance_settings; + } + + /** + * {@inheritdoc} + */ + public function transformFieldStorageSettings(Row $row) { + $settings['target_type'] = 'node'; + return $settings; + } + + } diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/UserReference.php b/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/UserReference.php index 01bf09cec7..c53924da14 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/UserReference.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/field/d7/UserReference.php @@ -3,6 +3,7 @@ namespace Drupal\migrate_drupal\Plugin\migrate\field\d7; use Drupal\migrate\Plugin\MigrationInterface; +use Drupal\migrate\Row; use Drupal\migrate_drupal\Plugin\migrate\field\ReferenceBase; /** @@ -73,4 +74,52 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field } + /** + * {@inheritdoc} + */ + public function transformFieldStorageSettings(Row $row) { + $settings['target_type'] = 'user'; + return $settings; + } + + /** + * {@inheritdoc} + */ + public function alterFieldInstanceMigration(MigrationInterface $migration) { + parent::alterFieldInstanceMigration($migration); + + $migration_dependencies = $migration->getMigrationDependencies(); + $migration_dependencies['required'][] = $this->userTypeMigration; + $migration->set('migration_dependencies', $migration_dependencies); + } + + /** + * {@inheritdoc} + */ + public function transformFieldInstanceSettings(Row $row) { + $instance_settings['handler'] = 'default:user'; + + $instance_settings['handler_settings'] = [ + 'include_anonymous' => TRUE, + 'filter' => [ + 'type' => '_none', + ], + 'sort' => [ + 'field' => '_none', + 'direction' => 'ASC', + ], + 'auto_create' => FALSE, + ]; + + if ($row->hasSourceProperty('roles')) { + $instance_settings['handler_settings']['filter']['type'] = 'role'; + foreach ($row->get('roles') as $role) { + $instance_settings['handler_settings']['filter']['role'] = [ + $role['name'] => $role['name'], + ]; + } + } + return $instance_settings; + } + } diff --git a/core/modules/taxonomy/src/Plugin/migrate/field/TaxonomyTermReference.php b/core/modules/taxonomy/src/Plugin/migrate/field/TaxonomyTermReference.php index 1696182275..6b14d7a0cd 100644 --- a/core/modules/taxonomy/src/Plugin/migrate/field/TaxonomyTermReference.php +++ b/core/modules/taxonomy/src/Plugin/migrate/field/TaxonomyTermReference.php @@ -75,4 +75,20 @@ public function defineValueProcessPipeline(MigrationInterface $migration, $field $migration->setProcessOfProperty($field_name, $process); } + /** + * {@inheritdoc} + */ + public function transformFieldInstanceSettings(Row $row) { + $instance_settings['handler_settings']['sort'] = [ + 'field' => '_none', + ]; + $allowed_values = $row->get('@allowed_values'); + foreach ($allowed_values as $allowed_value) { + foreach ($allowed_value as $vocabulary) { + $instance_settings['handler_settings']['target_bundles'][$vocabulary] = $vocabulary; + } + } + return $instance_settings; + } + }