You are here

public function CheckoutForm::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/CheckoutForm.php, line 78

Class

CheckoutForm
The checkout form built up from the enabled checkout panes.

Namespace

Drupal\uc_cart\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $order = NULL) {
  if ($processed = $form_state
    ->has('order')) {
    $order = $form_state
      ->get('order');
  }
  else {
    $form_state
      ->set('order', $order);
  }
  $form['#attributes']['class'][] = 'uc-cart-checkout-form';
  $form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
  $form['panes'] = [
    '#tree' => TRUE,
  ];
  $filter = [
    'enabled' => FALSE,
  ];

  // If the order isn't shippable, remove panes with shippable == TRUE.
  if (!$order
    ->isShippable() && $this
    ->config('uc_cart.settings')
    ->get('panes.delivery.settings.delivery_not_shippable')) {
    $filter['shippable'] = TRUE;
  }
  $panes = $this->checkoutPaneManager
    ->getPanes($filter);

  // Invoke the 'prepare' op of enabled panes, but only if their 'process' ops
  // have not been invoked on this request (i.e. when rebuilding after AJAX).
  foreach ($panes as $id => $pane) {
    if (!$form_state
      ->get([
      'panes',
      $id,
      'prepared',
    ])) {
      $pane
        ->prepare($order, $form, $form_state);
      $form_state
        ->set([
        'panes',
        $id,
        'prepared',
      ], TRUE);

      // Make sure we save the updated order.
      $processed = FALSE;
    }
  }

  // Load the line items and save the order. We do this after the 'prepare'
  // callbacks of enabled panes have been invoked, because these may have
  // altered the order.
  if (!$processed) {
    $order->line_items = $order
      ->getLineItems();
    $order
      ->save();
  }
  foreach ($panes as $id => $pane) {
    $form['panes'][$id] = $pane
      ->view($order, $form, $form_state);
    $form['panes'][$id] += [
      '#type' => 'details',
      '#title' => $pane
        ->getTitle(),
      '#id' => $id . '-pane',
      '#open' => TRUE,
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Cancel'),
    '#validate' => [],
    '#limit_validation_errors' => [],
    '#submit' => [
      [
        $this,
        'cancel',
      ],
    ],
  ];
  $form['actions']['continue'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Review order'),
    '#button_type' => 'primary',
  ];
  $form['#process'][] = [
    $this,
    'ajaxProcessForm',
  ];
  $this->session
    ->remove('uc_checkout_review_' . $order
    ->id());
  $this->session
    ->remove('uc_checkout_complete_' . $order
    ->id());
  return $form;
}