class CommerceAdjustments in Commerce Migrate 3.1.x
Same name and namespace in other branches
- 8.2 src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments
- 3.0.x src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments
Converts a nested array into Commerce Adjustments.
The commerce adjustments process plugin converts adjustment data to Commerce Adjustments. The input value is an indexed array of adjustment data arrays.
Example:
process:
field_adjustment:
plugin: commerce_adjustments
source: input_array
process:
field_adjustment:
plugin: commerce_adjustments
source:
- input_array_A
- input_array_B
Plugin annotation
@MigrateProcessPlugin(
id = "commerce_adjustments",
handle_multiples = true
)
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\Plugin\migrate\process\CommerceAdjustments
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CommerceAdjustments
1 file declares its use of CommerceAdjustments
- CommerceAdjustmentsTest.php in tests/
src/ Kernel/ Plugin/ migrate/ process/ CommerceAdjustmentsTest.php
File
- src/
Plugin/ migrate/ process/ CommerceAdjustments.php, line 43
Namespace
Drupal\commerce_migrate\Plugin\migrate\processView source
class CommerceAdjustments extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_array($value) && !empty($value)) {
$adjustments = [];
$i = 0;
foreach ($value as $data) {
if ($data) {
$adjustment = [];
// Thrown an exception if amount and currency_code are not set. Let
// Price validate the amount.
$not_set = [];
foreach ([
'amount',
'currency_code',
] as $property) {
if (!isset($data[$property])) {
$not_set[] = $property;
}
}
if ($not_set) {
if (count($not_set) > 1) {
throw new MigrateSkipRowException(sprintf("Properties 'amount' and 'currency_code' are not set for adjustment '%s'", var_export($data, TRUE)));
}
else {
throw new MigrateSkipRowException(sprintf("Property '%s' is not set for adjustment '%s'", reset($not_set), var_export($data, TRUE)));
}
}
// Skip the row if the price can't be created.
try {
$adjustment['amount'] = new Price(Calculator::trim($data['amount']), $data['currency_code']);
} catch (\InvalidArgumentException $e) {
throw new MigrateSkipRowException(sprintf('Failed creating price for adjustment %s', var_export($data, TRUE)));
}
$adjustment['delta'] = $i++;
$adjustment['type'] = !empty($data['type']) ? $data['type'] : 'custom';
// Allow the label to be in a title or label property.
$adjustment['label'] = !empty($data['label']) ? $data['label'] : '';
if (empty($adjustment['label'])) {
$adjustment['label'] = !empty($data['title']) ? $data['title'] : 'Custom';
}
$adjustment['percentage'] = !empty($data['percentage']) ? $data['percentage'] : NULL;
$adjustment['source_id'] = !empty($data['source_id']) ? $data['source_id'] : 'custom';
$adjustment['included'] = !empty($data['included']) ? $data['included'] : FALSE;
$adjustment['locked'] = !empty($data['locked']) ? $data['locked'] : TRUE;
$adjustments[] = new Adjustment($adjustment);
}
}
return $adjustments;
}
return $value;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommerceAdjustments:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
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 |
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. |