You are here

class EntityGenerate in Migrate Plus 8.2

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.3 src/Plugin/migrate/process/EntityGenerate.php \Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate
  3. 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

Expanded class hierarchy of EntityGenerate

See also

EntityLookup

File

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

Namespace

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

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::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
EntityLookup::$destinationBundleKey protected property @var string|bool
EntityLookup::$destinationEntityType protected property @var string
EntityLookup::$destinationProperty protected property @var string
EntityLookup::$entityManager protected property @var \Drupal\Core\Entity\EntityManagerInterface
EntityLookup::$lookupBundle protected property @var string
EntityLookup::$lookupBundleKey protected property @var string
EntityLookup::$lookupEntityType protected property @var string
EntityLookup::$lookupValueKey protected property @var string
EntityLookup::$migration protected property @var \Drupal\migrate\Plugin\MigrationInterface
EntityLookup::$selectionPluginManager protected property @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface
EntityLookup::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
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.
EntityLookup::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
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.