diff --git a/insert.module b/insert.module index 76b3747..e9cb16d 100644 --- a/insert.module +++ b/insert.module @@ -1089,13 +1089,38 @@ function insert_migrate_prepare_row(Row $row, MigrateSourceInterface $source, Mi ->condition('fci.bundle', $row->getSourceProperty('bundle')) ->execute() ->fetchField(); - $insert_enabled = $result !== FALSE ? unserialize($result) : FALSE; - if ($insert_enabled) { + $insert_settings = $result !== FALSE ? unserialize($result) : FALSE; + if ($insert_settings['widget']['settings']['insert'] && $insert_settings['widget']['settings']['insert_styles']) { + foreach ($insert_settings['widget']['settings']['insert_styles'] as $style_key => $style_value) { + $style_key = getTargetStyleFromSource($style_key); + $insert_settings['styles'][$style_key] = $style_value ? $style_key : 0; + } $row->setSourceProperty( 'insert_config', [ - 'style' => $insert_enabled['widget']['settings']['insert_styles'], - 'default' => $insert_enabled['widget']['settings']['insert_default'], - 'width' => $insert_enabled['widget']['settings']['insert_width'], + 'styles' => $insert_settings['styles'], + 'default' => $insert_settings['widget']['settings']['insert_default'], + 'class' => $insert_settings['widget']['settings']['insert_class'], + 'width' => $insert_settings['widget']['settings']['insert_width'], ]); } } + +/** + * Helper function to reformat styles. + * + * @param null $source_style + * + * @return string + */ +function getTargetStyleFromSource($source_style = NULL): string { + // Map 'auto' to 'insert__auto'. + if ($source_style === 'auto') { + return 'insert__auto'; + } + // Map D7 image styles to D9 image style config name. + if (preg_match('/^image_(.*)$/', $source_style, $matches)) { + return $matches[1]; + } + + return $source_style ?? INSERT_DEFAULT_SETTINGS['default']; +} diff --git a/migrations/d7_field_instance_widget_insert_settings.yml b/migrations/d7_field_instance_widget_insert_settings.yml new file mode 100644 index 0000000..763b320 --- /dev/null +++ b/migrations/d7_field_instance_widget_insert_settings.yml @@ -0,0 +1,45 @@ +# This file basically is a copy of d7_field_instance_widget_settings.yml with +# unnecessary parts removed and third_party_settings getting filled with Insert +# settings. +id: d7_field_instance_widget_insert_settings +label: Field instance widget Insert configuration +migration_tags: + - Drupal 7 + - Configuration +source: + plugin: d7_field_instance_per_form_display + constants: + form_mode: default + source_module: insert +process: + field_type_exists: + - + plugin: migration_lookup + migration: d7_field + source: + - field_name + - entity_type + - + plugin: extract + index: + - 0 + - + plugin: skip_on_empty + method: row + bundle: + plugin: static_map + source: bundle + bypass: true + map: + comment_node_forum: comment_forum + form_mode: 'constants/form_mode' + field_name: field_name + entity_type: entity_type + 'options/third_party_settings': + plugin: field_instance_widget_insert_settings + source: 'widget/settings' +destination: + plugin: component_entity_form_display_insert +migration_dependencies: + required: + - d7_field_instance diff --git a/src/Plugin/migrate/destination/PerComponentEntityFormDisplayInsert.php b/src/Plugin/migrate/destination/PerComponentEntityFormDisplayInsert.php new file mode 100644 index 0000000..7079bdd --- /dev/null +++ b/src/Plugin/migrate/destination/PerComponentEntityFormDisplayInsert.php @@ -0,0 +1,43 @@ +getIds()) as $id) { + $values[$id] = $row->getDestinationProperty($id); + } + $entity = $this->getEntity($values['entity_type'], $values['bundle'], $values[static::MODE_NAME]); + + $insert_settings = $row->getDestinationProperty('options/third_party_settings/insert'); + // Add Insert module third party settings to field settings: + if ( + $insert_settings && + $field_component = $entity->getComponent($values['field_name']) + ) { + $field_component['third_party_settings']['insert'] = $insert_settings; + $entity->setComponent($values['field_name'], $field_component); + } + + $entity->save(); + return array_values($values); + } + +} diff --git a/src/Plugin/migrate/process/FieldInstanceWidgetInsertSettings.php b/src/Plugin/migrate/process/FieldInstanceWidgetInsertSettings.php new file mode 100644 index 0000000..9158564 --- /dev/null +++ b/src/Plugin/migrate/process/FieldInstanceWidgetInsertSettings.php @@ -0,0 +1,77 @@ +getInsertSettings($value); + } + + /** + * Merges the default D8 and specified D7 Insert module settings for a widget + * type. + * + * @param array $widget_settings + * The widget settings from D7 for this widget. + * + * @return array[] + */ + public function getInsertSettings(array $widget_settings) { + if (!isset($widget_settings['insert'])) { + return []; + } + + // While Insert features a dedicated "enabled" checkbox + // ($widget_settings['insert']) in D7, Insert is enabled whenever one or + // more styles are activated in D8. Therefore, if Insert is disabled in D7, + // deactivate all styles in D8. + if ($widget_settings['insert'] && $widget_settings['insert_styles']) { + foreach ($widget_settings['insert_styles'] as $style_key => $style_value) { + $style_key = static::getTargetStyleFromSource($style_key); + $insert_settings['styles'][$style_key] = $style_value ? $style_key : 0; + } + } + + return [ + 'insert' => [ + 'styles' => $insert_settings['styles'], + 'default' => $widget_settings['insert_default'], + 'class' => $widget_settings['insert_class'], + 'width' => $widget_settings['insert_width'], + ], + ]; + } + + /** + * + */ + public static function getTargetStyleFromSource($source_style = NULL): string { + // Map 'auto' to 'insert__auto'. + if ($source_style === 'auto') { + return 'insert__auto'; + } + // Map D7 image styles to D9 image style config name. + if (preg_match('/^image_(.*)$/', $source_style, $matches)) { + return $matches[1]; + } + + return $source_style ?? INSERT_DEFAULT_SETTINGS['default']; + } + +} diff --git a/tests/src/Kernel/InsertMigrateTest.php b/tests/src/Kernel/InsertMigrateTest.php index 3f848a4..67a7683 100644 --- a/tests/src/Kernel/InsertMigrateTest.php +++ b/tests/src/Kernel/InsertMigrateTest.php @@ -62,6 +62,7 @@ class InsertMigrateTest extends MigrateDrupal7TestBase { * Tests insert settings migration. */ public function testInsertMigration(): void { + $this->startCollectingMessages(); $this->executeMigrations([ 'd7_node_type', 'd7_comment_type', @@ -71,53 +72,46 @@ class InsertMigrateTest extends MigrateDrupal7TestBase { 'd7_field_instance', 'd7_field_instance_widget_settings', ]); + $this->assertNoMigrationMessages(); $expected_article_config = [ - 'style' => [ + 'styles' => [ 'icon_link' => 'icon_link', 'image' => 'image', - 'image_large' => 'image_large', - 'image_medium' => 'image_medium', - 'image_thumbnail' => 'image_thumbnail', - 'auto' => 0, + 'large' => 'large', + 'medium' => 'medium', + 'thumbnail' => 'thumbnail', + 'insert__auto' => 0, 'link' => 0, ], 'default' => 'image_thumbnail', + 'class' => 'classs', 'width' => '105', ]; $config_after_article = $this->config('core.entity_form_display.node.article.default')->getRawData(); $this->assertSame($expected_article_config, $config_after_article['content']['field_image']['third_party_settings']['insert']); - $expected_gallery_config = [ - 'style' => [ - 'auto' => 'auto', - 'link' => 'link', - 'image' => 'image', - 'icon_link' => 0, - 'image_large' => 0, - 'image_medium' => 0, - 'image_thumbnail' => 0, - ], - 'default' => 'image_large', - 'width' => '80', - ]; $config_after_gallery = $this->config('core.entity_form_display.node.gallery.default')->getRawData(); - $this->assertSame($expected_gallery_config, $config_after_gallery['content']['field_file']['third_party_settings']['insert']); + $this->assertArrayNotHasKey('insert', $config_after_gallery['content']['field_file']['third_party_settings']); - $expected_comment_config = [ - 'style' => [ - 'auto' => 'auto', - 'icon_link' => 'icon_link', - 'image_large' => 'image_large', - 'image_thumbnail' => 'image_thumbnail', - 'link' => 0, - 'image' => 0, - 'image_medium' => 0, - ], - 'default' => 'image_medium', - 'width' => '100', - ]; $config_after_comment = $this->config('core.entity_form_display.comment.comment_node_article.default')->getRawData(); - $this->assertSame($expected_comment_config, $config_after_comment['content']['field_cmnt_image']['third_party_settings']['insert']); + $this->assertArrayNotHasKey('insert', $config_after_comment['content']['field_cmnt_image']['third_party_settings']); + } + + /** + * DX. + */ + public function assertNoMigrationMessages() { + $messages_as_strings = []; + $dummies = []; + foreach ($this->migrateMessages as $type => $messages) { + foreach ($messages as $message) { + $messages_as_strings[$type][] = (string) $message; + } + + $dummies[$type] = array_fill(0, count($messages), '...'); + } + + $this->assertEquals($dummies, $messages_as_strings); } }