Commit 7ac800ec authored by Yash Rode's avatar Yash Rode Committed by Zoltan Attila Horvath
Browse files

Issue #3273427 by yash.rode, huzooka: Migrate Video Embed Field from D7 to Media in D9

parent b27795b1
Loading
Loading
Loading
Loading
+10 −0
Changes for media_migration.module: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -118,3 +118,13 @@ function media_migration_migrate_d7_filter_format_prepare_row(Row $row, MigrateS
    $row->setSourceProperty('filters', $filters);
  }
}

/**
 * Implements hook_migrate_field_info_alter().
 *
 * If destination site has video embed field module enabled then we don't want
 * to migrate data from source to video embed field so unsetting that.
 */
function media_migration_migrate_field_info_alter(array &$definitions) {
  unset($definitions['video_embed_field']);
}
+21 −0
Changes for migrations/d7_video_embed_field.yml: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
id: d7_video_embed_field
label: Video Embed Field migration
migration_tags:
  - Drupal 7
  - Content
  - Media Migration
  - Media Entity
idMap:
  plugin: smart_sql
source:
  plugin: video_embed
  constants:
    bundle: remote_video
process:
  bundle: 'constants/bundle'
  field_media_oembed_video/value:
    plugin: media_internet_field_value
    source: video_url
  name: '@field_media_oembed_video/value'
destination:
  plugin: entity:media
+78 −0
Changes for src/Plugin/migrate/field/VideoEmbedField.php: 78 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\media_migration\Plugin\migrate\field;

use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;

/**
 * Field migration plugin for Drupal 7 Video embed field.
 *
 * @MigrateField(
 *   id = "videoembedfield",
 *   core = {7},
 *   type_map = {
 *    "video_embed_field" = "entity_reference"
 *   },
 *   source_module = "video_embed_field",
 *   destination_module = "media"
 * )
 */
class VideoEmbedField extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getFieldFormatterMap() {
    return [
      'video_embed_field' => 'entity_reference_entity_view',
      'video_embed_field_url' => 'entity_reference_label',
      'video_embed_field_thumbnail' => 'media_thumbnail',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldWidgetMap() {
    return [
      'video_embed_field_video' => 'media_library_widget',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function alterFieldMigration(MigrationInterface $migration) {
    $settings = [
      'video_embed_field' => [
        'plugin' => 'media_image_field_settings',
        'expected_source_type' => 'video_embed_field',
      ],
    ];
    $migration->mergeProcessOfProperty('settings', $settings);

    parent::alterFieldMigration($migration);
  }

  /**
   * {@inheritdoc}
   */
  public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
    $migration->setProcessOfProperty(
      $field_name,
      [
        'plugin' => 'sub_process',
        'source' => $field_name,
        'process' => [
          'target_id' => [
            'plugin' => 'migration_lookup',
            'migration' => 'd7_video_embed_field',
            'source' => 'video_url',
          ],
        ],
      ]
    );
  }

}
+86 −0
Changes for src/Plugin/migrate/source/VideoEmbedField.php: 86 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\media_migration\Plugin\migrate\source;

use Drupal\Core\Database\Query\SelectInterface;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * This source plugin migrates Video Embed Field.
 *
 * @MigrateSource(
 *   id = "video_embed",
 *   source_module = "video_embed_field"
 * )
 */
class VideoEmbedField extends DrupalSqlBase {

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'video_url' => 'URL of video',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return [
      'video_url' => [
        'type' => 'string',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = NULL;
    foreach ($this->getVideoEmbedFieldNames() as $video_embed_field_name) {
      $union_query = $this->select("field_data_$video_embed_field_name", $video_embed_field_name);
      $union_query->addField($video_embed_field_name, "{$video_embed_field_name}_video_url", 'video_url');
      static::addUnionQuery($query, $union_query);
    }
    $main_query = $this->select($query, 'all_embed_video')->fields('all_embed_video');
    $main_query->orderBy('all_embed_video.video_url');
    return $main_query;
  }

  /**
   * Fetch field names for  Video Embed fields.
   *
   * @return string[]
   *   array of Video Embed field names.
   */
  protected function getVideoEmbedFieldNames(): array {
    $video_embed_fields = $this->select('field_config')
      ->fields('field_config', ['field_name'])
      ->condition('type', 'video_embed_field')
      ->condition('module', 'video_embed_field')
      ->execute()
      ->fetchAllKeyed(0, 0);

    return array_values($video_embed_fields);
  }

  /**
   * Performs a query union.
   *
   * @param \Drupal\Core\Database\Query\SelectInterface|null $destination
   *   The "destination" query which should be extended.
   * @param \Drupal\Core\Database\Query\SelectInterface $source
   *   The query which should be added.
   */
  protected static function addUnionQuery(&$destination, SelectInterface $source) {
    if ($destination instanceof SelectInterface) {
      $destination->union($source);
      return;
    }
    $destination = clone $source;
  }

}
+29 −0
Changes for tests/fixtures/drupal7.php: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
// phpcs:ignoreFile
/**
 * @file
 * A database agnostic dump for testing purposes.
 *
 * This file was generated by the Drupal 9.2.6 db-tools.php script.
 */

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

include 'drupal7' . DIRECTORY_SEPARATOR . 'file_managed.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'filter.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'filter_format.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_config.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_config_instance.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_data_field_vid_emb.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_revision_body.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_revision_field_vid_emb.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'node.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'node_revision.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'node_type.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'role.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'role_permission.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'system.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'users.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'users_roles.php';
Loading