You are here

media_migration.module in Media Migration 8

Primary module hooks for Media migration module.

File

media_migration.module
View source
<?php

/**
 * @file
 * Primary module hooks for Media migration module.
 */
use Drupal\media_migration\MediaMigration;
use Drupal\media_migration\Utility\SourceDatabase;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * Implements hook_migration_plugins_alter().
 */
function media_migration_migration_plugins_alter(array &$migrations) {
  \Drupal::service('media_migration.migrate_plugin_alterer')
    ->alter($migrations);
}

/**
 * Implements hook_migrate_MIGRATION_ID_prepare_row() for d7_filter_format.
 */
function media_migration_migrate_d7_filter_format_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  assert($migration
    ->getSourcePlugin() instanceof DrupalSqlBase);
  $source_connection = $migration
    ->getSourcePlugin()
    ->getDatabase();
  $filter_format_id = $row
    ->getSourceProperty('format');
  $filters = $row
    ->getSourceProperty('filters');
  $media_filter_is_mapped = MediaMigration::embedTokenDestinationFilterPluginIsValid();
  $formats_formatting_img_tag = SourceDatabase::getFormatsUsingTag($source_connection, 'img');

  // Add the media_embed (and accompanying filter_align, filter_caption) filter
  // to text formats using <img> tags, to convert those to <drupal-media>.
  // @see \Drupal\media_migration\Plugin\migrate\process\ImgTagToEmbedFilter
  if (in_array($filter_format_id, $formats_formatting_img_tag) || in_array('media_filter', $filters) && $media_filter_is_mapped) {

    // Ensure they're added with the appropriate weight.
    if (isset($filters['filter_htmlcorrector'])) {
      $appropriate_weight = (int) $filters['filter_htmlcorrector']['weight'] + 1;
    }
    elseif (isset($filters['filter_html'])) {
      $appropriate_weight = (int) $filters['filter_html']['weight'] + 1;
    }
    elseif (isset($filters['filter_html_image_secure'])) {
      $appropriate_weight = (int) $filters['filter_html_image_secure']['weight'] + 1;
    }
    else {
      $appropriate_weight = -1000;
      foreach ($filters as $filter) {
        $appropriate_weight = max($appropriate_weight, (int) $filter['weight']);
      }
    }
    $filters['media_migration_added__filter_align'] = [
      // Only name, weight and settings are truly necessary for
      // core/modules/filter/migrations/d7_filter_format.yml.
      'name' => 'filter_align',
      'weight' => $appropriate_weight + 0,
      'settings' => [],
    ];
    $filters['media_migration_added__filter_caption'] = [
      'name' => 'filter_caption',
      'weight' => $appropriate_weight + 1,
      'settings' => [],
    ];
    if (in_array('media_filter', $filters)) {
      $filters['media_filter']['weight'] = $appropriate_weight + 2;
    }
    else {
      $filters['media_migration_added__media_filter'] = [
        'name' => MediaMigration::getEmbedTokenDestinationFilterPlugin(),
        'weight' => $appropriate_weight + 2,
        'settings' => [],
      ];
    }
    $row
      ->setSourceProperty('filters', $filters);
  }

  // Add the linkit filter to text formats using <a href="/file/%">, to convert
  // them to <a data-entity-substitution="media" data-entity-type="media"
  // data-entity-uuid="<media UUID>" href="/file/%">.
  // @see \Drupal\media_migration\Plugin\migrate\process\CKEditorLinkFileToLinkitFilter
  $formats_formatting_linkit = SourceDatabase::getFormatsHavingFileLink($source_connection);
  if (in_array($filter_format_id, $formats_formatting_linkit)) {
    $filters['media_migration_added__linkit'] = [
      'name' => 'linkit',
      'weight' => 0,
      'settings' => [],
    ];
    $row
      ->setSourceProperty('filters', $filters);
  }
}