diff --git a/core/modules/migrate/src/EntityFieldDefinitionTrait.php b/core/modules/migrate/src/EntityFieldDefinitionTrait.php new file mode 100644 index 0000000000..f88e36b4a4 --- /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, ':')) { + list(, $entity_type_id) = explode(':', $plugin_id, 2); + } + return $entity_type_id; + } + +} diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php index 3f8dae982f..7d141d565a 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -12,6 +12,7 @@ use Drupal\Core\TypedData\TranslatableInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\migrate\Audit\HighestIdInterface; +use Drupal\migrate\EntityFieldDefinitionTrait; use Drupal\migrate\Exception\EntityValidationException; use Drupal\migrate\Plugin\MigrateValidatableEntityInterface; use Drupal\migrate\Plugin\MigrationInterface; @@ -89,6 +90,7 @@ */ class EntityContentBase extends Entity implements HighestIdInterface, MigrateValidatableEntityInterface { use DeprecatedServicePropertyTrait; + use EntityFieldDefinitionTrait; /** * {@inheritdoc} @@ -370,34 +372,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_drupal/src/Plugin/migrate/source/ContentEntity.php b/core/modules/migrate_drupal/src/Plugin/migrate/source/ContentEntity.php index 1219510fa3..e80de1b7f8 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(); - } - }