public function ParagraphsProcessOnValue::transform

File

paragraphs/src/Plugin/migrate/process/ParagraphsProcessOnValue.php, line 34

Class

ParagraphsProcessOnValue
Runs a migration process on a value if a condition is met.

Namespace

Drupal\paragraphs\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (empty($this->configuration['source_value'])) {
    throw new \InvalidArgumentException("Required argument 'source_value' not set for paragraphs_process_on_value plugin");
  }
  if (!isset($this->configuration['expected_value'])) {
    throw new \InvalidArgumentException("Required argument 'expected_value' not set for paragraphs_process_on_value plugin");
  }
  if (empty($this->configuration['process']) || !is_array($this->configuration['process'])) {
    throw new \InvalidArgumentException("Required argument 'process' not set or invalid for paragraphs_process_on_value plugin");
  }
  $source_value = $row
    ->getSourceProperty($this->configuration['source_value']);
  if (is_null($source_value)) {

    // This is probably a migration that shouldn't be touched by Paragraphs.
    // For example, throwing an exception here would prevent the migration of
    // the comment field configurations.
    return $value;
  }
  if ($source_value === $this->configuration['expected_value']) {
    $process = $this->configuration['process'];

    // Append the current working value to the new source we are creating.
    $source = $row
      ->getSource();
    $source['paragraphs_process_on_value_source_field'] = $value;

    // If there is a single process plugin, add the source field.  If there
    // is an array of process plugins, add the source field to the first one.
    if (array_key_exists('plugin', $process)) {
      if (empty($process['source'])) {
        $process['source'] = 'paragraphs_process_on_value_source_field';
      }
    }
    else {
      if (empty($process[0]['source'])) {
        $process[0]['source'] = 'paragraphs_process_on_value_source_field';
      }
    }
    $source = $row
      ->getSource();
    $source['paragraphs_process_on_value_source_field'] = $value;
    $new_row = new Row($source, []);
    $migrate_executable
      ->processRow($new_row, [
      $destination_property => $process,
    ]);
    return $new_row
      ->getDestinationProperty($destination_property);
  }
  else {
    return $value;
  }
}