You are here

public function CartForm::buildForm in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()
  2. 8.0 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()
  3. 8.2 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()
  4. 8.3 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()
  5. 8.4 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()
  6. 8.5 src/Form/CartForm.php \Drupal\basic_cart\Form\CartForm::buildForm()

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

src/Form/CartForm.php, line 26

Class

CartForm

Namespace

Drupal\basic_cart\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#theme'] = 'cart_form';
  $cart = Utility::getCart();
  $config = Utility::cartSettings();
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $price = Utility::getTotalPrice();
  $total = Utility::formatPrice($price->total);
  $vat_is_enabled = (int) $config
    ->get('vat_state');
  $vat_value = !empty($vat_is_enabled) && $vat_is_enabled ? Utility::formatPrice($price->vat) : 0;

  // And now the form.
  $form['cartcontents'] = array(
    // Make the returned array come back in tree form.
    '#tree' => TRUE,
  );

  // Cart elements.
  foreach ($cart['cart_quantity'] as $nid => $quantity) {
    $variable = Utility::quantityPrefixData($nid);
    $form['cartcontents'][$nid] = array(
      '#type' => $config
        ->get('quantity_status') ? 'textfield' : 'markup',
      '#size' => 2,
      '#quantity_id' => $nid,
      "#suffix" => Utility::render('basic-cart-quantity-suffix.html.twig', $variable),
      "#prefix" => Utility::render('basic-cart-quantity-prefix.html.twig', $variable),
      '#default_value' => $quantity,
    );
  }
  $form['total_price'] = array(
    '#markup' => Utility::render('total-price-markup.html.twig', Utility::getTotalPriceMarkupData()),
  );

  // Buttons.
  $form['buttons'] = array(
    '#tree' => TRUE,
  );
  $form['buttons']['update'] = array(
    '#type' => 'submit',
    '#value' => t($config
      ->get('cart_update_button')),
    '#name' => "update",
  );
  if ($config
    ->get('order_status')) {
    $form['buttons']['checkout'] = array(
      '#type' => 'submit',
      '#value' => t('Checkout'),
      '#name' => "checkout",
    );
  }
  return $form;
}