<?php
namespace Drupal\paragraphs;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Drupal\paragraphs\Plugin\migrate\field\FieldCollection;
final class MigrationPluginsAlterer {
use MigrationDeriverTrait;
protected $loggerChannel;
public function __construct(LoggerChannelFactoryInterface $logger_factory) {
$this->loggerChannel = $logger_factory
->get('paragraphs');
}
public function alterMigrationPlugins(array &$migrations) {
foreach ($migrations as &$migration) {
if (!isset($migration['process']) || !is_array($migration['process'])) {
continue;
}
foreach ([
'entity_type',
'targetEntityType',
] as $process_property) {
if (isset($migration['process'][$process_property])) {
$this
->paragraphsMigrationEntityTypeAdjust($migration, $process_property);
$this
->paragraphsMigrationBundleAdjust($migration);
$migration['migration_dependencies']['optional'][] = 'd7_field_collection_type';
$migration['migration_dependencies']['optional'][] = 'd7_paragraphs_type';
}
}
}
}
public function paragraphsMigrationEntityTypeAdjust(array &$migration, $process_property) {
if (!$this
->paragraphsMigrationPrepareProcess($migration['process'], $process_property)) {
return;
}
$entity_type_process =& $migration['process'][$process_property];
$entity_type_process[] = [
'plugin' => 'static_map',
'map' => [
'field_collection_item' => 'paragraph',
'paragraphs_item' => 'paragraph',
],
'bypass' => TRUE,
];
}
public function paragraphsMigrationBundleAdjust(array &$migration) {
$key = version_compare(\Drupal::VERSION, '9.1.4', '<') ? 'bundle' : 'bundle_mapped';
if (!$this
->paragraphsMigrationPrepareProcess($migration['process'], $key)) {
return;
}
$bundle_process =& $migration['process'][$key];
$bundle_process[] = [
'plugin' => 'paragraphs_process_on_value',
'source_value' => 'entity_type',
'expected_value' => 'field_collection_item',
'process' => [
'plugin' => 'paragraphs_strip_field_prefix',
],
];
}
public function paragraphsMigrationPrepareProcess(array &$process, $property) : bool {
if (!isset($process[$property])) {
return FALSE;
}
$process_element =& $process[$property];
if (is_string($process_element)) {
$process_element = [
[
'plugin' => 'get',
'source' => $process_element,
],
];
}
elseif (is_array($process_element) && array_key_exists('plugin', $process_element)) {
$process_element = [
$process_element,
];
}
if (!is_array($process_element)) {
$this->loggerChannel
->error('Unknown migration process element type: @type.', [
'@type' => gettype($process_element),
]);
return FALSE;
}
return TRUE;
}
}