View source
<?php
namespace Drupal\media_migration\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate_plus\Event\MigrateEvents;
use Drupal\migrate_plus\Event\MigratePrepareRowEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MediaMigrationSubscriber implements EventSubscriberInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function onPrepareRow(MigratePrepareRowEvent $event) {
$this
->fileEntityFieldToMedia($event);
$this
->imageFieldToMedia($event);
}
private function fileEntityFieldToMedia(MigratePrepareRowEvent $event) {
$row = $event
->getRow();
$source = $event
->getSource();
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');
}
}
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);
}
}
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);
}
if ($source
->getPluginId() == 'd7_path_redirect' && strpos($row
->getSourceProperty('redirect'), 'file/') === 0) {
$redirect = preg_replace('/^file/', 'media', $row
->getSourceProperty('redirect'));
$row
->setSourceProperty('redirect', $redirect);
}
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);
}
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.");
}
}
}
private function imageFieldToMedia(MigratePrepareRowEvent $event) {
if (in_array($event
->getSource()
->getPluginId(), [
'd7_field',
'd7_field_instance',
'd7_field_instance_per_view_mode',
'd7_field_instance_per_form_display',
'd7_view_mode',
])) {
$row = $event
->getRow();
if ($row
->getSourceProperty('type') == 'image') {
$row
->setSourceProperty('type', 'media_image');
}
}
}
public static function getSubscribedEvents() {
return [
MigrateEvents::PREPARE_ROW => [
'onPrepareRow',
],
];
}
}