Commit 86d3e5e7 authored by Dan Flanagan's avatar Dan Flanagan Committed by Dan Flanagan
Browse files

Issue #3319183 by danflanagan8, xurizaemon: Allow declaring source for migrate_conditions plugins

parent 0dccc720
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -20,6 +20,13 @@ abstract class ConditionBase extends PluginBase implements ConditionInterface {
   */
  protected $negated;

  /**
   * Properties to override the source value passed to evaluate().
   *
   * @var string|string[]
   */
  protected $source;

  /**
   * {@inheritdoc}
   */
@@ -32,6 +39,7 @@ abstract class ConditionBase extends PluginBase implements ConditionInterface {
      }
    }
    $this->negated = $configuration['negate'] ?? FALSE;
    $this->source = $configuration['source'] ?? NULL;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

@@ -39,6 +47,9 @@ abstract class ConditionBase extends PluginBase implements ConditionInterface {
   * {@inheritdoc}
   */
  public function evaluate($source, Row $row) {
    if ($this->source) {
      $source = $this->getSource($row);
    }
    return ($this->doEvaluate($source, $row) xor $this->negated);
  }

@@ -55,4 +66,18 @@ abstract class ConditionBase extends PluginBase implements ConditionInterface {
   */
  abstract protected function doEvaluate($source, Row $row);

  /**
   * {@inheritdoc}
   */
  protected function getSource(Row $row) {
    if (is_string($this->source)) {
      return $row->get($this->source);
    }
    $return = [];
    foreach ($this->source as $property) {
      $return[] = $row->get($property);
    }
    return $return;
  }

}
+3 −0
Original line number Diff line number Diff line
@@ -95,6 +95,9 @@ abstract class LogicalConditionBase extends ConditionBase implements ContainerFa
   * {@inheritdoc}
   */
  public function evaluate($source, Row $row) {
    if ($this->source) {
      $source = $this->getSource($row);
    }
    if ($this->iterate && !is_array($source)) {
      throw new MigrateException("If the 'iterate' property is true, the source must be an array.");
    }
+4 −0
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ use Drupal\migrate_conditions\Plugin\ArrayConditionBase;
 * - negate: (optional) Whether to negate the all_elements condition.
 *   Defaults to FALSE. You can also negate the 'all_elements' plugin by using
 *   'not:all_elements' as the plugin id.
 * - source: (optional) Property or array of properties on which to evaluate
 *   the condition. If not set, the condition will be evaluated on the source
 *   passed to the ::evaluate() method, typically the source of the process
 *   plugin that is using this condition.
 *
 * Example:
 *
+25 −0
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ use Drupal\migrate_conditions\Plugin\LogicalConditionBase;
 * - negate: (optional) Whether the 'and' condition should be negated.
 *   Defaults to FALSE. You can also negate the 'and' plugin by using
 *   'not:and' as the plugin id.
 * - source: (optional) Property or array of properties on which to evaluate
 *   the condition. If not set, the condition will be evaluated on the source
 *   passed to the ::evaluate() method, typically the source of the process
 *   plugin that is using this condition.
 *
 * Examples:
 *
@@ -92,6 +96,27 @@ use Drupal\migrate_conditions\Plugin\LogicalConditionBase;
 *           value: 'in charge'
 * @endcode
 *
 * The previous example is likely clearer if we configure the source
 * of each condition separately, rather than relying on the source
 * passed to the process plugin.
 *
 * @code
 * process:
 *   large_and_in_charge:
 *     plugin: evaluate_condition
 *     condition:
 *       plugin: and
 *       conditions:
 *         -
 *           plugin: equals
 *           value: 'large'
 *           source: source_size
 *         -
 *           plugin: equals
 *           value: 'in charge'
 *           source: source_comportment
 * @endcode
 *
 * @MigrateConditionsConditionPlugin(
 *   id = "and",
 *   required = {"conditions"}
+4 −0
Original line number Diff line number Diff line
@@ -22,6 +22,10 @@ use Drupal\migrate_conditions\Plugin\ConditionBase;
 * - negate: (optional) Whether the result of the callable function should be
 *   negated. Defaults to FALSE. You can also negate the result of the
 *   callable function by using 'not:callback' as the plugin id.
 * - source: (optional) Property or array of properties on which to evaluate
 *   the condition. If not set, the condition will be evaluated on the source
 *   passed to the ::evaluate() method, typically the source of the process
 *   plugin that is using this condition.
 *
 * Examples:
 *
Loading