class ResolveProductVariationType in Commerce Migrate 3.0.x
Same name and namespace in other branches
- 8.2 modules/commerce/src/Plugin/migrate/process/commerce1/ResolveProductVariationType.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\ResolveProductVariationType
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\ResolveProductVariationType
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\commerce1View 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
ResolveProductVariationType:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |