FilterID.php

Namespace

Drupal\filter\Plugin\migrate\process

File

core/modules/filter/src/Plugin/migrate/process/FilterID.php

View source
<?php

namespace Drupal\filter\Plugin\migrate\process;

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\migrate\Attribute\MigrateProcess;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\migrate\process\StaticMap;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
// cspell:ignore abbrfilter adsense autofloat biblio cincopa codefilter
// cspell:ignore commonmark deepzoom emogrifier emptyparagraphkiller forena
// cspell:ignore gotwo htmlpurifier htmltidy intlinks lazyloader linktitle
// cspell:ignore multicolumn multilink mytube openlayers opengraph sanitizable
// cspell:ignore shortcode spamspan typogrify wordfilter xbbcode

/**
 * Determines the filter ID.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. There is no
 *    replacement.
 *
 * @see https://www.drupal.org/node/3533560
 */
class FilterID extends StaticMap implements ContainerFactoryPluginInterface {
  
  /**
   * The filter plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface|\Drupal\Component\Plugin\FallbackPluginManagerInterface
   */
  protected $filterManager;
  
  /**
   * FilterID constructor.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $filter_manager
   *   The filter plugin manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translator
   *   (optional) The string translation service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $filter_manager, ?TranslationInterface $translator = NULL) {
    @trigger_error(__CLASS__ . ' is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3533560', E_USER_DEPRECATED);
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->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 {
      if (in_array(static::getSourceFilterType($value), [
        FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
        FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      ], TRUE)) {
        $message = sprintf('Filter %s could not be mapped to an existing filter plugin; omitted since it is a transformation-only filter. Install and configure a successor after the migration.', $plugin_id);
        $migrate_executable->saveMessage($message, MigrationInterface::MESSAGE_INFORMATIONAL);
        $this->stopPipeline();
        return NULL;
      }
      $fallback = $this->filterManager
        ->getFallbackPluginId($plugin_id);
      // @see \Drupal\filter\Plugin\migrate\process\FilterSettings::transform()
      $message = sprintf('Filter %s could not be mapped to an existing filter plugin; defaulting to %s and dropping all settings. Either redo the migration with the module installed that provides an equivalent filter, or modify the text format after the migration to remove this filter if it is no longer necessary.', $plugin_id, $fallback);
      $migrate_executable->saveMessage($message, MigrationInterface::MESSAGE_WARNING);
      return $fallback;
    }
  }
  
  /**
   * Gets the Drupal 8 filter type for a Drupal 7 filter.
   *
   * @param string $filter_id
   *   A Drupal 7 filter ID.
   *
   * @return int
   *   One of:
   *   - FilterInterface::TYPE_MARKUP_LANGUAGE
   *   - FilterInterface::TYPE_HTML_RESTRICTOR
   *   - FilterInterface::TYPE_TRANSFORM_REVERSIBLE
   *   - FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE
   *
   * @see \Drupal\filter\Plugin\FilterInterface::getType()
   */
  protected static function getSourceFilterType($filter_id) {
    // Drupal 7 core filters.
    // - https://git.drupalcode.org/project/drupal/blob/7.69/modules/filter/filter.module#L1229
    // - https://git.drupalcode.org/project/drupal/blob/7.69/modules/php/php.module#L139
    return match ($filter_id) {  'filter_html' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'filter_url' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'filter_autop' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'filter_htmlcorrector' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_html_escape' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'php_code' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'abbrfilter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'ace_editor' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'adsense' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'api_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'api_tokens' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_autofloat' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'bbcode' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'biblio_filter_reference', 'biblio_filter_inline_reference' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'caption' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'caption_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_cincopa' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'ckeditor_blocks' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'ckeditor_filter' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'ckeditor_link_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'ckeditor_swf_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'codefilter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'collapse_text_filter' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'columns_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'commonmark' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'filter_hashtags' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'deepzoom' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'editor_align', 'editor_caption' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'elf' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'filter_emogrifier' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'emptyparagraphkiller' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'entity_embed' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_align' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'ext_link_page' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_html_image_secure' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'filter_transliteration' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'flickr_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'float_filter' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'filter_footnotes' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'forena_report' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_g2' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'geo_filter_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_google_analytics_counter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_google_analytics_referrer' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'gotwo_link' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'h5p_content' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'highlight_js' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'htmLawed' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'htmlpurifier_basic', 'htmlpurifier_advanced' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'htmltidy' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'icon_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'iframe' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'image_resize_filter' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'insert_view' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'intlinks title', 'intlinks hide bad' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'accordion', 'dialog', 'tabs' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'language_sections' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'lazy_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'lazyloader_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_link_node' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'linktitle' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_markdown' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'media_filter', 'media_filter_paragraph_fix' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_mentions' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'menu_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'mobile_codes' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'multicolumn' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'multilink_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'mytube' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'node_embed' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'node_field_embed' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'external_links' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'noreferrer' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'oembed', 'oembed_legacy' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'office_html_strip' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'office_html_convert' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'openlayers' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'opengraph_filter' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'pathologic' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'popup_tags' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'prettify' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'rel_to_abs' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'rollover_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'sanitizable' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'smart_paging_filter', 'smart_paging_filter_autop' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'spamspan' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'mee_scald_widgets' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'script_filter' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'shortcode' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'shortcode_text_corrector' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'smiley' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'filter_svg_embed' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'spoiler' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_toc' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_tables' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'target_filter_url' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'textile' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      'theme_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'filter_tokens' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'transliteration' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'typogrify' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'uuid_link_filter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'wysiwyg', 'wysiwyg_template_cleanup' => FilterInterface::TYPE_HTML_RESTRICTOR,
      'word_link' => FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      'wordfilter' => FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
      'xbbcode' => FilterInterface::TYPE_MARKUP_LANGUAGE,
      default => NULL,
    
    };
  }

}

Classes

Title Deprecated Summary
FilterID

in drupal:11.3.0 and is removed from drupal:12.0.0. There is no replacement.

Determines the filter ID.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.