public function EditQuantity::viewsForm in Commerce Core 8.2
Form constructor for the views form.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- modules/
cart/ src/ Plugin/ views/ field/ EditQuantity.php, line 127
Class
- EditQuantity
- Defines a form element for editing the order item quantity.
Namespace
Drupal\commerce_cart\Plugin\views\fieldCode
public function viewsForm(array &$form, FormStateInterface $form_state) {
// Make sure we do not accidentally cache this form.
$form['#cache']['max-age'] = 0;
// The view is empty, abort.
if (empty($this->view->result)) {
unset($form['actions']);
return;
}
$form['#attached'] = [
'library' => [
'commerce_cart/cart_form',
],
];
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $this
->getEntity($row);
if ($this->options['allow_decimal']) {
$form_display = commerce_get_entity_display('commerce_order_item', $order_item
->bundle(), 'form');
$quantity_component = $form_display
->getComponent('quantity');
$step = $quantity_component['settings']['step'];
$precision = $step >= '1' ? 0 : strlen($step) - 2;
}
else {
$step = 1;
$precision = 0;
}
if (!$order_item
->isLocked()) {
$form[$this->options['id']][$row_index] = [
'#type' => 'number',
'#title' => $this
->t('Quantity'),
'#title_display' => 'invisible',
'#default_value' => round($order_item
->getQuantity(), $precision),
'#size' => 4,
'#min' => 0,
'#max' => 9999,
'#step' => $step,
'#required' => TRUE,
'#attributes' => [
'class' => [
'quantity-edit-input',
],
],
];
}
else {
$form[$this->options['id']][$row_index] = [
'#type' => 'item',
'#plain_text' => round($order_item
->getQuantity(), $precision),
];
}
}
$form['actions']['submit']['#update_cart'] = TRUE;
$form['actions']['submit']['#show_update_message'] = TRUE;
// Replace the form submit button label.
$form['actions']['submit']['#value'] = $this
->t('Update cart');
}