You are here

public function CartLinksForm::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 ConfirmFormBase::buildForm

File

uc_cart_links/src/Form/CartLinksForm.php, line 145

Class

CartLinksForm
Preprocesses a cart link, confirming with the user for destructive actions.

Namespace

Drupal\uc_cart_links\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $actions = NULL) {
  $cart_links_config = $this
    ->config('uc_cart_links.settings');
  $this->actions = $actions;

  // Fail if the link is restricted.
  $data = trim($cart_links_config
    ->get('restrictions'));
  if (!empty($data)) {
    $restrictions = explode("\n", $cart_links_config
      ->get('restrictions'));
    $restrictions = array_map('trim', $restrictions);
    if (!empty($restrictions) && !in_array($this->actions, $restrictions)) {

      // A destination from the request's query will always override the
      // current RedirectResponse.
      $this
        ->getRequest()->query
        ->remove('destination');
      $path = $cart_links_config
        ->get('invalid_page');
      if (empty($path)) {
        return $this
          ->redirect('<front>');
      }
      return new RedirectResponse(Url::fromUri('internal:/' . $path, [
        'absolute' => TRUE,
      ])
        ->toString());
    }
  }

  // Confirm with the user if the form contains a destructive action.
  $cart = $this->cartManager
    ->get();
  $items = $cart
    ->getContents();
  if ($cart_links_config
    ->get('empty') && !empty($items)) {
    $actions = explode('-', urldecode($this->actions));
    foreach ($actions as $action) {
      $action = mb_substr($action, 0, 1);
      if ($action == 'e' || $action == 'E') {
        $form = parent::buildForm($form, $form_state);
        $form['actions']['cancel']['#href'] = $cart_links_config
          ->get('invalid_page');
        return $form;
      }
    }
  }

  // No destructive actions, so process the link immediately.
  return $this
    ->submitForm($form, $form_state);
}