You are here

public function ParagraphsProcessOnValue::transform in Paragraphs 8

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

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;
  }
}