From 176461346a394eb47821dd482dae2811580f8dd4 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Sun, 9 Apr 2017 18:52:45 -0400 Subject: [PATCH] Issue #2630732 by mallezie, sylus: Implement Entity::fields() for migration destinations --- .../Plugin/migrate/destination/EntityComment.php | 12 ++- .../src/Plugin/migrate/destination/Entity.php | 35 ++++++++- .../migrate/destination/EntityContentBase.php | 10 ++- .../migrate_destination_test.info.yml | 6 ++ ...migrate.migration.destination_bundle_entity.yml | 12 +++ .../migrate.migration.destination_entity.yml | 11 +++ .../source/MigrateDestinationTestSource.php | 63 +++++++++++++++ .../src/Kernel/MigrateEntityDestinationTest.php | 89 ++++++++++++++++++++++ .../src/Plugin/migrate/destination/EntityUser.php | 10 ++- 9 files changed, 236 insertions(+), 12 deletions(-) create mode 100644 core/modules/migrate/tests/modules/migrate_destination_test/migrate_destination_test.info.yml create mode 100644 core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_bundle_entity.yml create mode 100644 core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_entity.yml create mode 100644 core/modules/migrate/tests/modules/migrate_destination_test/src/Plugin/migrate/source/MigrateDestinationTestSource.php create mode 100644 core/modules/migrate/tests/src/Kernel/MigrateEntityDestinationTest.php diff --git a/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php b/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php index efce666..c3d571d 100644 --- a/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php +++ b/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php @@ -3,6 +3,7 @@ namespace Drupal\comment\Plugin\migrate\destination; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\State\StateInterface; @@ -53,9 +54,13 @@ class EntityComment extends EntityContentBase { * The field type plugin manager service. * @param \Drupal\Core\State\StateInterface $state * The state storage object. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager service. + * @param EntityFieldManagerInterface $entity_field_manager + * The entity field manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, StateInterface $state) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager); + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, StateInterface $state, EntityFieldManagerInterface $entity_field_manager = NULL) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager, $entity_field_manager); $this->state = $state; } @@ -73,7 +78,8 @@ public static function create(ContainerInterface $container, array $configuratio array_keys($container->get('entity.manager')->getBundleInfo($entity_type)), $container->get('entity.manager'), $container->get('plugin.manager.field.field_type'), - $container->get('state') + $container->get('state'), + $container->get('entity_field.manager') ); } diff --git a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php index 1b2c2e7..4a7bfb0 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php @@ -5,6 +5,7 @@ use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Core\Entity\DependencyTrait; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; @@ -37,6 +38,13 @@ protected $bundles; /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + + /** * Construct a new entity. * * @param array $configuration @@ -51,11 +59,14 @@ * The storage for this entity type. * @param array $bundles * The list of bundles this entity type has. + * @param EntityFieldManagerInterface $entity_field_manager + * The entity field manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityFieldManagerInterface $entity_field_manager = NULL) { parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); $this->storage = $storage; $this->bundles = $bundles; + $this->entityFieldManager = $entity_field_manager; $this->supportsRollback = TRUE; } @@ -70,7 +81,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $migration, $container->get('entity.manager')->getStorage($entity_type_id), - array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)) + array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id)), + $container->get('entity_field.manager') ); } @@ -107,7 +119,24 @@ public function getBundle(Row $row) { * {@inheritdoc} */ public function fields(MigrationInterface $migration = NULL) { - // TODO: Implement fields() method. + $entity_type_id = self::getEntityTypeId($this->getPluginId()); + $fields = []; + if (!is_null($this->entityFieldManager)) { + if (isset($this->configuration['default_bundle'])) { + $default_bundle = $this->configuration['default_bundle']; + $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $default_bundle); + foreach ($field_definitions as $field_definition) { + $fields[$field_definition->getName()] = $field_definition->getLabel(); + } + } + else { + $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id); + foreach ($storage_definitions as $storage_definition) { + $fields[$storage_definition->getName()] = $storage_definition->getLabel(); + } + } + } + return $fields; } /** diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php index d4f9bd7..13c2c93 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\TypedData\TranslatableInterface; @@ -53,9 +54,11 @@ class EntityContentBase extends Entity { * The entity manager service. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The field type plugin manager service. + * @param EntityFieldManagerInterface $entity_field_manager + * The entity field manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles); + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, EntityFieldManagerInterface $entity_field_manager = NULL) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_field_manager); $this->entityManager = $entity_manager; $this->fieldTypeManager = $field_type_manager; } @@ -73,7 +76,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity.manager')->getStorage($entity_type), array_keys($container->get('entity.manager')->getBundleInfo($entity_type)), $container->get('entity.manager'), - $container->get('plugin.manager.field.field_type') + $container->get('plugin.manager.field.field_type'), + $container->get('entity_field.manager') ); } diff --git a/core/modules/migrate/tests/modules/migrate_destination_test/migrate_destination_test.info.yml b/core/modules/migrate/tests/modules/migrate_destination_test/migrate_destination_test.info.yml new file mode 100644 index 0000000..d8f21fb --- /dev/null +++ b/core/modules/migrate/tests/modules/migrate_destination_test/migrate_destination_test.info.yml @@ -0,0 +1,6 @@ +name: 'Migrate module destination tests' +type: module +description: 'Support module destination testing.' +package: Testing +version: VERSION +core: 8.x diff --git a/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_bundle_entity.yml b/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_bundle_entity.yml new file mode 100644 index 0000000..576509a --- /dev/null +++ b/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_bundle_entity.yml @@ -0,0 +1,12 @@ +id: destination_bundle_entity +label: Migrate to bundle specified destination +source: + plugin: migrate_destination_test + constants: + type: test_node_type +process: + type: constants/type + title: title +destination: + plugin: entity:node + default_bundle: test_node_type diff --git a/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_entity.yml b/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_entity.yml new file mode 100644 index 0000000..f3f1698 --- /dev/null +++ b/core/modules/migrate/tests/modules/migrate_destination_test/migrations/migrate.migration.destination_entity.yml @@ -0,0 +1,11 @@ +id: destination_entity +label: Migrate to no bundle specified destination +source: + plugin: migrate_destination_test + constants: + type: test_node_type +process: + type: constants/type + title: title +destination: + plugin: entity:node diff --git a/core/modules/migrate/tests/modules/migrate_destination_test/src/Plugin/migrate/source/MigrateDestinationTestSource.php b/core/modules/migrate/tests/modules/migrate_destination_test/src/Plugin/migrate/source/MigrateDestinationTestSource.php new file mode 100644 index 0000000..45c45f6 --- /dev/null +++ b/core/modules/migrate/tests/modules/migrate_destination_test/src/Plugin/migrate/source/MigrateDestinationTestSource.php @@ -0,0 +1,63 @@ + 'Cat'], + ['title' => 'Dog'], + ['title' => 'Monkey'], + ]; + + /** + * {@inheritdoc} + */ + public function fields() { + return [ + 'title' => $this->t('Title'), + ]; + } + + /** + * {@inheritdoc} + */ + public function __toString() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['title']['type'] = 'string'; + return $ids; + } + + /** + * {@inheritdoc} + */ + protected function initializeIterator() { + $data = []; + foreach ($this->import as $row) { + $data[] = $row; + } + + return new \ArrayIterator($data); + } + +} diff --git a/core/modules/migrate/tests/src/Kernel/MigrateEntityDestinationTest.php b/core/modules/migrate/tests/src/Kernel/MigrateEntityDestinationTest.php new file mode 100644 index 0000000..e3257da --- /dev/null +++ b/core/modules/migrate/tests/src/Kernel/MigrateEntityDestinationTest.php @@ -0,0 +1,89 @@ +installSchema('system', ['sequences']); + $this->installSchema('node', ['node_access']); + $this->installEntitySchema('user'); + $this->installEntitySchema('node'); + + $type = NodeType::create([ + 'type' => 'test_node_type', + 'name' => 'Test node type', + ]); + $type->save(); + + $type = NodeType::create([ + 'type' => 'test_node_type_2', + 'name' => 'Test node type 2', + ]); + $type->save(); + } + + /** + * Test destination fields() method. + */ + public function testDestinationField() { + $migration = $this->getMigration('destination_entity'); + $destination = $migration->getDestinationPlugin(); + + $migration2 = $this->getMigration('destination_bundle_entity'); + $destination2 = $migration2->getDestinationPlugin(); + + $this->assertTrue(in_array('nid', array_keys($destination->fields()))); + $this->assertFalse(in_array('field_text', array_keys($destination->fields()))); + + $this->assertTrue(in_array('nid', array_keys($destination2->fields()))); + $this->assertFalse(in_array('field_text', array_keys($destination2->fields()))); + + // Create a text field attached to 'test_node_type_2' node-type. + FieldStorageConfig::create([ + 'type' => 'string', + 'entity_type' => 'node', + 'field_name' => 'field_text', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'node', + 'bundle' => 'test_node_type_2', + 'field_name' => 'field_text', + ])->save(); + + $this->assertTrue(in_array('field_text', array_keys($destination->fields()))); + // The destination_bundle_entity migration has default bundle of + // test_node_type so it shouldn't show the fields on other node types. + $this->assertFalse(in_array('field_text', array_keys($destination2->fields()))); + + } + +} diff --git a/core/modules/user/src/Plugin/migrate/destination/EntityUser.php b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php index 7c2c81a..7138601 100644 --- a/core/modules/user/src/Plugin/migrate/destination/EntityUser.php +++ b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php @@ -5,6 +5,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem; @@ -49,9 +50,11 @@ class EntityUser extends EntityContentBase { * The field type plugin manager service. * @param \Drupal\Core\Password\PasswordInterface $password * The password service. + * @param EntityFieldManagerInterface $entity_field_manager + * The entity field manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager); + public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password, EntityFieldManagerInterface $entity_field_manager = NULL) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager, $entity_field_manager); $this->password = $password; } @@ -69,7 +72,8 @@ public static function create(ContainerInterface $container, array $configuratio array_keys($container->get('entity.manager')->getBundleInfo($entity_type)), $container->get('entity.manager'), $container->get('plugin.manager.field.field_type'), - $container->get('password') + $container->get('password'), + $container->get('entity_field.manager') ); } -- 2.5.4 (Apple Git-61)