Commit 962e7bff authored by webchick's avatar webchick

Issue #2268897 by benny, Berdir, chx: Write per user contact settings D6=>D8 migration.

parent c1c7e14b
......@@ -237,6 +237,21 @@ migrate.source.empty:
type: boolean
label: 'Display field'
migrate.source.d6_user:
type: migrate_source
label: 'Drupal 6 user'
mapping:
constants:
type: mapping
label: 'Constants'
mapping:
key:
type: string
label: 'User data key'
module:
type: string
label: 'Module name'
migrate.source.d6_user_picture_file:
type: migrate_source
label: 'Drupal 6 user picure display'
......
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\destination\UserData.
*/
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\user\UserData as UserDataStorage;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
/**
* @MigrateDestination(
* id = "user_data"
* )
*/
class UserData extends DestinationBase implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\user\UserData
*/
protected $userData;
/**
* Builds an user data entity destination.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration.
* @param \Drupal\user\UserData $user_data
* The user data service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, UserDataStorage $user_data) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->userData = $user_data;
}
/**
* {@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('user.data')
);
}
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = array()) {
$uid = $row->getDestinationProperty('uid');
$module = $row->getDestinationProperty('module');
$key = $row->getDestinationProperty('key');
$this->userData->set($module, $uid, $key, $row->getDestinationProperty('settings'));
return [$uid, $module, $key];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['uid']['type'] = 'integer';
$ids['module']['type'] = 'string';
$ids['key']['type'] = 'string';
return $ids;
}
/**
* {@inheritdoc}
*/
public function fields(MigrationInterface $migration = NULL) {
return [
'uid' => 'The user id.',
'module' => 'The module name responsible for the settings.',
'key' => 'The setting key to save under.',
'settings' => 'The settings to save.',
];
}
}
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\Plugin\migrate\process\d6\SkipRowIfNotSet.
*/
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateSkipRowException;
/**
* If the source evaluates to empty, we skip the current row.
*
* @MigrateProcessPlugin(
* id = "skip_row_if_not_set",
* handle_multiples = TRUE
* )
*/
class SkipRowIfNotSet extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
if (!isset($value[$this->configuration['index']])) {
throw new MigrateSkipRowException();
}
return $value[$this->configuration['index']];
}
}
id: d6_user_contact_settings
label: Drupal 6 user contact settings
migration_groups:
- Drupal 6
source:
plugin: d6_user
constants:
key: contact
module: contact
process:
uid: uid
key: 'constants/key'
module: 'constants/module'
settings:
plugin: skip_row_if_not_set
index: contact
source: data
destination:
plugin: user_data
migration_dependencies:
required:
- d6_user
......@@ -76,6 +76,9 @@ public function prepareRow(Row $row) {
}
}
// Unserialize Data.
$row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
return parent::prepareRow($row);
}
......@@ -113,6 +116,7 @@ protected function baseFields() {
'language' => $this->t('Language'),
'picture' => $this->t('Picture'),
'init' => $this->t('Init'),
'data' => $this->t('User data'),
);
// Possible field added by Date contributed module.
......
......@@ -321,7 +321,7 @@ public static function getData($table) {
'timezone' => '3600',
'language' => 'fr',
'init' => 'doe@example.com',
'data' => serialize(array()),
'data' => serialize(array('contact' => 1)),
'timezone_name' => NULL,
'timezone_id' => 1,
'pass_plain' => 'john.doe_pass',
......@@ -346,7 +346,7 @@ public static function getData($table) {
'timezone' => '7200',
'language' => 'ro',
'init' => 'roe@example.com',
'data' => serialize(array()),
'data' => serialize(array('contact' => 0)),
'timezone_name' => 'Europe/Helsinki',
'timezone_id' => 0,
'pass_plain' => 'joe.roe_pass',
......
<?php
/**
* @file
* Contains \Drupal\migrate_drupal\Tests\d6\MigrateUserContactSettingsTest.
*/
namespace Drupal\migrate_drupal\Tests\d6;
use Drupal\Core\Database\Database;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase;
/**
* Users contact settings migration.
*
* @group migrate_drupal
*/
class MigrateUserContactSettingsTest extends MigrateDrupalTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$dumps = array(
$this->getDumpDirectory() . '/Drupal6User.php',
);
$this->loadDumps($dumps);
$id_mappings = array(
'd6_user' => array(
array(array(2), array(2)),
array(array(8), array(8)),
array(array(15), array(15)),
),
);
$this->prepareMigrations($id_mappings);
// Migrate users.
$migration = entity_load('migration', 'd6_user_contact_settings');
$executable = new MigrateExecutable($migration, $this);
$executable->import();
}
/**
* Tests the Drupal6 user contact settings migration.
*/
public function testUserContactSettings() {
$user_data = \Drupal::service('user.data');
$module = $key = 'contact';
$uid = 2;
$setting = $user_data->get($module, $uid, $key);
$this->assertIdentical($setting, '1');
$uid = 8;
$setting = $user_data->get($module, $uid, $key);
$this->assertIdentical($setting, '0');
$uid = 15;
$setting = $user_data->get($module, $uid, $key);
$this->assertIdentical($setting, NULL);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment