View source
<?php
namespace Drupal\agreement\Plugin\migrate\process;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigratePluginManagerInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AgreementSettings extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $migration;
protected $processPluginManager;
protected $migrationPluginManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, MigratePluginManagerInterface $process_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->migrationPluginManager = $migration_plugin_manager;
$this->processPluginManager = $process_plugin_manager;
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$value['recipient'] = $value['email_recipient'];
unset($value['email_recipient']);
if (!is_array($value['role'])) {
$value['roles'] = [
$this
->getRoleId($value['role'], $migrate_executable),
];
}
else {
$value['roles'] = [];
foreach ($value['role'] as $role) {
$value['roles'][] = $this
->getRoleId($role, $migrate_executable);
}
}
unset($value['role']);
$value['visibility'] = [
'settings' => (int) $value['visibility_settings'],
'pages' => [],
];
$pages = preg_split('/\\r?\\n/', $value['visibility_pages']);
if (!empty($pages)) {
foreach ($pages as $page) {
if ($page) {
$value['visibility']['pages'][] = '/' . $page;
}
}
}
unset($value['visibility_pages']);
unset($value['visibility_settings']);
$value['reset_date'] = 0;
$value['destination'] = !empty($value['destination']) ? '/' . $value['destination'] : '';
return $value;
}
protected function getRoleId($value, MigrateExecutableInterface $executable) {
if ($value === 1) {
return 'anonymous';
}
elseif ($value === 2) {
return 'authenticated';
}
try {
$row = new Row([
'rid' => $value,
], [
'rid' => [
'type' => 'integer',
],
]);
$migration = $this->migrationPluginManager
->createInstance('d7_user_role');
$configuration = [
'source' => 'rid',
];
$source_rid = $this->processPluginManager
->createInstance('get', $configuration, $this->migration)
->transform(NULL, $executable, $row, 'rid');
if (!is_array($source_rid)) {
$source_rid = [
'rid' => $source_rid,
];
}
$source_id_values['d7_user_role'] = $source_rid;
if ($destination_ids = $migration
->getIdMap()
->lookupDestinationIds($source_id_values['d7_user_role'])) {
$collapsed_ids = reset($destination_ids);
if (!empty($collapsed_ids)) {
return reset($collapsed_ids);
}
}
return $value;
} catch (PluginException $e) {
return $value;
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('plugin.manager.migration'), $container
->get('plugin.manager.migrate.process'));
}
}