/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $quote_config = $this->config('uc_quote.settings'); $form['methods'] = array('#type' => 'table', '#header' => array(t('Shipping method'), t('Details'), t('List position'), t('Operations')), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'uc-quote-method-weight')), '#empty' => t('No shipping quotes have been configured yet.')); foreach (uc_quote_methods(TRUE) as $method) { if (isset($method['quote'])) { $id = $method['id']; // Build a list of operations links. $operations = isset($method['operations']) ? $method['operations'] : array(); // $operations += array('conditions' => array( // 'title' => t('conditions'), // 'url' => Url::fromRoute('admin/store/config/quotes/manage/get_quote_from_', ['id' => $id]), // 'weight' => 5, // )); // Ensure "delete" comes towards the end of the list. if (isset($operations['delete'])) { $operations['delete']['weight'] = 10; } uasort($operations, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement'); $form['methods'][$id]['status'] = array('#type' => 'checkbox', '#title' => SafeMarkup::checkPlain($method['title']), '#default_value' => $method['enabled']); $form['methods'][$id]['description'] = array('#markup' => isset($method['description']) ? $method['description'] : ''); $form['methods'][$id]['weight'] = array('#type' => 'weight', '#default_value' => $method['weight'], '#attributes' => array('class' => array('uc-quote-method-weight'))); $form['methods'][$id]['operations'] = array('#type' => 'operations', '#links' => $operations); } } $shipping_types = uc_quote_shipping_type_options(); if (is_array($shipping_types)) { $form['uc_quote_type_weight'] = array('#type' => 'details', '#title' => t('List position'), '#description' => t('Determines which shipping methods are quoted at checkout when products of different shipping types are ordered. Larger values take precedence.'), '#tree' => TRUE); $weight = $quote_config->get('type_weight'); $shipping_methods = \Drupal::moduleHandler()->invokeAll('uc_shipping_method'); $method_types = array(); foreach ($shipping_methods as $method) { // Get shipping method types from shipping methods that provide quotes if (isset($method['quote'])) { $method_types[$method['quote']['type']][] = $method['title']; } } if (isset($method_types['order']) && is_array($method_types['order'])) { $count = count($method_types['order']); $form['uc_quote_type_weight']['#description'] .= $this->formatPlural($count, '<br />The %list method is compatible with any shipping type.', '<br />The %list methods are compatible with any shipping type.', array('%list' => implode(', ', $method_types['order']))); } foreach ($shipping_types as $id => $title) { $form['uc_quote_type_weight'][$id] = array('#type' => 'weight', '#title' => $title . (isset($method_types[$id]) && is_array($method_types[$id]) ? ' (' . implode(', ', $method_types[$id]) . ')' : ''), '#delta' => 5, '#default_value' => isset($weight[$id]) ? $weight[$id] : 0); } } $form['uc_store_shipping_type'] = array('#type' => 'select', '#title' => t('Default order fulfillment type for products'), '#options' => $shipping_types, '#default_value' => $quote_config->get('shipping_type')); return parent::buildForm($form, $form_state); }
/** * {@inheritdoc} */ public function prepare(OrderInterface $order, array $form, FormStateInterface $form_state) { // If a quote was explicitly selected, add it to the order. if (isset($form['panes']['quotes']['quotes']['quote_option']['#value']) && isset($form['panes']['quotes']['quotes']['quote_option']['#default_value']) && $form['panes']['quotes']['quotes']['quote_option']['#value'] !== $form['panes']['quotes']['quotes']['quote_option']['#default_value']) { $quote_option = explode('---', $form_state->getValue(['panes', 'quotes', 'quotes', 'quote_option'])); $order->quote['method'] = $quote_option[0]; $order->quote['accessorials'] = $quote_option[1]; $order->data->uc_quote_selected = TRUE; } // If the current quote was never explicitly selected, discard it and // use the default. if (empty($order->data->uc_quote_selected)) { unset($order->quote); } // Ensure that the form builder uses the default value to decide which // radio button should be selected. $input = $form_state->getUserInput(); unset($input['panes']['quotes']['quotes']['quote_option']); $form_state->setUserInput($input); $order->quote_form = uc_quote_build_quote_form($order, !$form_state->get('quote_requested')); $default_option = _uc_quote_extract_default_option($order->quote_form); if ($default_option) { $order->quote['rate'] = $order->quote_form[$default_option]['rate']['#value']; $quote_option = explode('---', $default_option); $order->quote['method'] = $quote_option[0]; $order->quote['accessorials'] = $quote_option[1]; $methods = uc_quote_methods(); $method = $methods[$quote_option[0]]; $label = $method['quote']['accessorials'][$quote_option[1]]; $result = db_query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = :id AND type = :type", [':id' => $order->id(), ':type' => 'shipping']); if ($lid = $result->fetchField()) { uc_order_update_line_item($lid, $label, $order->quote['rate']); } else { uc_order_line_item_add($order->id(), 'shipping', $label, $order->quote['rate']); } } else { unset($order->quote); } }