Commit 54585069 authored by xjm's avatar xjm

Issue #2937782 by quietone, jofitz, yo30, heddn, Wim Leers, catch: Create...

Issue #2937782 by quietone, jofitz, yo30, heddn, Wim Leers, catch: Create trait for getDefinitionFromEntity
parent 80ce3e02
<?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;
}
}
......@@ -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.
*
......
......@@ -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}
*/
......
......@@ -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.
*
......
......@@ -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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment