public function StockLevel::setValue in Commerce Stock 8
@inheritdoc
This updates the stock based on parameters set by the stock widget.
For computed fields we didn't find a chance to trigger the transaction, other than in ::setValue(). ::postSave() is not called for computed fields.
If you pass in a single value programmatically, note that we do not support the setting of a absolute stock levels here. We assume a stock adjustment if we get a singe value here. As usual a negative value decreases the stock level and a positive value increases the stock level.
Throws
\InvalidArgumentException In case of a invalid stock level value.
Overrides FieldItemBase::setValue
File
- modules/
field/ src/ Plugin/ Field/ FieldType/ StockLevel.php, line 101
Class
- StockLevel
- Plugin implementation of the 'commerce_stock_field' field type.
Namespace
Drupal\commerce_stock_field\Plugin\Field\FieldTypeCode
public function setValue($values, $notify = TRUE) {
// Supports absolute values being passed in directly, i.e.
// programmatically.
if (!is_array($values)) {
$value = filter_var($values, FILTER_VALIDATE_FLOAT);
if ($value !== FALSE) {
$values = [
'adjustment' => $value,
];
}
else {
throw new \InvalidArgumentException('Values passed to the commerce stock level field must be floats');
}
}
// Set the value so it is not recognized as empty by isEmpty() and
// postSave() is called.
if (isset($values['value'])) {
$values['value'] = $values['value'];
}
elseif (isset($values['adjustment'])) {
$values['value'] = $values['adjustment'];
}
else {
$values['value'] = 0.0;
}
parent::setValue($values, $notify);
}