diff --git a/core/modules/filter/migration_templates/d6_filter_format.yml b/core/modules/filter/migration_templates/d6_filter_format.yml index 6b767f2..a5f4388 100644 --- a/core/modules/filter/migration_templates/d6_filter_format.yml +++ b/core/modules/filter/migration_templates/d6_filter_format.yml @@ -16,11 +16,16 @@ process: key: '@id' process: id: - plugin: static_map - default_value: filter_null + # If the filter ID cannot be mapped, it will be passed through + # unchanged because the bypass flag is set. The filter_id plugin + # will flatten the input value and default it to filter_null (the + # fallback filter plugin ID) if the flattened input value is not + # a valid plugin ID. + plugin: filter_id source: - module - delta + bypass: true map: filter: - filter_html diff --git a/core/modules/filter/migration_templates/d7_filter_format.yml b/core/modules/filter/migration_templates/d7_filter_format.yml index 2b44a80..c0710b5 100644 --- a/core/modules/filter/migration_templates/d7_filter_format.yml +++ b/core/modules/filter/migration_templates/d7_filter_format.yml @@ -15,11 +15,16 @@ process: key: '@id' process: id: - plugin: static_map + # If the filter ID cannot be mapped, it will pass through unmodified + # because the bypass flag is set. When the user actually tries to + # view text through an invalid filter plugin, the filter system will + # fall back to filter_null and display a helpful error message. + plugin: filter_id bypass: true source: name - map: - php_code: filter_null + # No need to map anything -- filter plugin IDs haven't changed since + # Drupal 7. + map: { } settings: plugin: filter_settings source: settings diff --git a/core/modules/filter/src/Plugin/migrate/process/FilterID.php b/core/modules/filter/src/Plugin/migrate/process/FilterID.php new file mode 100644 index 0000000..2656986 --- /dev/null +++ b/core/modules/filter/src/Plugin/migrate/process/FilterID.php @@ -0,0 +1,90 @@ +filterManager = $filter_manager; + $this->stringTranslation = $translator; + } + + /** + * {@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.filter'), + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + $plugin_id = parent::transform($value, $migrate_executable, $row, $destination_property); + + // If the static map is bypassed on failure, the returned plugin ID will be + // an array if $value was. Plugin IDs cannot be arrays, so flatten it before + // passing it into the filter manager. + if (is_array($plugin_id)) { + $plugin_id = implode(':', $plugin_id); + } + + if ($this->filterManager->hasDefinition($plugin_id)) { + return $plugin_id; + } + else { + $fallback = $this->filterManager->getFallbackPluginId($plugin_id); + + $message = $this->t('Filter @plugin_id could not be mapped to an existing filter plugin; defaulting to @fallback.', [ + '@plugin_id' => $plugin_id, + '@fallback' => $fallback, + ]); + $migrate_executable->saveMessage((string) $message, MigrationInterface::MESSAGE_WARNING); + + return $fallback; + } + } + +} diff --git a/core/modules/filter/tests/src/Kernel/Migrate/d6/MigrateFilterFormatTest.php b/core/modules/filter/tests/src/Kernel/Migrate/d6/MigrateFilterFormatTest.php index ed5f0fb..81faf3e 100644 --- a/core/modules/filter/tests/src/Kernel/Migrate/d6/MigrateFilterFormatTest.php +++ b/core/modules/filter/tests/src/Kernel/Migrate/d6/MigrateFilterFormatTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\filter\Kernel\Migrate\d6; use Drupal\filter\Entity\FilterFormat; +use Drupal\filter\FilterFormatInterface; use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase; /** @@ -39,14 +40,18 @@ public function testFilterFormat() { $this->assertFalse(isset($filters['filter_html_image_secure'])); // Check variables migrated into filter. - $this->assertIdentical('