Issue #2937782 by quietone, jofitz, yo30, heddn, Wim Leers, catch: Create trait for getDefinitionFromEntity

merge-requests/2419/head
xjm 2020-01-24 06:29:02 -06:00
parent 80ce3e025f
commit 545850691e
5 changed files with 60 additions and 71 deletions

View File

@ -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;
}
}

View File

@ -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.
*

View File

@ -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}
*/

View File

@ -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.
*

View File

@ -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();
}
}