/**
  * Allows a customer to review their order before finally submitting it.
  */
 public function review()
 {
     if (!$this->session->has('cart_order') || !$this->session->has('uc_checkout_review_' . $this->session->get('cart_order'))) {
         return $this->redirect('uc_cart.checkout');
     }
     $order = $this->loadOrder();
     if (!$order || $order->getStateId() != 'in_checkout') {
         $this->session->remove('uc_checkout_complete_' . $this->session->get('cart_order'));
         return $this->redirect('uc_cart.checkout');
     } elseif (!uc_order_product_revive($order->products)) {
         drupal_set_message($this->t('Some of the products in this order are no longer available.'), 'error');
         return $this->redirect('uc_cart.cart');
     }
     $filter = array('enabled' => FALSE);
     // If the cart isn't shippable, bypass panes with shippable == TRUE.
     if (!$order->isShippable() && $this->config('uc_cart.settings')->get('delivery_not_shippable')) {
         $filter['shippable'] = TRUE;
     }
     $panes = $this->checkoutPaneManager->getPanes($filter);
     foreach ($panes as $pane) {
         $return = $pane->review($order);
         if (!is_null($return)) {
             $data[$pane->getTitle()] = $return;
         }
     }
     $build = array('#theme' => 'uc_cart_checkout_review', '#panes' => $data, '#form' => $this->formBuilder()->getForm('Drupal\\uc_cart\\Form\\CheckoutReviewForm', $order));
     $build['#attached']['library'][] = 'uc_cart/uc_cart.styles';
     $build['#attached']['library'][] = 'uc_cart/uc_cart.review.scripts';
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $cart_config = $this->config('uc_cart.settings');
     $messages = $this->config('uc_cart.messages');
     $form['checkout-settings'] = array('#type' => 'vertical_tabs', '#attached' => array('library' => array('uc_cart/uc_cart.admin.scripts')));
     $form['checkout'] = array('#type' => 'details', '#title' => t('Basic settings'), '#group' => 'checkout-settings', '#weight' => -10);
     $form['checkout']['uc_checkout_enabled'] = array('#type' => 'checkbox', '#title' => t('Enable checkout.'), '#description' => t('Disable this to use only third party checkout services, such as PayPal Express Checkout.'), '#default_value' => $cart_config->get('checkout_enabled'));
     //@todo: Uncomment this conditional when Rules actually works.
     //if (!\Drupal::moduleHandler()->moduleExists('rules')) {
     $form['checkout']['uc_checkout_email_customer'] = array('#type' => 'checkbox', '#title' => t('Send e-mail invoice to customer after checkout.'), '#default_value' => $cart_config->get('checkout_email_customer'));
     $form['checkout']['uc_checkout_email_admin'] = array('#type' => 'checkbox', '#title' => t('Send e-mail order notification to admin after checkout.'), '#default_value' => $cart_config->get('checkout_email_admin'));
     //}
     $form['anonymous'] = array('#type' => 'details', '#title' => t('Anonymous checkout'), '#group' => 'checkout-settings', '#weight' => -5);
     $form['anonymous']['uc_checkout_anonymous'] = array('#type' => 'checkbox', '#title' => t('Enable anonymous checkout.'), '#description' => t('Disable this to force users to log in before the checkout page.'), '#default_value' => $cart_config->get('checkout_anonymous'));
     $anon_state = array('visible' => array('input[name="uc_checkout_anonymous"]' => array('checked' => TRUE)));
     $form['anonymous']['uc_cart_mail_existing'] = array('#type' => 'checkbox', '#title' => t("Allow anonymous customers to use an existing account's email address."), '#default_value' => $cart_config->get('mail_existing'), '#description' => t('If enabled, orders will be attached to the account matching the email address. If disabled, anonymous users using a registered email address must log in or use a different email address.'), '#states' => $anon_state);
     $form['anonymous']['uc_cart_email_validation'] = array('#type' => 'checkbox', '#title' => t('Require e-mail confirmation for anonymous customers.'), '#default_value' => $cart_config->get('email_validation'), '#states' => $anon_state);
     $form['anonymous']['uc_cart_new_account_name'] = array('#type' => 'checkbox', '#title' => t('Allow new customers to specify a username.'), '#default_value' => $cart_config->get('new_account_name'), '#states' => $anon_state);
     $form['anonymous']['uc_cart_new_account_password'] = array('#type' => 'checkbox', '#title' => t('Allow new customers to specify a password.'), '#default_value' => $cart_config->get('new_account_password'), '#states' => $anon_state);
     $form['anonymous']['uc_new_customer_email'] = array('#type' => 'checkbox', '#title' => t('Send new customers a separate e-mail with their account details.'), '#default_value' => $cart_config->get('new_customer_email'), '#states' => $anon_state);
     $form['anonymous']['uc_new_customer_login'] = array('#type' => 'checkbox', '#title' => t('Log in new customers after checkout.'), '#default_value' => $cart_config->get('new_customer_login'), '#states' => $anon_state);
     $form['anonymous']['uc_new_customer_status_active'] = array('#type' => 'checkbox', '#title' => t('Set new customer accounts to active.'), '#description' => t('Uncheck to create new accounts but make them blocked.'), '#default_value' => $cart_config->get('new_customer_status_active'), '#states' => $anon_state);
     $panes = $this->checkoutPaneManager->getPanes();
     $form['checkout']['panes'] = array('#type' => 'table', '#header' => array(t('Pane'), t('List position')), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'uc-checkout-pane-weight')));
     $form['checkout']['pane_settings']['#tree'] = TRUE;
     foreach ($panes as $id => $pane) {
         $form['checkout']['panes'][$id]['#attributes']['class'][] = 'draggable';
         $form['checkout']['panes'][$id]['status'] = array('#type' => 'checkbox', '#title' => SafeMarkup::checkPlain($pane->getTitle()), '#default_value' => $pane->isEnabled());
         $form['checkout']['panes'][$id]['weight'] = array('#type' => 'weight', '#title' => t('Weight for @title', array('@title' => $pane->getTitle())), '#title_display' => 'invisible', '#default_value' => $pane->getWeight(), '#attributes' => array('class' => array('uc-checkout-pane-weight')));
         $form['checkout']['panes'][$id]['#weight'] = $pane->getWeight();
         // @todo Move settingsForm to an interface.
         $pane_settings = $pane->settingsForm();
         if (!empty($pane_settings)) {
             $form['checkout']['pane_settings'][$id] = $pane_settings + array('#type' => 'details', '#title' => t('@pane pane', array('@pane' => $pane->getTitle())), '#group' => 'checkout-settings', '#parents' => array('panes', $id, 'settings'));
         }
     }
     $form['checkout']['uc_cart_delivery_not_shippable'] = array('#type' => 'checkbox', '#title' => t('Hide delivery information when carts have no shippable items.'), '#default_value' => $cart_config->get('delivery_not_shippable'));
     $form['completion_messages'] = array('#type' => 'details', '#title' => t('Completion messages'), '#group' => 'checkout-settings');
     $form['completion_messages']['uc_msg_order_logged_in'] = array('#type' => 'textarea', '#title' => t('Logged in users'), '#description' => t('Message displayed upon checkout for a user who is logged in.'), '#default_value' => $messages->get('logged_in'), '#rows' => 3);
     $form['completion_messages']['uc_msg_order_existing_user'] = array('#type' => 'textarea', '#title' => t('Existing users'), '#description' => t("Message displayed upon checkout for a user who has an account but wasn't logged in."), '#default_value' => $messages->get('existing_user'), '#rows' => 3, '#states' => $anon_state);
     $form['completion_messages']['uc_msg_order_new_user'] = array('#type' => 'textarea', '#title' => t('New users'), '#description' => t("Message displayed upon checkout for a new user whose account was just created. You may use the special tokens !new_username for the username of a newly created account and !new_password for that account's password."), '#default_value' => $messages->get('new_user'), '#rows' => 3, '#states' => $anon_state);
     $form['completion_messages']['uc_msg_order_new_user_logged_in'] = array('#type' => 'textarea', '#title' => t('New logged in users'), '#description' => t('Message displayed upon checkout for a new user whose account was just created and also <em>"Login users when new customer accounts are created at checkout."</em> is set on the <a href=":url">checkout settings</a>.', array(':url' => Url::fromRoute('uc_cart.checkout_settings')->toString())), '#default_value' => $messages->get('new_user_logged_in'), '#rows' => 3, '#states' => $anon_state);
     if (\Drupal::moduleHandler()->moduleExists('token')) {
         $form['completion_messages']['token_tree'] = array('#markup' => theme('token_tree', array('token_types' => array('uc_order', 'site', 'store'))));
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 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'] = array('#tree' => TRUE);
     $filter = array('enabled' => FALSE);
     // If the order isn't shippable, remove panes with shippable == TRUE.
     if (!$order->isShippable() && $this->config('uc_cart.settings')->get('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);
             $processed = FALSE;
             // Make sure we save the updated order.
         }
     }
     // 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] += array('#type' => 'details', '#title' => SafeMarkup::checkPlain($pane->getTitle()), '#id' => $id . '-pane', '#open' => TRUE);
     }
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['cancel'] = array('#type' => 'submit', '#value' => $this->t('Cancel'), '#validate' => array(), '#limit_validation_errors' => array(), '#submit' => array(array($this, 'cancel')));
     $form['actions']['continue'] = array('#type' => 'submit', '#value' => $this->t('Review order'), '#button_type' => 'primary');
     $form_state->loadInclude('uc_store', 'inc', 'includes/uc_ajax_attach');
     $form['#process'][] = 'uc_ajax_process_form';
     $this->session->remove('uc_checkout_review_' . $order->id());
     $this->session->remove('uc_checkout_complete_' . $order->id());
     return $form;
 }