Skip to content
Snippets Groups Projects

Resolve #3448960 "Provide migration plugins"

4 files
+ 188
43
Compare changes
  • Side-by-side
  • Inline
Files
4
<?php
declare(strict_types=1);
namespace Drupal\simple_access\Plugin\migrate\destination;
use Drupal\Core\Database\Connection;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Destination for Simple Access Node Groups.
*
* Use the following migration configuration to migrate Simple Access Groups.
*
* @code
* source:
* plugin: d7_simple_access_node_groups
* process:
* nid: nid
* gid: gid
* grant_view: sa_view
* grant_update: sa_update
* grant_delete: sa_delete
* destination:
* plugin: simple_access_node_group
* group_map:
* '1': 'group_one'
* '2': 'group_two'
* '3': 'group_three'
* @endcode
*
* @MigrateDestination(
* id = "simple_access_node_group"
* )
*/
class SimpleAccessNodeGroup extends DestinationBase implements ContainerFactoryPluginInterface {
/**
* SimpleAccessNodeGroup constructor.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MigrationInterface $migration,
protected Connection $database,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
}
/**
* {@inheritdoc}
*/
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('database'),
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
return [
'nid' => ['type' => 'integer'],
'gid' => ['type' => 'string'],
];
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'nid' => $this->t('The node id.'),
'gid' => $this->t('The group id.'),
'grant_view' => $this->t('Whether view is granted.'),
'grant_update' => $this->t('Whether update is granted.'),
'grant_delete' => $this->t('Whether delete is granted.'),
];
}
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []) {
$gid = (string) $row->getDestinationProperty('gid');
$groupMap = $this->configuration['group_map'] ?? [];
if (!isset($groupMap[$gid])) {
throw new MigrateException(sprintf('Group id "%s" is missing from the group_map config', $gid));
}
$group = $groupMap[$gid];
$this->database->insert('simple_access_node_group')
->fields([
'nid' => $row->getDestinationProperty('nid'),
'gid' => $group,
'grant_view' => $row->getDestinationProperty('grant_view'),
'grant_update' => $row->getDestinationProperty('grant_update'),
'grant_delete' => $row->getDestinationProperty('grant_delete'),
])
->execute();
return [
'nid' => $row->getDestinationProperty('nid'),
'gid' => $group,
];
}
/**
* {@inheritdoc}
*/
public function rollback(array $destination_identifier) {
$this->database->delete('simple_access_node_group')
->condition('nid', $destination_identifier['nid'])
->condition('gid', $destination_identifier['gid'])
->execute();
}
}
Loading