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
Loading
Loading
Loading
Loading
+10 −0
Changes for core/modules/field/tests/src/Kernel/Migrate/d6/MigrateFieldTest.php: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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');
+7 −28
Changes for core/modules/migrate_drupal/src/Plugin/migrate/field/NodeReference.php: 7 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -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);

/**
 * @MigrateField(
 *   id = "nodereference",
 *   core = {6},
 *   type_map = {
 *     "nodereference" = "entity_reference",
 *   },
 *   source_module = "nodereference",
 *   destination_module = "core",
 * )
 */
class NodeReference extends FieldPluginBase {
use Drupal\migrate_drupal\Plugin\migrate\field\d6\NodeReference as NonLegacyNodeReference;

/**
   * {@inheritdoc}
 * @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
 */
  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 {
}
+66 −0
Changes for core/modules/migrate_drupal/src/Plugin/migrate/field/ReferenceBase.php: 66 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?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',
    ];
  }

}
+45 −0
Changes for core/modules/migrate_drupal/src/Plugin/migrate/field/d6/NodeReference.php: 45 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?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';
  }

}
+28 −2
Changes for core/modules/migrate_drupal/src/Plugin/migrate/field/d6/UserReference.php: 28 added lines, 2 removed lines.
Original line number Diff line number Diff line
<?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}
Loading