You are here

class EntityGenerate in Migrate Plus 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
  2. 8.2 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
  3. 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

Expanded class hierarchy of EntityGenerate

See also

EntityLookup

File

src/Plugin/migrate/process/EntityGenerate.php, line 47

Namespace

Drupal\migrate_plus\Plugin\migrate\process
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityGenerate::$getProcessPlugin protected property The get process plugin instance.
EntityGenerate::$processPluginManager protected property The MigratePluginManager instance.
EntityGenerate::$row protected property The row from the source to process.
EntityGenerate::create public static function Creates an instance of the plugin. Overrides EntityLookup::create
EntityGenerate::entity protected function Fabricate an entity.
EntityGenerate::generateEntity protected function Generates an entity for a given value.
EntityGenerate::transform public function Performs the associated process. Overrides EntityLookup::transform
EntityGenerate::__construct public function EntityGenerate constructor. Overrides EntityLookup::__construct
EntityLookup::$accessCheck protected property The access check flag.
EntityLookup::$destinationBundleKey protected property The destination bundle.
EntityLookup::$destinationEntityType protected property The destination type.
EntityLookup::$destinationProperty protected property The destination property or field.
EntityLookup::$entityManager protected property The entity manager.
EntityLookup::$lookupBundle protected property The lookup bundle.
EntityLookup::$lookupBundleKey protected property The lookup bundle's key.
EntityLookup::$lookupEntityType protected property The lookup entity type.
EntityLookup::$lookupValueKey protected property The lookup value's key.
EntityLookup::$migration protected property The migration.
EntityLookup::$selectionPluginManager protected property The selection plugin.
EntityLookup::determineLookupProperties protected function Determine the lookup properties from config or target field configuration.
EntityLookup::query protected function Checks for the existence of some value.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.