You are here

function uc_quote_build_quote_form in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 shipping/uc_quote/uc_quote.module \uc_quote_build_quote_form()

Calculates and returns the shipping quote selection form.

2 calls to uc_quote_build_quote_form()
QuotePane::prepare in shipping/uc_quote/src/Plugin/Ubercart/CheckoutPane/QuotePane.php
Prepares a pane for display.
Quotes::buildForm in shipping/uc_quote/src/Plugin/Ubercart/OrderPane/Quotes.php
Form constructor.

File

shipping/uc_quote/uc_quote.module, line 444
The controller module for fulfillment modules that process physical goods.

Code

function uc_quote_build_quote_form($order, $show_errors = TRUE) {
  $return = [];
  $quote_config = \Drupal::config('uc_quote.settings');
  \Drupal::moduleHandler()
    ->loadInclude('uc_quote', 'inc', 'uc_quote.pages');
  $quotes = uc_quote_assemble_quotes($order);
  $quote_options = [];
  if (!empty($quotes)) {
    foreach ($quotes as $method => $data) {
      foreach ($data as $accessorial => $quote) {
        $key = $method . '---' . $accessorial;
        if (isset($quote['rate'])) {
          $quote_options[$key] = t('@label: @price', [
            '@label' => $quote['option_label'],
            '@price' => $quote['format'],
          ]);
          $return[$key]['rate'] = [
            '#type' => 'hidden',
            '#value' => $quote['rate'],
          ];
        }
        if (!empty($quote['error'])) {
          $item_list = [
            '#theme' => 'item_list',
            '#items' => [
              'items' => $quote['error'],
            ],
          ];
          $return[$key]['error'] = [
            '#type' => 'container',
            '#markup' => drupal_render($item_list),
            '#attributes' => [
              'class' => [
                'quote-error',
              ],
            ],
          ];
        }
        if (!empty($quote['notes'])) {
          $return[$key]['notes'] = [
            '#type' => 'container',
            '#markup' => $quote['notes'],
            '#attributes' => [
              'class' => [
                'quote-notes',
              ],
            ],
          ];
        }
        if (!empty($quote['debug'])) {
          $return[$key]['debug'] = [
            '#markup' => '<pre>' . $quote['debug'] . '</pre>',
          ];
        }
        if (!isset($quote['rate']) && isset($quote['label']) && count($return[$key])) {
          $return[$key]['#prefix'] = $quote['label'] . ': ';
        }
      }
    }
  }
  $num_quotes = count($quote_options);
  $default = key($quote_options);
  if ($num_quotes > 1) {
    if (isset($order->quote['method']) && isset($order->quote['accessorials'])) {
      $chosen = $order->quote['method'] . '---' . $order->quote['accessorials'];
      if (isset($quote_options[$chosen])) {
        $default = $chosen;
      }
    }
    $return['quote_option'] = [
      '#type' => 'radios',
      '#options' => $quote_options,
      '#default_value' => $default,
    ];
  }
  elseif ($num_quotes == 1) {
    $return['quote_option'] = [
      '#type' => 'hidden',
      '#value' => $default,
      '#suffix' => $quote_options[$default],
    ];
  }
  elseif ($show_errors) {
    $return['error'] = [
      '#markup' => t('There were problems getting a shipping quote. Please verify the delivery address and try again.'),
    ];
  }
  $return['#theme'] = 'uc_quote_returned_rates';
  return $return;
}