Problem/Motivation

Determine if a source value is empty and conditionally run a pipeline.
The if...else Statement
If (empty) (process1) else (process2)

Comments

joshua1234511 created an issue. See original summary.

joshua1234511’s picture

Status: Active » Needs review
<?php

namespace Drupal\migrate_plus\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * This plugin checks if a value is empty and conditionally run a pipeline.
 *
 * @MigrateProcessPlugin(
 *   id = "if_empty",
 *   handle_multiples = TRUE
 * )
 *
 * Required configuration keys:
 * - source: the value to evaluate.
 * - on_true: the plugin(s) that will process when the source is empty.
 * - on_false: the plugin(s) that will process when the source is not empty.
 *
 * Example usage:
 * @code
 * process:
 *   field_price:
 *     plugin: if_empty
 *     source: source_price
 *     on_true:
 *       plugin: default_value
 *       default_value: 'Free entry'
 *     on_false:
 *       -
 *         plugin: default_value
 *         default_value: '$'
 *       -
 *         plugin: concat
 *         source:
 *           -
 *           - source_price
 * @endcode
 */
class IfEmpty extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $new_row = clone $row;
    if (empty($value)) {
      $migrate_executable->processRow($new_row, [$destination_property => $this->configuration['on_true']]);
    }
    else {
      $migrate_executable->processRow($new_row, [$destination_property => $this->configuration['on_false']]);
    }
    $new_value = $new_row->getDestinationProperty($destination_property);

    return $new_value;
  }

}
mikelutz’s picture

Status: Needs review » Closed (won't fix)

This is doable several ways using existing plugins

In this case:

source:
  constants:
  currency_symbol: '$'
process:
  field_price:
    -
      plugin: concat
      source:
        - constants/currency_symbol
        - source_price
    -
      plugin: static_map
      map:
        '$': 'Free entry'

but in the more general case:

source:
  constants:
  currency_symbol: '$'
  free_entry: 'Free entry'
process:
  // pipeline for the value being empty goes here
  _empty_price: constants/free_entry
  // pipeline if it's not empty goes here, start with a skip_on_empty, ensuring this is null if the value is empty.
  _processed_price:
    - plugin: skip_on_empty
       method: process
       source: source_price
    - plugin: concat
        source:
          - constants/currency_symbol
          - source_price 
  field_price:
    // use the first non-null value. if the source is empty, '@_processed_price' will be null, and the empty price string will be used.
    // don't actually need the _empty_price pipeline here, the second argument could just be constants/free_entry, I'm only using it
    // to show where you could do additional processing.
    plugin: null_coalesce
    source:
      - '@_processed_price'
      - '@_empty_price'