View source
<?php
namespace Drupal\media_migration;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\field\Plugin\migrate\source\d7\Field;
use Drupal\field\Plugin\migrate\source\d7\FieldInstance;
use Drupal\field\Plugin\migrate\source\d7\ViewMode;
use Drupal\media_migration\Plugin\MediaWysiwyg\Paragraphs;
use Drupal\media_migration\Utility\MigrationPluginTool;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\RequirementsInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\FieldMigration;
use Psr\Log\LoggerInterface;
class MigratePluginAlterer {
protected $pluginManagerMediaWysiwyg;
protected $logger;
protected $moduleHandler;
public function __construct(MediaWysiwygPluginManager $plugin_manager_media_wysiwyg, LoggerInterface $logger, ModuleHandlerInterface $module_handler) {
$this->pluginManagerMediaWysiwyg = $plugin_manager_media_wysiwyg;
$this->logger = $logger;
$this->moduleHandler = $module_handler;
}
public function alter(array &$migrations) {
$this
->alterFieldMigrations($migrations);
$this
->addMediaWysiwygProcessor($migrations);
$this
->alterFilterFormatMigration($migrations);
}
protected function alterFieldMigrations(array &$migrations) {
foreach ($migrations as &$migration) {
if (!in_array('Drupal 7', $migration['migration_tags'] ?? [])) {
continue;
}
$source = NULL;
if (!empty($migration['source']['plugin'])) {
$source = MigrationPluginTool::getSourcePlugin($migration['source']);
if (is_a($migration['class'], FieldMigration::class, TRUE)) {
if (is_a($source, Field::class) || is_a($source, FieldInstance::class)) {
static::mapMigrationProcessValueToMedia($migration, 'entity_type');
}
}
if (is_a($source, ViewMode::class)) {
static::mapMigrationProcessValueToMedia($migration, 'targetEntityType');
}
if ($migration['source']['plugin'] === 'd7_field_instance') {
$migration['migration_dependencies']['optional'][] = 'd7_file_entity_type';
}
}
}
}
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;
}
}
}
}
protected function alterFilterFormatMigration(array &$migrations) : void {
$destination_filter_plugin_id = MediaMigration::getEmbedTokenDestinationFilterPlugin();
if (!$this->moduleHandler
->moduleExists('entity_embed')) {
unset($migrations['d7_embed_button_media']);
}
if (isset($migrations['d7_filter_format']) && MediaMigration::embedTokenDestinationFilterPluginIsValid($destination_filter_plugin_id)) {
$migrations['d7_filter_format']['process']['filters']['process']['id']['map']['media_filter'] = $destination_filter_plugin_id;
}
else {
return;
}
if (MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_ENTITY_EMBED == $destination_filter_plugin_id && isset($migrations['d7_filter_format']) && isset($migrations['d7_embed_button_media'])) {
$migrations['d7_filter_format']['migration_dependencies']['required'][] = 'd7_embed_button_media';
}
if (isset($migrations['d7_filter_format'])) {
$filter_plugin_settings_processes = MigrationPluginTool::getAssociativeMigrationProcess($migrations['d7_filter_format']['process']['filters']['process']['settings']);
$filter_plugin_settings_processes[] = [
'plugin' => 'filter_settings_embed_media',
];
$migrations['d7_filter_format']['process']['filters']['process']['settings'] = $filter_plugin_settings_processes;
}
}
public static function mapMigrationProcessValueToMedia(array &$migration, string $property) {
if (!empty($migration['source'][__FUNCTION__])) {
return;
}
try {
$value = static::getSourceValueOfMigrationProcess($migration, $property);
switch ($value) {
case 'file':
$migration['source']["media_migration_{$property}"] = 'media';
$migration['process'][$property] = "media_migration_{$property}";
break;
case NULL:
$entity_type_process = MigrationPluginTool::getAssociativeMigrationProcess($migration['process'][$property]);
$entity_type_process[] = [
'plugin' => 'static_map',
'map' => [
'file' => 'media',
],
'bypass' => TRUE,
];
$migration['process'][$property] = $entity_type_process;
break;
}
} catch (\LogicException $e) {
}
$migration['source'][__FUNCTION__] = TRUE;
}
public static function getSourceValueOfMigrationProcess(array $migration, string $process_property_key) {
if (!array_key_exists('process', $migration) || !is_array($migration['process']) || !array_key_exists($process_property_key, $migration['process'])) {
throw new \LogicException('No corresponding process found');
}
$property_processes = MigrationPluginTool::getAssociativeMigrationProcess($migration['process'][$process_property_key]);
$the_first_process = reset($property_processes);
$property_value = NULL;
if (!array_key_exists('source', $migration) || count($property_processes) !== 1 || $the_first_process['plugin'] !== 'get' || empty($the_first_process['source'])) {
return NULL;
}
$process_value_source = $the_first_process['source'];
$property_value = NestedArray::getValue($migration['source'], explode(Row::PROPERTY_SEPARATOR, $process_value_source), $key_exists);
if (!$key_exists && $migration['source']['plugin'] === 'embedded_data') {
$embedded_rows = $migration['source']['data_rows'] ?? [];
$embedded_property_values = array_reduce($embedded_rows, function (array $carry, array $row) use ($process_value_source) {
$embedded_value = NestedArray::getValue($row, explode(Row::PROPERTY_SEPARATOR, $process_value_source));
$carry = array_unique(array_merge($carry, [
$embedded_value,
]));
return $carry;
}, []);
return count($embedded_property_values) === 1 ? $embedded_property_values[0] : NULL;
}
return $key_exists ? $property_value : NULL;
}
}