Commit 2e80fc24 authored by catch's avatar catch
Browse files

Issue #2447727 by webflo, quietone, jofitz, Neslee Canil Pinto, mradcliffe,...

Issue #2447727 by webflo, quietone, jofitz, Neslee Canil Pinto, mradcliffe, claudiu.cristea, k4v, yogeshmpawar, kgoel, dimaro, hussainweb, vprocessor, malcomio, iMiksu, phenaproxima, keithm, tlyngej, benjy, heddn, infopicard, Kristen Pol, KarenS: Add base class for migrating reference fields
parent ce7d1d3c
......@@ -114,6 +114,16 @@ public function testFields() {
$this->assertSame('entity_reference', $field_storage->getType());
$this->assertSame('user', $field_storage->getSetting('target_type'));
// Node reference to entity reference migration.
$field_storage = FieldStorageConfig::load('node.field_node_reference');
$this->assertSame('entity_reference', $field_storage->getType());
$this->assertSame('node', $field_storage->getSetting('target_type'));
// User reference to entity reference migration.
$field_storage = FieldStorageConfig::load('node.field_user_reference');
$this->assertSame('entity_reference', $field_storage->getType());
$this->assertSame('user', $field_storage->getSetting('target_type'));
// Validate that the source count and processed count match up.
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = $this->getMigration('d6_field');
......
......@@ -2,36 +2,15 @@
namespace Drupal\migrate_drupal\Plugin\migrate\field;
use Drupal\migrate\Plugin\MigrationInterface;
@trigger_error('The ' . __NAMESPACE__ . '\NodeReference is deprecated in drupal:9.1.0 and will be removed from drupal:10.0.0. Instead use \Drupal\migrate_drupal\Plugin\migrate\field\d6\NodeReference. See https://www.drupal.org/node/3159537.', E_USER_DEPRECATED);
use Drupal\migrate_drupal\Plugin\migrate\field\d6\NodeReference as NonLegacyNodeReference;
/**
* @MigrateField(
* id = "nodereference",
* core = {6},
* type_map = {
* "nodereference" = "entity_reference",
* },
* source_module = "nodereference",
* destination_module = "core",
* )
* @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use
* \Drupal\migrate_drupal\Plugin\migrate\field\d6\NodeReference instead.
*
* @see https://www.drupal.org/node/3159537
*/
class NodeReference extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'sub_process',
'source' => $field_name,
'process' => [
'target_id' => [
'plugin' => 'get',
'source' => 'nid',
],
],
];
$migration->setProcessOfProperty($field_name, $process);
}
class NodeReference extends NonLegacyNodeReference {
}
<?php
namespace Drupal\migrate_drupal\Plugin\migrate\field;
use Drupal\migrate\Plugin\MigrationInterface;
/**
* Base class for Drupal reference fields.
*/
abstract class ReferenceBase extends FieldPluginBase {
/**
* Gets the plugin ID for the reference type migration.
*
* The reference type migration will be added as a required dependency.
*
* @return string
* The plugin id.
*/
abstract protected function getEntityTypeMigrationId();
/**
* Gets the name of the field property which holds the entity ID.
*
* @return string
* The entity id.
*/
abstract protected function entityId();
/**
* {@inheritdoc}
*/
public function alterFieldInstanceMigration(MigrationInterface $migration) {
parent::alterFieldInstanceMigration($migration);
// Add the reference migration as a required dependency to this migration.
$migration_dependencies = $migration->getMigrationDependencies();
array_push($migration_dependencies['required'], $this->getEntityTypeMigrationId());
$migration_dependencies['required'] = array_unique($migration_dependencies['required']);
$migration->set('migration_dependencies', $migration_dependencies);
}
/**
* {@inheritdoc}
*/
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'sub_process',
'source' => $field_name,
'process' => ['target_id' => $this->entityId()],
];
$migration->setProcessOfProperty($field_name, $process);
}
/**
* {@inheritdoc}
*/
public function getFieldWidgetMap() {
return [
$this->pluginId . '_select' => 'options_select',
$this->pluginId . '_buttons' => 'options_buttons',
$this->pluginId . '_autocomplete' => 'entity_reference_autocomplete_tags',
];
}
}
<?php
namespace Drupal\migrate_drupal\Plugin\migrate\field\d6;
use Drupal\migrate_drupal\Plugin\migrate\field\ReferenceBase;
/**
* MigrateField Plugin for Drupal 6 node reference fields.
*
* @MigrateField(
* id = "nodereference",
* core = {6},
* type_map = {
* "nodereference" = "entity_reference",
* },
* source_module = "nodereference",
* destination_module = "core",
* )
*
* @internal
*/
class NodeReference extends ReferenceBase {
/**
* The plugin ID for the reference type migration.
*
* @var string
*/
protected $nodeTypeMigration = 'd6_node_type';
/**
* {@inheritdoc}
*/
protected function getEntityTypeMigrationId() {
return $this->nodeTypeMigration;
}
/**
* {@inheritdoc}
*/
protected function entityId() {
return 'nid';
}
}
<?php
namespace Drupal\migrate_drupal\Plugin\migrate\field;
namespace Drupal\migrate_drupal\Plugin\migrate\field\d6;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\field\ReferenceBase;
// cspell:ignore userreference
/**
* MigrateField Plugin for Drupal 6 user reference fields.
*
* @MigrateField(
* id = "userreference",
* core = {6},
......@@ -16,8 +19,31 @@
* source_module = "userreference",
* destination_module = "core",
* )
*
* @internal
*/
class UserReference extends FieldPluginBase {
class UserReference extends ReferenceBase {
/**
* The plugin ID for the reference type migration.
*
* @var string
*/
protected $userTypeMigration = 'd6_user_role';
/**
* {@inheritdoc}
*/
protected function getEntityTypeMigrationId() {
return $this->userTypeMigration;
}
/**
* {@inheritdoc}
*/
protected function entityId() {
return 'uid';
}
/**
* {@inheritdoc}
......
......@@ -2351,6 +2351,89 @@
'delta' => '1',
))
->execute();
$connection->schema()->createTable('content_field_node_reference', array(
'fields' => array(
'vid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'nid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'delta' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'field_node_reference_nid' => array(
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
),
),
'primary key' => array(
'vid',
'delta',
),
'indexes' => array(
'nid' => array(
'nid',
),
'field_node_reference_nid' => array(
'field_node_reference_nid',
),
),
'mysql_character_set' => 'utf8',
));
$connection->insert('content_field_node_reference')
->fields(array(
'vid',
'nid',
'delta',
'field_node_reference_nid',
))
->values(array(
'vid' => '1',
'nid' => '1',
'delta' => '0',
'field_node_reference_nid' => NULL,
))
->values(array(
'vid' => '2',
'nid' => '1',
'delta' => '0',
'field_node_reference_nid' => NULL,
))
->values(array(
'vid' => '3',
'nid' => '2',
'delta' => '0',
'field_node_reference_nid' => '1',
))
->values(array(
'vid' => '5',
'nid' => '2',
'delta' => '0',
'field_node_reference_nid' => NULL,
))
->values(array(
'vid' => '12',
'nid' => '9',
'delta' => '0',
'field_node_reference_nid' => '2',
))
->execute();
$connection->schema()->createTable('content_field_test', array(
'fields' => array(
'vid' => array(
......@@ -2595,6 +2678,89 @@
'field_test_two_value' => '20',
))
->execute();
$connection->schema()->createTable('content_field_user_reference', array(
'fields' => array(
'vid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'nid' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'delta' => array(
'type' => 'int',
'not null' => TRUE,
'size' => 'normal',
'default' => '0',
'unsigned' => TRUE,
),
'field_user_reference_uid' => array(
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
),
),
'primary key' => array(
'vid',
'delta',
),
'indexes' => array(
'nid' => array(
'nid',
),
'field_user_reference_uid' => array(
'field_user_reference_uid',
),
),
'mysql_character_set' => 'utf8',
));
$connection->insert('content_field_user_reference')
->fields(array(
'vid',
'nid',
'delta',
'field_user_reference_uid',
))
->values(array(
'vid' => '1',
'nid' => '1',
'delta' => '0',
'field_user_reference_uid' => NULL,
))
->values(array(
'vid' => '2',
'nid' => '1',
'delta' => '0',
'field_user_reference_uid' => NULL,
))
->values(array(
'vid' => '3',
'nid' => '2',
'delta' => '0',
'field_user_reference_uid' => NULL,
))
->values(array(
'vid' => '5',
'nid' => '2',
'delta' => '0',
'field_user_reference_uid' => NULL,
))
->values(array(
'vid' => '12',
'nid' => '9',
'delta' => '0',
'field_user_reference_uid' => NULL,
))
->execute();
$connection->schema()->createTable('content_group', array(
'fields' => array(
'group_type' => array(
......@@ -2809,6 +2975,30 @@
'active' => '1',
'locked' => '0',
))
->values(array(
'field_name' => 'field_node_reference',
'type' => 'nodereference',
'global_settings' => 'a:1:{s:19:"referenceable_types";a:9:{s:7:"article";i:0;s:7:"company";i:0;s:8:"employee";i:0;s:10:"test_event";i:0;s:9:"test_page";i:0;s:11:"test_planet";i:0;s:10:"test_story";i:0;s:7:"sponsor";i:0;s:5:"story";i:0;}}',
'required' => '0',
'multiple' => '1',
'db_storage' => '0',
'module' => 'nodereference',
'db_columns' => 'a:1:{s:3:"nid";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:0;s:5:"index";b:1;}}',
'active' => '1',
'locked' => '0',
))
->values(array(
'field_name' => 'field_node_reference_2',
'type' => 'nodereference',
'global_settings' => 'a:1:{s:19:"referenceable_types";a:9:{s:7:"article";s:7:"article";s:7:"company";i:0;s:8:"employee";i:0;s:10:"test_event";i:0;s:9:"test_page";i:0;s:11:"test_planet";i:0;s:10:"test_story";i:0;s:7:"sponsor";i:0;s:5:"story";i:0;}}',
'required' => '0',
'multiple' => '0',
'db_storage' => '1',
'module' => 'nodereference',
'db_columns' => 'a:1:{s:3:"nid";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:0;s:5:"index";b:1;}}',
'active' => '1',
'locked' => '0',
))
->values(array(
'field_name' => 'field_reference',
'type' => 'nodereference',
......@@ -3097,6 +3287,30 @@
'active' => '1',
'locked' => '0',
))
->values(array(
'field_name' => 'field_user_reference',
'type' => 'userreference',
'global_settings' => 'a:2:{s:19:"referenceable_roles";a:4:{i:2;i:0;i:3;i:0;i:4;i:0;i:5;i:0;}s:20:"referenceable_status";s:0:"";}',
'required' => '0',
'multiple' => '1',
'db_storage' => '0',
'module' => 'userreference',
'db_columns' => 'a:1:{s:3:"uid";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:0;s:5:"index";b:1;}}',
'active' => '1',
'locked' => '0',
))
->values(array(
'field_name' => 'field_user_reference_2',
'type' => 'userreference',
'global_settings' => 'a:2:{s:19:"referenceable_roles";a:4:{i:3;i:3;i:2;i:0;i:4;i:0;i:5;i:0;}s:20:"referenceable_status";s:1:"1";}',
'required' => '0',
'multiple' => '0',
'db_storage' => '1',
'module' => 'userreference',
'db_columns' => 'a:1:{s:3:"uid";a:4:{s:4:"type";s:3:"int";s:8:"unsigned";b:1;s:8:"not null";b:0;s:5:"index";b:1;}}',
'active' => '1',
'locked' => '0',
))
->execute();
$connection->schema()->createTable('content_node_field_instance', array(
'fields' => array(
......@@ -3238,6 +3452,30 @@
'widget_module' => 'number',
'widget_active' => '1',
))
->values(array(
'field_name' => 'field_node_reference',
'type_name' => 'story',
'weight' => '21',
'label' => 'Node reference',
'widget_type' => 'nodereference_autocomplete',
'widget_settings' => 'a:4:{s:18:"autocomplete_match";s:8:"contains";s:4:"size";s:2:"60";s:13:"default_value";a:1:{i:0;a:2:{s:3:"nid";N;s:14:"_error_element";s:55:"default_value_widget][field_node_reference][0][nid][nid";}}s:17:"default_value_php";N;}',
'display_settings' => 'a:5:{s:5:"label";a:2:{s:6:"format";s:5:"above";s:7:"exclude";i:0;}i:5;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:6:"teaser";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:4:"full";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}i:4;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}}',
'description' => '',
'widget_module' => 'nodereference',
'widget_active' => '1',
))
->values(array(
'field_name' => 'field_node_reference_2',
'type_name' => 'story',
'weight' => '23',
'label' => 'Node reference 2',
'widget_type' => 'nodereference_select',
'widget_settings' => 'a:4:{s:18:"autocomplete_match";s:8:"contains";s:4:"size";i:60;s:13:"default_value";a:1:{i:0;a:1:{s:3:"nid";s:0:"";}}s:17:"default_value_php";N;}',
'display_settings' => 'a:5:{s:5:"label";a:2:{s:6:"format";s:5:"above";s:7:"exclude";i:0;}i:5;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:6:"teaser";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:4:"full";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}i:4;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}}',
'description' => '',
'widget_module' => 'nodereference',
'widget_active' => '1',
))
->values(array(
'field_name' => 'field_reference',
'type_name' => 'page',
......@@ -3550,6 +3788,30 @@
'widget_module' => 'number',
'widget_active' => '1',
))
->values(array(
'field_name' => 'field_user_reference',
'type_name' => 'story',
'weight' => '20',
'label' => 'User reference',
'widget_type' => 'userreference_autocomplete',
'widget_settings' => 'a:5:{s:18:"autocomplete_match";s:8:"contains";s:4:"size";s:2:"60";s:12:"reverse_link";i:0;s:13:"default_value";a:1:{i:0;a:2:{s:3:"uid";N;s:14:"_error_element";s:55:"default_value_widget][field_user_reference][0][uid][uid";}}s:17:"default_value_php";N;}',
'display_settings' => 'a:5:{s:5:"label";a:2:{s:6:"format";s:5:"above";s:7:"exclude";i:0;}i:5;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:6:"teaser";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:4:"full";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}i:4;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}}',
'description' => '',
'widget_module' => 'userreference',
'widget_active' => '1',
))
->values(array(
'field_name' => 'field_user_reference_2',
'type_name' => 'story',
'weight' => '21',
'label' => 'User reference 2',
'widget_type' => 'userreference_select',
'widget_settings' => 'a:5:{s:18:"autocomplete_match";s:8:"contains";s:4:"size";i:60;s:12:"reverse_link";i:0;s:13:"default_value";a:1:{i:0;a:1:{s:3:"uid";s:0:"";}}s:17:"default_value_php";N;}',
'display_settings' => 'a:5:{s:5:"label";a:2:{s:6:"format";s:5:"above";s:7:"exclude";i:0;}i:5;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:6:"teaser";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}s:4:"full";a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}i:4;a:2:{s:6:"format";s:7:"default";s:7:"exclude";i:0;}}',
'description' => '',
'widget_module' => 'userreference',
'widget_active' => '1',
))
->execute();
$connection->schema()->createTable('content_type_employee', array(
'fields' => array(
......@@ -3893,6 +4155,18 @@
'not null' => FALSE,
'size' => 'big',
),
'field_user_reference_2_uid' => array(
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
),
'field_node_reference_2_nid' => array(
'type' => 'int',
'not null' => FALSE,
'size' => 'normal',
'unsigned' => TRUE,
),
),
'primary key' => array(
'vid',
......@@ -3901,6 +4175,12 @@
'nid' => array(
'nid',
),
'field_user_reference_2_uid' => array(
'field_user_reference_2_uid',
),
'field_node_reference_2_nid' => array(
'field_node_reference_2_nid',
),
),
'mysql_character_set' => 'utf8',
));
......@@ -3937,6 +4217,8 @@
'field_test_datestamp_value2',
'field_test_datetime_value2',
'field_test_string_selectlist_value',
'field_user_reference_2_uid',
'field_node_reference_2_nid',
))
->values(array(
'nid' => '1',
......@@ -3969,6 +4251,8 @@
'field_test_datestamp_value2' => NULL,
'field_test_datetime_value2' => NULL,
'field_test_string_selectlist_value' => NULL,
'field_user_reference_2_uid' => NULL,
'field_node_reference_2_nid' => NULL,
))
->values(array(
'nid' => '2',
......@@ -4001,6 +4285,8 @@
'field_test_datestamp_value2' => NULL,
'field_test_datetime_value2' => NULL,
'field_test_string_selectlist_value' => NULL,
'field_user_reference_2_uid' => NULL,
'field_node_reference_2_nid' => NULL,
))
->values(array(
'nid' => '1',
......@@ -4033,6 +4319,8 @@
'field_test_datestamp_value2' => NULL,
'field_test_datetime_value2' => NULL,
'field_test_string_selectlist_value' => NULL,
'field_user_reference_2_uid' => NULL,
'field_node_reference_2_nid' => NULL,
))
->values(array(
'nid' => '9',
......@@ -4065,6 +4353,8 @@
'field_test_datestamp_value2' => '1391357160',
'field_test_datetime_value2' => '2015-03-04 06:07:00',
'field_test_string_selectlist_value' => NULL,
'field_user_reference_2_uid' => NULL,
'field_node_reference_2_nid' => NULL,
))
->values(array(
'nid' => '1',
......@@ -4097,6 +4387,8 @@
'field_test_datestamp_value2' => NULL,
'field_test_datetime_value2' => NULL,
'field_test_string_selectlist_value' => NULL,
'field_user_reference_2_uid' => NULL,
'field_node_reference_2_nid' => NULL,
))
->execute();
$connection->schema()->createTable('content_type_test_page', array(
......@@ -189,8 +189,12 @@ public function addAllFieldProcessesAltersData() {
'options/type' => [
'type' => [
'map' => [
'userreference' => 'userreference_default',
'nodereference' => 'nodereference_default',
'userreference_select' => 'options_select',
'userreference_buttons' => 'options_buttons',
'userreference_autocomplete' => 'entity_reference_autocomplete_tags',
'nodereference_select' => 'options_select',
'nodereference_buttons' => 'options_buttons',