From 545850691ec2edc14407c08af9074e66623a8908 Mon Sep 17 00:00:00 2001 From: xjm Date: Fri, 24 Jan 2020 06:29:02 -0600 Subject: [PATCH] Issue #2937782 by quietone, jofitz, yo30, heddn, Wim Leers, catch: Create trait for getDefinitionFromEntity --- .../src/EntityFieldDefinitionTrait.php | 56 +++++++++++++++++++ .../src/Plugin/migrate/destination/Entity.php | 16 +----- .../migrate/destination/EntityContentBase.php | 28 ---------- .../migrate/destination/EntityRevision.php | 8 --- .../Plugin/migrate/source/ContentEntity.php | 23 +------- 5 files changed, 60 insertions(+), 71 deletions(-) create mode 100644 core/modules/migrate/src/EntityFieldDefinitionTrait.php diff --git a/core/modules/migrate/src/EntityFieldDefinitionTrait.php b/core/modules/migrate/src/EntityFieldDefinitionTrait.php new file mode 100644 index 00000000000..bc8af81909b --- /dev/null +++ b/core/modules/migrate/src/EntityFieldDefinitionTrait.php @@ -0,0 +1,56 @@ +getPluginId(); + $entity_type_id = $this->getEntityTypeId($plugin_id); + /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $definitions */ + $definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); + $field_definition = $definitions[$key]; + + return [ + 'type' => $field_definition->getType(), + ] + $field_definition->getSettings(); + } + + /** + * Finds the entity type from configuration or plugin ID. + * + * @param string $plugin_id + * The plugin ID. + * + * @return string + * The entity type. + */ + protected static function getEntityTypeId($plugin_id) { + $entity_type_id = NULL; + if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) { + list(, $entity_type_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2); + } + return $entity_type_id; + } + +} diff --git a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php index 12792005599..6bd1ec4edf9 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/Entity.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php @@ -6,6 +6,7 @@ use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Core\Entity\DependencyTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\migrate\EntityFieldDefinitionTrait; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -53,6 +54,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface { use DependencyTrait; + use EntityFieldDefinitionTrait; /** * The entity storage. @@ -110,20 +112,6 @@ abstract class Entity extends DestinationBase implements ContainerFactoryPluginI ); } - /** - * Finds the entity type from configuration or plugin ID. - * - * @param string $plugin_id - * The plugin ID. - * - * @return string - * The entity type. - */ - protected static function getEntityTypeId($plugin_id) { - // Remove "entity:". - return substr($plugin_id, 7); - } - /** * Gets the bundle for the row taking into account the default. * diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php index 38fbe994c0e..f3262a811a7 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -363,34 +363,6 @@ class EntityContentBase extends Entity implements HighestIdInterface, MigrateVal } } - /** - * Gets the field definition from a specific entity base field. - * - * The method takes the field ID as an argument and returns the field storage - * definition to be used in getIds() by querying the destination entity base - * field definition. - * - * @param string $key - * The field ID key. - * - * @return array - * An associative array with a structure that contains the field type, keyed - * as 'type', together with field storage settings as they are returned by - * FieldStorageDefinitionInterface::getSettings(). - * - * @see \Drupal\Core\Field\FieldStorageDefinitionInterface::getSettings() - */ - protected function getDefinitionFromEntity($key) { - $entity_type_id = static::getEntityTypeId($this->getPluginId()); - /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $definitions */ - $definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id); - $field_definition = $definitions[$key]; - - return [ - 'type' => $field_definition->getType(), - ] + $field_definition->getSettings(); - } - /** * {@inheritdoc} */ diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php index 4b651fe9f80..55ac460b5d3 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityRevision.php @@ -121,14 +121,6 @@ class EntityRevision extends EntityContentBase { parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_field_manager, $field_type_manager); } - /** - * {@inheritdoc} - */ - protected static function getEntityTypeId($plugin_id) { - // Remove entity_revision: - return substr($plugin_id, 16); - } - /** * Gets the entity. * diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php index 1219510fa3c..e80de1b7f80 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\migrate\EntityFieldDefinitionTrait; use Drupal\migrate\Plugin\migrate\source\SourcePluginBase; use Drupal\migrate\Plugin\MigrationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -54,6 +55,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * ) */ class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginInterface { + use EntityFieldDefinitionTrait; /** * The entity type manager. @@ -270,25 +272,4 @@ class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginIn return $ids; } - /** - * Gets the field definition from a specific entity base field. - * - * @param string $key - * The field ID key. - * - * @return array - * An associative array with a structure that contains the field type, keyed - * as 'type', together with field storage settings as they are returned by - * FieldStorageDefinitionInterface::getSettings(). - * - * @see \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::getDefinitionFromEntity() - */ - protected function getDefinitionFromEntity($key) { - /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ - $field_definition = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id())[$key]; - return [ - 'type' => $field_definition->getType(), - ] + $field_definition->getSettings(); - } - }