public function UnitPriceWidget::extractFormValues in Commerce Core 8.2
Extracts field values from submitted form values.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values. This parameter is altered by reference to receive the incoming form values.
array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Overrides WidgetBase::extractFormValues
File
- modules/
order/ src/ Plugin/ Field/ FieldWidget/ UnitPriceWidget.php, line 154
Class
- UnitPriceWidget
- Plugin implementation of the 'commerce_unit_price' widget.
Namespace
Drupal\commerce_order\Plugin\Field\FieldWidgetCode
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$path = array_merge($form['#parents'], [
$field_name,
0,
]);
$values = NestedArray::getValue($form_state
->getValues(), $path);
$order_item = $items
->getEntity();
assert($order_item instanceof OrderItemInterface);
$unit_price = NULL;
if (!empty($values['override']) || !$this
->getSetting('require_confirmation')) {
// Verify the passed number was numeric before trying to set it.
try {
$unit_price = Price::fromArray($values['amount']);
$order_item
->setUnitPrice($unit_price, TRUE);
} catch (\InvalidArgumentException $e) {
$form_state
->setErrorByName(implode('][', $path), $this
->t('%title must be a number.', [
'%title' => $this->fieldDefinition
->getLabel(),
]));
}
}
elseif ($order_item
->isNew() && !$order_item
->getUnitPrice()) {
$purchased_entity = $order_item
->getPurchasedEntity();
if ($purchased_entity !== NULL) {
$order = $order_item
->getOrder();
if ($order === NULL) {
$form_object = $form_state
->getFormObject();
if ($form_object instanceof OrderForm) {
$order = $form_object
->getEntity();
if ($order instanceof OrderInterface) {
$context = new Context($order
->getCustomer(), $order
->getStore());
$unit_price = $this->chainPriceResolver
->resolve($purchased_entity, $order_item
->getQuantity(), $context);
$order_item
->setUnitPrice($unit_price, FALSE);
}
}
}
}
}
// Put delta mapping in $form_state, so that flagErrors() can use it.
$field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
foreach ($items as $delta => $item) {
$field_state['original_deltas'][$delta] = $delta;
}
static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
}