You are here

protected function MediaWysiwygPluginBase::appendProcessor in Media Migration 8

Appends the media wysiwyg migrate processor to a field.

Parameters

array $migrations: The array of migrations.

string $migration_id: The migration to adjust.

string $field_name_in_source: The migration field name.

string $source_entity_type: The source entity type.

Return value

array The updated array of migrations.

1 call to MediaWysiwygPluginBase::appendProcessor()
MediaWysiwygPluginBase::process in src/MediaWysiwygPluginBase.php
Processes the migrations affected by the given field instance row.

File

src/MediaWysiwygPluginBase.php, line 85

Class

MediaWysiwygPluginBase
Base class for media_wysiwyg plugins.

Namespace

Drupal\media_migration

Code

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;
}