Add set_on_condition process plugin

Created on 13 May 2024, 7 months ago

Problem/Motivation

Is this a good idea?

<?php

namespace Drupal\migrate_conditions\Plugin\migrate\process;

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate_conditions\Plugin\ProcessPluginWithConditionBase;

/**
 * Sets destination property with source value if condition is met.
 *
 * This is most useful when re-migrating content. It is part of a possible
 * solution to the problem of how to avoid overwriting existing data
 *
 * Available configuration keys:
 * - dest_id: The destination id to set with the source value.
 * - condition: The condition plugin to evaluate on each element.
 *   Can be either:
 *   - The id of the condition. This is possible if the condition does not
 *     require any configuration, such as the 'empty' condition.
 *   - An array with a 'plugin' key that is the id of the condition.
 *     Any additional properties will be used as configuration when
 *     creating an instance of the condition.
 *
 * Examples:
 *
 * Don't overwrite existing values on field_my_field. Write my_source_value
 * to field_foo as long as field_foo is not already set. We assume that you
 * have somehow found the nid into which you are migrating, possibly using
 * a migrate_lookup or entity_lookup or possibly because it's in the source.
 *
 * @code
 * process:
 *   _field_foo:
 *     plugin: entity_value
 *     source: nid_into_which_we_are_migrating
 *     entity_type: node
 *     field_name: field_foo
 *   _set_field_foo:
 *     plugin: set_on_condition
 *     dest_id: field_foo
 *     source: my_source_value
 *     condition:
 *       plugin: empty
 *       source: '@_field_foo'
 * @endcode
 *
 * This is different from using overwrite_properties since (a) we don't have
 * to maintain a list of overwrite_properties if the migration is edited and
 * (b) the logic depends on the current value of the field rather than solely
 * whether the node is new.
 *
 * Skip the row is the source_value is not empty.
 * @MigrateProcessPlugin(
 *   id = "set_on_condition",
 *   handle_multiples = TRUE
 * )
 */
class SetOnCondition extends ProcessPluginWithConditionBase {

  /**
   * Constructs a SetOnCondition object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $condition_manager
   *   The MigrateCondition plugin manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $condition_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $condition_manager);
    if (!isset($configuration['dest_id'])) {
      throw new \InvalidArgumentException('The "dest_id" must be configured.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if ($this->condition->evaluate($value, $row)) {
      $row->setDestinationProperty($this->configuration['dest_id'], $value);
      return TRUE;
    }
    return FALSE;
  }

}
✨ Feature request
Status

Needs review

Version

2.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

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

Comments & Activities

Production build 0.71.5 2024