public function CartForm::buildForm in Ubercart 8.4
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- uc_cart/
src/ Form/ CartForm.php, line 82
Class
- CartForm
- Displays the contents of the customer's cart.
Namespace
Drupal\uc_cart\FormCode
public function buildForm(array $form, FormStateInterface $form_state, CartInterface $cart = NULL) {
$form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
$cart_config = $this
->config('uc_cart.settings');
$form['items'] = [
'#type' => 'table',
'#tree' => TRUE,
'#header' => [
'remove' => [
'data' => $this
->t('Remove'),
'class' => [
'remove',
],
],
'image' => [
'data' => '',
'class' => [
'image',
RESPONSIVE_PRIORITY_LOW,
],
],
'desc' => [
'data' => $this
->t('Product'),
'class' => [
'desc',
],
],
'qty' => [
'data' => $this
->t('Quantity'),
'class' => [
'qty',
],
],
'total' => [
'data' => $this
->t('Total'),
'class' => [
'price',
],
],
],
];
$form['data'] = [
'#tree' => TRUE,
'#parents' => [
'items',
],
];
$i = 0;
$subtotal = 0;
foreach ($cart
->getContents() as $cart_item) {
$item = $this->moduleHandler
->invoke($cart_item->data->module, 'uc_cart_display', [
$cart_item,
]);
if (Element::children($item)) {
$form['items'][$i]['remove'] = $item['remove'];
$form['items'][$i]['remove']['#name'] = 'remove-' . $i;
$form['items'][$i]['remove']['#wrapper_attributes'] = [
'class' => [
'remove',
],
];
$form['items'][$i]['image'] = uc_product_get_picture($item['nid']['#value'], 'uc_cart');
$form['items'][$i]['image']['#wrapper_attributes'] = [
'class' => [
'image',
],
];
$form['items'][$i]['desc']['title'] = $item['title'];
$form['items'][$i]['desc']['description'] = $item['description'];
$form['items'][$i]['desc']['#wrapper_attributes'] = [
'class' => [
'desc',
],
];
$form['items'][$i]['qty'] = $item['qty'];
$form['items'][$i]['qty']['#wrapper_attributes'] = [
'class' => [
'qty',
],
];
$form['items'][$i]['total'] = [
'#theme' => 'uc_price',
'#price' => $item['#total'],
'#wrapper_attributes' => [
'class' => [
'price',
],
],
];
if (!empty($item['#suffixes'])) {
$form['items'][$i]['total']['#suffixes'] = $item['#suffixes'];
}
$form['data'][$i]['module'] = $item['module'];
$form['data'][$i]['nid'] = $item['nid'];
$form['data'][$i]['data'] = $item['data'];
$form['data'][$i]['title'] = [
'#type' => 'value',
// $item['title'] can be either #markup or #type => 'link',
// so render it.
'#value' => drupal_render($item['title']),
];
$subtotal += $item['#total'];
}
$i++;
}
$footer[] = [
[
'',
],
[
'',
],
[
'data' => [
'#markup' => $this
->t('Subtotal:'),
],
'colspan' => 2,
'class' => [
'subtotal-title',
],
],
[
'data' => [
'#theme' => 'uc_price',
'#price' => $subtotal,
],
'class' => [
'price',
],
],
];
$form['items']['#footer'] = $footer;
$form['actions'] = [
'#type' => 'actions',
];
// If the continue shopping element is enabled...
if (($cs_type = $cart_config
->get('continue_shopping_type')) !== 'none') {
// Add the element to the form based on the element type.
if ($cart_config
->get('continue_shopping_type') == 'link') {
$form['actions']['continue_shopping'] = [
'#type' => 'link',
'#title' => $this
->t('Continue shopping'),
'#url' => Url::fromUri('internal:' . $this
->continueShoppingUrl()),
];
}
elseif ($cart_config
->get('continue_shopping_type') == 'button') {
$form['actions']['continue_shopping'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue shopping'),
'#submit' => [
[
$this,
'submitForm',
],
[
$this,
'continueShopping',
],
],
];
}
}
// Add the empty cart button if enabled.
if ($cart_config
->get('empty_button')) {
$form['actions']['empty'] = [
'#type' => 'submit',
'#value' => $this
->t('Empty cart'),
'#submit' => [
[
$this,
'emptyCart',
],
],
];
}
// Add the control buttons for updating and proceeding to checkout.
$form['actions']['update'] = [
'#type' => 'submit',
'#name' => 'update-cart',
'#value' => $this
->t('Update cart'),
'#submit' => [
[
$this,
'submitForm',
],
[
$this,
'displayUpdateMessage',
],
],
];
$form['actions']['checkout'] = [
'#theme' => 'uc_cart_checkout_buttons',
];
if ($cart_config
->get('checkout_enabled')) {
$form['actions']['checkout']['checkout'] = [
'#type' => 'submit',
'#value' => $this
->t('Checkout'),
'#button_type' => 'primary',
'#submit' => [
[
$this,
'submitForm',
],
[
$this,
'checkout',
],
],
];
}
$this->renderer
->addCacheableDependency($form, $cart);
$this->renderer
->addCacheableDependency($form, $cart_config);
return $form;
}