You are here

public function Quotes::buildForm in Ubercart 8.4

Form constructor.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being viewed.

array $form: An 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 EditableOrderPanePluginInterface::buildForm

File

shipping/uc_quote/src/Plugin/Ubercart/OrderPane/Quotes.php, line 39

Class

Quotes
Get a shipping quote for the order from a quoting module.

Namespace

Drupal\uc_quote\Plugin\Ubercart\OrderPane

Code

public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) {
  $form['quote_button'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Get shipping quotes'),
    '#submit' => [
      [
        $this,
        'retrieveQuotes',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'replaceOrderQuotes',
      ],
      'wrapper' => 'quote',
      'effect' => 'slide',
      'progress' => [
        'type' => 'bar',
        'message' => $this
          ->t('Receiving quotes...'),
      ],
    ],
  ];
  $form['quotes'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'quote',
    ],
    '#tree' => TRUE,
  ];
  if ($form_state
    ->get('quote_requested')) {

    // Rebuild form products, from uc_order_edit_form_submit()
    foreach ($form_state
      ->getValue('products') as $product) {
      if (!isset($product['remove']) && intval($product['qty']) > 0) {
        foreach ([
          'qty',
          'title',
          'model',
          'weight',
          'weight_units',
          'cost',
          'price',
        ] as $field) {
          $order->products[$product['order_product_id']]->{$field} = $product[$field];
        }
      }
    }
    $form['quotes'] += uc_quote_build_quote_form($order);
    $form['quotes']['add_quote'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Apply to order'),
      '#submit' => [
        [
          $this,
          'applyQuote',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'updateOrderRates',
        ],
        'effect' => 'fade',
        'progress' => [
          'type' => 'throbber',
          'message' => $this
            ->t('Applying quotes...'),
        ],
      ],
    ];
  }
  $form_state
    ->set([
    'uc_ajax',
    'uc_quote',
    'delivery][delivery_country',
  ], [
    'quote' => [
      $this,
      'replaceOrderQuotes',
    ],
  ]);
  return $form;
}