Updating referenced entities through entity_generate process plugin

Created on 8 October 2021, over 2 years ago
Updated 1 November 2023, 8 months ago

At the moment, the entity_generate plugin doesn't seem to update changes on referenced entities, once they exists.
Therefore I would suggest to allow an extra configuration setting (update = TRUE) to force to update value on referenced entities through the entity_generate plugin.

✨ Feature request
Status

Closed: won't fix

Version

5.0

Component

Plugins

Created by

πŸ‡§πŸ‡ͺBelgium kensae

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡Έ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();
      }
    
    }
    
    
Production build 0.69.0 2024