- πΊπΈUnited States amaisano Boston
FYI I've made adjustments to this patch as a custom plugin in my repo, and made it support entity translation updates, too. Thanks.
- πΊπΈUnited States amaisano Boston
Here's my take on this, in a custom plugin:
<?php declare(strict_types = 1); namespace Drupal\sw_migrate\Plugin\migrate\process; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\Row; use Drupal\migrate_plus\Plugin\migrate\process\EntityLookup; /** * This plugin updates existing entities within the process plugin. * * All the configuration from the generate plugin applies here. * * Available configuration keys: * - langcode: (optional) If set, update translations instead of base entity. * * Example: * @code * destination: * plugin: 'entity:node' * process: * field_tags: * plugin: entity_update * source: '@_parent_id' * entity_type: paragraphs_library_item * langcode: '@_translation' * value_key: id * values: * label: title * @endcode * * @see \Drupal\migrate_plus\Plugin\migrate\process\EntityLookup * * @MigrateProcessPlugin( * id = "entity_update" * ) */ class EntityUpdate extends EntityLookup { /** * The current row. * * @var \Drupal\migrate\Row|null */ protected ?Row $row = NULL; /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { $this->row = $row; $result = parent::transform($value, $migrate_executable, $row, $destination_property); return $this->updateEntity($result, $value); } /** * Update an entity for a given target. * * @param string $target_id * The id of the entity. * @param mixed $value * The entity values. * * @return int|string * The entity id of the generated entity. */ protected function updateEntity($target_id, $value) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $this->entityTypeManager ->getStorage($this->lookupEntityType) ->load($target_id); $entity_values = $this->configuration['values']; // Support for plugin-level translation config: $translation = FALSE; $langcode = $this->configuration['langcode'] ?? NULL; if ($langcode) { // If langcode is present in plugin config, expand it: $langcode = $this->row->get($langcode); // Unless value is false, get the language translation: if ($langcode !== FALSE) { $translation = TRUE; } } if ($translation) { foreach ($entity_values as $key => $value) { $value = $this->row->get($value); $entity->getTranslation($langcode)->set($key, $value); } } else { foreach ($entity_values as $key => $value) { $value = $this->row->get($value); $entity->set($key, $value); } } $entity->save(); return $entity->id(); } }