You are here

public function ResolveProductVariationType::transform in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/src/Plugin/migrate/process/commerce1/ResolveProductVariationType.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\ResolveProductVariationType::transform()
  2. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/ResolveProductVariationType.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\ResolveProductVariationType::transform()

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

modules/commerce/src/Plugin/migrate/process/commerce1/ResolveProductVariationType.php, line 58

Class

ResolveProductVariationType
Resolve the product variation type.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (!is_string($value)) {
    throw new MigrateException(sprintf("Input should be an string, instead it was of type '%s'", gettype($value)));
  }
  $new_value = $value;

  // Get all the product types for this commerce display type.
  $product_variation_types = array_filter($row
    ->getSourceProperty('data/settings/referenceable_types'));
  $count = count($product_variation_types);
  if ($count > 1) {

    // Assume the default type if it is set in the configuration.
    if (!empty($this->configuration['variations']['default'])) {
      $new_value = $this->configuration['variations']['default'];
    }

    // Try to find a variation type that matches the product type.
    if (isset($this->configuration['variations']['matching'])) {
      $key = array_search($value, $product_variation_types);
      if ($key !== FALSE) {
        $new_value = $product_variation_types[$key];
      }
    }
  }
  if ($count === 1) {
    $new_value = reset($product_variation_types);
  }
  return $new_value;
}