Skip to content
Snippets Groups Projects
Commit f201570b authored by Mike Ryan's avatar Mike Ryan Committed by Mike Ryan
Browse files

Issue #2765543 by mikeryan: Deprecate "stub" language in entity_generate

parent bb296e33
No related branches found
No related tags found
No related merge requests found
<?php <?php
/**
* @file
* Contains Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate.
*/
namespace Drupal\migrate_plus\Plugin\migrate\process; namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row; use Drupal\migrate\Row;
/** /**
* This plugin generates entity stubs. * This plugin generates entities within the process plugin.
* *
* @MigrateProcessPlugin( * @MigrateProcessPlugin(
* id = "entity_generate" * id = "entity_generate"
...@@ -19,9 +14,9 @@ use Drupal\migrate\Row; ...@@ -19,9 +14,9 @@ use Drupal\migrate\Row;
* *
* @see EntityLookup * @see EntityLookup
* *
* All the configuration from the lookup plugin applies here. In it's most * 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 * simple form, this plugin needs no configuration. If there are fields on the
* stub entity that are required or need some default value, that can be * generated entity that are required or need some default value, that can be
* provided via a default_values configuration option. * provided via a default_values configuration option.
* *
* Example usage with default_values configuration: * Example usage with default_values configuration:
...@@ -36,8 +31,8 @@ use Drupal\migrate\Row; ...@@ -36,8 +31,8 @@ use Drupal\migrate\Row;
* plugin: entity_generate * plugin: entity_generate
* source: tags * source: tags
* default_values: * default_values:
* description: Stub description * description: Default description
* field_long_description: Stub long description * field_long_description: Default long description
* @endcode * @endcode
*/ */
class EntityGenerate extends EntityLookup { class EntityGenerate extends EntityLookup {
...@@ -46,7 +41,7 @@ class EntityGenerate extends EntityLookup { ...@@ -46,7 +41,7 @@ class EntityGenerate extends EntityLookup {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) { public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
// Creates a stub entity if one doesn't exist. // Creates an entity if the lookup determines it doesn't exist.
if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) { if (!($result = parent::transform($value, $migrateExecutable, $row, $destinationProperty))) {
$result = $this->generateEntity($value); $result = $this->generateEntity($value);
} }
...@@ -55,19 +50,19 @@ class EntityGenerate extends EntityLookup { ...@@ -55,19 +50,19 @@ class EntityGenerate extends EntityLookup {
} }
/** /**
* Generates stub entity for a given value. * Generates an entity for a given value.
* *
* @param string $value * @param string $value
* Value to use in creation of stub entity. * Value to use in creation of the entity.
* *
* @return int|string * @return int|string
* The entity id of the generated entity. * The entity id of the generated entity.
*/ */
protected function generateEntity($value) { protected function generateEntity($value) {
if(!empty($value)) { if (!empty($value)) {
$entity = $this->entityManager $entity = $this->entityManager
->getStorage($this->lookupEntityType) ->getStorage($this->lookupEntityType)
->create($this->stub($value)); ->create($this->entity($value));
$entity->save(); $entity->save();
return $entity->id(); return $entity->id();
...@@ -75,32 +70,32 @@ class EntityGenerate extends EntityLookup { ...@@ -75,32 +70,32 @@ class EntityGenerate extends EntityLookup {
} }
/** /**
* Fabricate a stub entity. * Fabricate an entity.
* *
* This is intended to be extended by implementing classes to provide for more * This is intended to be extended by implementing classes to provide for more
* dynamic default values, rather than just static ones. * dynamic default values, rather than just static ones.
* *
* @param $value * @param $value
* Value to use in creation of stub entity. * Primary value to use in creation of the entity.
* *
* @return array * @return array
* The stub entity. * Entity value array.
*/ */
protected function stub($value) { protected function entity($value) {
$stub = [$this->lookupValueKey => $value]; $entity_values = [$this->lookupValueKey => $value];
if ($this->lookupBundleKey) { if ($this->lookupBundleKey) {
$stub[$this->lookupBundleKey] = $this->lookupBundle; $entity_values[$this->lookupBundleKey] = $this->lookupBundle;
} }
// Gather any static default values for properties/fields. // Gather any static default values for properties/fields.
if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) { if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
foreach ($this->configuration['default_values'] as $key => $value) { foreach ($this->configuration['default_values'] as $key => $value) {
$stub[$key] = $value; $entity_values[$key] = $value;
} }
} }
return $stub; return $entity_values;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment