You are here

protected function MigratePluginAlterer::addMediaWysiwygProcessor in Media Migration 8

Appends text processors to transform D7 tokens to embeds.

Find field instances with text processing and pass them to a MediaWysiwyg plugin that will add processors to the respective migrations.

Parameters

array $migrations: The array of migration plugins.

See also

\Drupal\media_migration\Plugin\MediaWysiwyg\Node

1 call to MigratePluginAlterer::addMediaWysiwygProcessor()
MigratePluginAlterer::alter in src/MigratePluginAlterer.php
Alters migrate plugins.

File

src/MigratePluginAlterer.php, line 129

Class

MigratePluginAlterer
Service for performing migration plugin alterations.

Namespace

Drupal\media_migration

Code

protected function addMediaWysiwygProcessor(array &$migrations) : void {
  $field_instance_migrations = array_filter($migrations, function (array $definition) {
    return $definition['id'] === 'd7_field_instance';
  });
  if (empty($field_instance_migrations)) {
    return;
  }
  $fields_in_content_entity_migrations_processed = [];
  $source_entity_types_without_plugin = [];
  foreach ($field_instance_migrations as $field_instance_migration_definition) {
    $field_instance_source = MigrationPluginTool::getSourcePlugin($field_instance_migration_definition['source']);
    if ($field_instance_source instanceof RequirementsInterface) {
      try {
        $field_instance_source
          ->checkRequirements();
      } catch (RequirementsException $e) {
        continue;
      }
    }
    foreach ($field_instance_source as $row) {
      assert($row instanceof Row);
      $source_entity_type_id = $row
        ->getSourceProperty('entity_type');
      if (!in_array($source_entity_type_id, $source_entity_types_without_plugin, TRUE) && $row
        ->getSourceProperty('field_definition')['module'] === 'text' && $row
        ->getSourceProperty('settings/text_processing') !== 0) {
        $field_name = $row
          ->getSourceProperty('field_name');
        if (!empty($fields_in_content_entity_migrations_processed[$source_entity_type_id]) && in_array($field_name, $fields_in_content_entity_migrations_processed[$source_entity_type_id], TRUE)) {
          continue;
        }
        try {
          $plugin = $this->pluginManagerMediaWysiwyg
            ->createInstanceFromSourceEntityType($source_entity_type_id);
          $migrations = $plugin
            ->process($migrations, $row);
        } catch (PluginException $e) {
          $source_entity_types_without_plugin[] = $source_entity_type_id;
          $this->logger
            ->warning(sprintf("Could not find a MediaWysiwyg plugin for field '%s' used in source entity type '%s'. You probably need to create a new one. Have a look at '%s' for an example.", $row
            ->getSourceProperty('field_name'), $source_entity_type_id, Paragraphs::class));
        }
        $fields_in_content_entity_migrations_processed[$source_entity_type_id][] = $field_name;
      }
    }
  }
}