You are here

abstract class MediaWysiwygPluginBase in Media Migration 8

Base class for media_wysiwyg plugins.

Hierarchy

Expanded class hierarchy of MediaWysiwygPluginBase

6 files declare their use of MediaWysiwygPluginBase
Bean.php in src/Plugin/MediaWysiwyg/Bean.php
FileEntity.php in src/Plugin/MediaWysiwyg/FileEntity.php
General.php in src/Plugin/MediaWysiwyg/General.php
MediaWysiwygPluginBaseTest.php in tests/src/Unit/MediaWysiwygPluginBaseTest.php
MultifieldToParagraphs.php in src/Plugin/MediaWysiwyg/MultifieldToParagraphs.php

... See full list

File

src/MediaWysiwygPluginBase.php, line 17

Namespace

Drupal\media_migration
View source
abstract class MediaWysiwygPluginBase extends PluginBase implements MediaWysiwygInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    $source_entity_type_id = $configuration['source_entity_type_id'] ?? NULL;
    if (!$source_entity_type_id && count($plugin_definition['entity_type_map']) === 1) {
      $source_entity_type_id_keys = array_keys($plugin_definition['entity_type_map']);
      $configuration['source_entity_type_id'] = reset($source_entity_type_id_keys);
    }
    if (empty($configuration['source_entity_type_id']) || empty($plugin_definition['entity_type_map'][$configuration['source_entity_type_id']])) {
      throw new PluginException(sprintf("The MediaWysiwyg plugin instance of class '%s' cannot be instantiated with the following configuration: %s", get_class($this), Variable::export($configuration)));
    }
    $configuration += [
      'destination_entity_type_id' => $plugin_definition['entity_type_map'][$configuration['source_entity_type_id']],
    ];
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function label() {

    // Cast the label to a string since it is a TranslatableMarkup object.
    return (string) $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function process(array $migrations, Row $row) {
    $matching_migration_plugin_ids = MigrationPluginTool::getContentEntityMigrations($migrations, $this->configuration['destination_entity_type_id']);
    foreach (array_keys($matching_migration_plugin_ids) as $migration_plugin_id) {
      $migrations = $this
        ->appendProcessor($migrations, $migration_plugin_id, $row
        ->getSourceProperty('field_name'), $this->configuration['source_entity_type_id']);
    }
    return $migrations;
  }

  /**
   * Appends the media wysiwyg migrate processor to a field.
   *
   * @param array $migrations
   *   The array of migrations.
   * @param string $migration_id
   *   The migration to adjust.
   * @param string $field_name_in_source
   *   The migration field name.
   * @param string $source_entity_type
   *   The source entity type.
   *
   * @return array
   *   The updated array of migrations.
   */
  protected function appendProcessor(array $migrations, string $migration_id, string $field_name_in_source, string $source_entity_type) {
    $extra_processes = [
      [
        'plugin' => 'media_wysiwyg_filter',
      ],
    ];

    // Add 'img_tag_to_embed' and 'ckeditor_link_file_to_linkit' text process
    // plugins and their dependencies when they're required.
    if (isset($migrations['d7_filter_format']) && ($filter_format_source = MigrationDeriverTrait::getSourcePlugin('d7_filter_format')) instanceof DrupalSqlBase) {
      $source_connection = $filter_format_source
        ->getDatabase();
      $file_link_deps_added = FALSE;
      if (!empty(SourceDatabase::getFormatsHavingFileLink($source_connection, $field_name_in_source, $source_entity_type))) {
        $extra_processes[] = [
          'plugin' => 'ckeditor_link_file_to_linkit',
        ];
        $migrations[$migration_id]['migration_dependencies']['required'] = array_unique(array_merge(array_values($migrations[$migration_id]['migration_dependencies']['required'] ?? []), [
          'd7_file_plain',
          'd7_file_entity',
        ]));
        $file_link_deps_added = TRUE;
      }
      if (!empty(SourceDatabase::getFormatsUsingTag($source_connection, 'img', $field_name_in_source, $source_entity_type))) {
        $extra_processes[] = [
          'plugin' => 'img_tag_to_embed',
        ];
        if (!$file_link_deps_added) {
          $migrations[$migration_id]['migration_dependencies']['required'] = array_unique(array_merge(array_values($migrations[$migration_id]['migration_dependencies']['required'] ?? []), [
            'd7_file_plain:image:public',
            'd7_file_entity:image:public',
          ]));
        }
      }
    }
    $migration_processes = $migrations[$migration_id]['process'] ?? [];
    $processes_needs_extra_processor = [];

    // The field might be renamed or completely removed by others: Media
    // Migration should check the processes' source values.
    // @todo Check subprocesses and find a way to handle array sources.
    foreach ($migration_processes as $process_key => $original_process) {
      $associative_process = MigrationPluginTool::getAssociativeMigrationProcess($original_process);
      foreach ($associative_process as $process_plugin_key => $process_plugin_config) {
        if (isset($process_plugin_config['source']) && $process_plugin_config['source'] === $field_name_in_source) {
          $processes_needs_extra_processor[$process_key][] = $process_plugin_key;
          $migration_processes[$process_key] = $associative_process;
        }
      }
    }

    // Add the text field value processes to the corresponding destination
    // properties.
    foreach ($processes_needs_extra_processor as $process_key => $process_plugin_keys) {
      foreach ($process_plugin_keys as $process_plugin_key) {

        // The process should be added right after the collected key (since the
        // field value array might be converted to a string value what the
        // process plugin does not handle). Since the process pipeline not
        // always have auto-incremented integer keys, Media Migration has to
        // work with the "real" key positions.
        $real_process_key_position = array_search($process_plugin_key, array_keys($migration_processes[$process_key])) + 1;
        $leading_processes = array_slice($migration_processes[$process_key], 0, $real_process_key_position);
        $trailing_processes = array_slice($migration_processes[$process_key], $real_process_key_position);
        $migrations[$migration_id]['process'][$process_key] = array_merge($leading_processes, $extra_processes, $trailing_processes);
      }
    }
    return $migrations;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaWysiwygPluginBase::appendProcessor protected function Appends the media wysiwyg migrate processor to a field.
MediaWysiwygPluginBase::label public function Returns the translated plugin label. Overrides MediaWysiwygInterface::label
MediaWysiwygPluginBase::process public function Processes the migrations affected by the given field instance row. Overrides MediaWysiwygInterface::process
MediaWysiwygPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.