diff --git a/core/modules/migrate/src/EntityFieldDefinitionTrait.php b/core/modules/migrate/src/EntityFieldDefinitionTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..bc8af81909b98bf9700c704e58b23a07ec81c623 --- /dev/null +++ b/core/modules/migrate/src/EntityFieldDefinitionTrait.php @@ -0,0 +1,56 @@ +<?php + +namespace Drupal\migrate; + +/** + * The entity field definition trait. + */ +trait EntityFieldDefinitionTrait { + + /** + * 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) { + $plugin_id = $this->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 12792005599b1a3170caf4a46b885c9be88b1a7e..6bd1ec4edf99c3e204d397b92a36423be2444fbe 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\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 @@ abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface { use DependencyTrait; + use EntityFieldDefinitionTrait; /** * The entity storage. @@ -110,20 +112,6 @@ public static function create(ContainerInterface $container, array $configuratio ); } - /** - * 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 38fbe994c0e0c306ec946f7eeb24ecc42affe338..f3262a811a79e8de1e63dd3a4e90b7d267110bd9 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 @@ public function rollback(array $destination_identifier) { } } - /** - * 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 4b651fe9f80f724c393959d895b89bfa082a03c4..55ac460b5d3fd929e07a6c2bb614c5f2c00f07e2 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 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition 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 1219510fa3cc582f973765a80860b220828cc850..e80de1b7f80a892d174d9cb673464b9dfe611d24 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\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 @@ * ) */ class ContentEntity extends SourcePluginBase implements ContainerFactoryPluginInterface { + use EntityFieldDefinitionTrait; /** * The entity type manager. @@ -270,25 +272,4 @@ public function getIds() { 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(); - } - }