class EntityGenerate in Migrate Plus 8.4
Same name and namespace in other branches
- 8.5 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
- 8.2 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
- 8.3 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
This plugin generates entities within the process plugin.
@MigrateProcessPlugin( id = "entity_generate" )
All the configuration from the lookup plugin applies here. In its most simple form, this plugin needs no configuration. If there are fields on the generated entity that are required or need some value, their values can be provided via values and/or default_values configuration options.
Example usage with values and default_values configuration:
destination:
plugin: 'entity:node'
process:
type:
plugin: default_value
default_value: page
foo: bar
field_tags:
plugin: entity_generate
source: tags
default_values:
description: Default description
values:
field_long_description: some_source_field
field_foo: '@foo'
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup implements ContainerFactoryPluginInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
- class \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup implements ContainerFactoryPluginInterface
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityGenerate
See also
File
- src/
Plugin/ migrate/ process/ EntityGenerate.php, line 47
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class EntityGenerate extends EntityLookup {
/**
* The row from the source to process.
*
* @var \Drupal\migrate\Row
*/
protected $row;
/**
* The MigratePluginManager instance.
*
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
*/
protected $processPluginManager;
/**
* The get process plugin instance.
*
* @var \Drupal\migrate\Plugin\migrate\process\Get
*/
protected $getProcessPlugin;
/**
* EntityGenerate constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $pluginId
* The plugin_id for the plugin instance.
* @param mixed $pluginDefinition
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The $entityManager instance.
* @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selectionPluginManager
* The $selectionPluginManager instance.
* @param \Drupal\migrate\Plugin\MigratePluginManager $processPluginManager
* The MigratePluginManager instance.
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration, EntityManagerInterface $entityManager, SelectionPluginManagerInterface $selectionPluginManager, MigratePluginManager $processPluginManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition, $migration, $entityManager, $selectionPluginManager);
$this->processPluginManager = $processPluginManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition, MigrationInterface $migration = NULL) {
return new static($configuration, $pluginId, $pluginDefinition, $migration, $container
->get('entity.manager'), $container
->get('plugin.manager.entity_reference_selection'), $container
->get('plugin.manager.migrate.process'));
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
$this->row = $row;
$this->migrateExecutable = $migrateExecutable;
// Creates an entity if the lookup determines it doesn't exist.
if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
$result = $this
->generateEntity($value);
}
return $result;
}
/**
* Generates an entity for a given value.
*
* @param string $value
* Value to use in creation of the entity.
*
* @return int|string
* The entity id of the generated entity.
*/
protected function generateEntity($value) {
if (!empty($value)) {
$entity = $this->entityManager
->getStorage($this->lookupEntityType)
->create($this
->entity($value));
$entity
->save();
return $entity
->id();
}
}
/**
* Fabricate an entity.
*
* This is intended to be extended by implementing classes to provide for more
* dynamic default values, rather than just static ones.
*
* @param mixed $value
* Primary value to use in creation of the entity.
*
* @return array
* Entity value array.
*/
protected function entity($value) {
$entity_values = [
$this->lookupValueKey => $value,
];
if ($this->lookupBundleKey) {
$entity_values[$this->lookupBundleKey] = $this->lookupBundle;
}
// Gather any static default values for properties/fields.
if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
foreach ($this->configuration['default_values'] as $key => $value) {
$entity_values[$key] = $value;
}
}
// Gather any additional properties/fields.
if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
foreach ($this->configuration['values'] as $key => $property) {
// TODO: Remove this logic once 8.6 is no longer supported.
// See https://www.drupal.org/project/migrate_plus/issues/3043199
if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
$source_value = $this->row
->get($property);
}
else {
$getProcessPlugin = $this->processPluginManager
->createInstance('get', [
'source' => $property,
]);
$source_value = $getProcessPlugin
->transform(NULL, $this->migrateExecutable, $this->row, $property);
}
NestedArray::setValue($entity_values, explode(Row::PROPERTY_SEPARATOR, $key), $source_value, TRUE);
}
}
return $entity_values;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityGenerate:: |
protected | property | The get process plugin instance. | |
EntityGenerate:: |
protected | property | The MigratePluginManager instance. | |
EntityGenerate:: |
protected | property | The row from the source to process. | |
EntityGenerate:: |
public static | function |
Creates an instance of the plugin. Overrides EntityLookup:: |
|
EntityGenerate:: |
protected | function | Fabricate an entity. | |
EntityGenerate:: |
protected | function | Generates an entity for a given value. | |
EntityGenerate:: |
public | function |
Performs the associated process. Overrides EntityLookup:: |
|
EntityGenerate:: |
public | function |
EntityGenerate constructor. Overrides EntityLookup:: |
|
EntityLookup:: |
protected | property | The access check flag. | |
EntityLookup:: |
protected | property | The destination bundle. | |
EntityLookup:: |
protected | property | The destination type. | |
EntityLookup:: |
protected | property | The destination property or field. | |
EntityLookup:: |
protected | property | The entity manager. | |
EntityLookup:: |
protected | property | The lookup bundle. | |
EntityLookup:: |
protected | property | The lookup bundle's key. | |
EntityLookup:: |
protected | property | The lookup entity type. | |
EntityLookup:: |
protected | property | The lookup value's key. | |
EntityLookup:: |
protected | property | The migration. | |
EntityLookup:: |
protected | property | The selection plugin. | |
EntityLookup:: |
protected | function | Determine the lookup properties from config or target field configuration. | |
EntityLookup:: |
protected | function | Checks for the existence of some value. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |