You are here

class ResolveProductVariationType 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
  2. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/ResolveProductVariationType.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\ResolveProductVariationType

Resolve the product variation type.

This plugin determines the product variation type referenced by a product type. This is necessary because in 2.x, products can only be mapped to a single product variation type, whereas in 1.x one product display node can be mapped to multiple product types.

The product variation type will be set to the same name as the product type, if that name exists in the Commerce 1 product reference field. If it does not exist then the name is set to 'default'. This solution may not work for all sites. In that case, this plugin can be overwritten by custom migration classes to provide the different logic for determining the target variation type. Another option is to modify the migration yml file to use the static_map process plugin instead.

Available configuration keys:

  • matching: (optional) Only used if there are more than one referenced product types. If set, returns the referenced type that matches the input type.
  • default: (optional) Only used if there are more than one referenced product types. A default type to use when a matching type is not found.

Example:


process:
  type:
    plugin: commerce1_resolve_product_variation_type
    source: type
    variations:
      matching: true
      default: trees

If the source value is 'boats' and there is a referenced type of 'boats' then the return vale is 'boats'. If that source value is not 'boats' then 'trees' is returned.

Plugin annotation


@MigrateProcessPlugin(
  id = "commerce1_resolve_product_variation_type"
)

Hierarchy

Expanded class hierarchy of ResolveProductVariationType

1 file declares its use of ResolveProductVariationType
ResolveProductVariationTypeTest.php in modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/ResolveProductVariationTypeTest.php

File

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

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1
View source
class ResolveProductVariationType extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
ResolveProductVariationType::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.