You are here

private function MediaMigrationSubscriber::fileEntityFieldToMedia in Media Migration 8

Migrates file entity fields to media ones.

Parameters

\Drupal\migrate_plus\Event\MigratePrepareRowEvent $event: The prepare row event.

Throws

\Exception If the row is empty.

1 call to MediaMigrationSubscriber::fileEntityFieldToMedia()
MediaMigrationSubscriber::onPrepareRow in src/EventSubscriber/MediaMigrationSubscriber.php
Migrate prepare row event handler.

File

src/EventSubscriber/MediaMigrationSubscriber.php, line 56

Class

MediaMigrationSubscriber
Media migration event subscriber.

Namespace

Drupal\media_migration\EventSubscriber

Code

private function fileEntityFieldToMedia(MigratePrepareRowEvent $event) {
  $row = $event
    ->getRow();
  $source = $event
    ->getSource();

  // Change the type from file to file_entity so it can be processed by
  // a migrate field plugin.
  // @see \Drupal\file_entity_migration\Plugin\migrate\field\FileEntity
  if (in_array($source
    ->getPluginId(), [
    'd7_field',
    'd7_field_instance',
    'd7_field_instance_per_view_mode',
    'd7_field_instance_per_form_display',
    'd7_view_mode',
  ])) {
    if ($row
      ->getSourceProperty('type') == 'file') {
      $row
        ->setSourceProperty('type', 'file_entity');
    }
  }

  // Transform entity reference fields pointing to file entities so
  // they point to media ones.
  if ($source
    ->getPluginId() == 'd7_field' && $row
    ->getSourceProperty('type') == 'entityreference') {
    $settings = $row
      ->getSourceProperty('settings');
    if ($settings['target_type'] == 'file') {
      $settings['target_type'] = 'media';
      $row
        ->setSourceProperty('settings', $settings);
    }
  }

  // Map path alias sources from file/1234 to media/1234.
  if ($source
    ->getPluginId() == 'd7_url_alias' && strpos($row
    ->getSourceProperty('source'), 'file/') === 0) {
    $source_url = preg_replace('/^file/', 'media', $row
      ->getSourceProperty('source'));
    $row
      ->setSourceProperty('source', $source_url);
  }

  // Map redirections from file/1234 to media/1234.
  if ($source
    ->getPluginId() == 'd7_path_redirect' && strpos($row
    ->getSourceProperty('redirect'), 'file/') === 0) {
    $redirect = preg_replace('/^file/', 'media', $row
      ->getSourceProperty('redirect'));
    $row
      ->setSourceProperty('redirect', $redirect);
  }

  // Map file menu links to media ones.
  if ($source
    ->getPluginId() == 'menu_link' && strpos($row
    ->getSourceProperty('link_path'), 'file/') === 0) {
    $link_path = preg_replace('/^file/', 'media', $row
      ->getSourceProperty('link_path'));
    $row
      ->setSourceProperty('link_path', $link_path);
  }

  // Prevent the migration of the alt and title field configurations for image
  // media type bundles. These properties will be migrated into the image
  // media's source field configuration (which is an image field).
  if (in_array($source
    ->getPluginId(), [
    'd7_field',
    'd7_field_instance',
    'd7_field_instance_per_view_mode',
    'd7_field_instance_per_form_display',
  ])) {
    $is_media_config = $row
      ->getSourceProperty('entity_type') === 'file';
    $is_image_bundle = $row
      ->getSourceProperty('bundle') === 'image';
    $special_fields_to_ignore = [
      'field_file_image_alt_text',
      'field_file_image_title_text',
    ];
    $skip_this_field = $is_media_config && ($is_image_bundle || $source
      ->getPluginId() === 'd7_field') && in_array($row
      ->getSourceProperty('field_name'), $special_fields_to_ignore, TRUE);
    if ($skip_this_field) {
      throw new MigrateSkipRowException('Skipping field ' . $row
        ->getSourceProperty('field_name') . " as it will be migrated to the image media entity's source image field.");
    }
  }
}