class EntityGenerate in Migrate Plus 8.2
Same name and namespace in other branches
- 8.5 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
- 8.4 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 default value, that can be provided via a default_values configuration option.
Example usage with default_values configuration:
destination:
plugin: 'entity:node'
process:
type:
plugin: default_value
default_value: page
field_tags:
plugin: entity_generate
source: tags
default_values:
description: Default description
field_long_description: Default long description
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 38
Namespace
Drupal\migrate_plus\Plugin\migrate\processView source
class EntityGenerate extends EntityLookup {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
// 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 $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;
}
}
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 | function | Fabricate an entity. | |
EntityGenerate:: |
protected | function | Generates an entity for a given value. | |
EntityGenerate:: |
public | function |
Performs the associated process. Overrides EntityLookup:: |
|
EntityLookup:: |
protected | property | @var string|bool | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var \Drupal\Core\Entity\EntityManagerInterface | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var string | |
EntityLookup:: |
protected | property | @var \Drupal\migrate\Plugin\MigrationInterface | |
EntityLookup:: |
protected | property | @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface | |
EntityLookup:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
EntityLookup:: |
protected | function | Determine the lookup properties from config or target field configuration. | |
EntityLookup:: |
protected | function | Checks for the existence of some value. | |
EntityLookup:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
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. |