View source
<?php
namespace Drupal\uc_quote\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class QuoteSettingsForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'));
}
public function getFormId() {
return 'uc_quote_admin_settings';
}
protected function getEditableConfigNames() {
return [
'uc_quote.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$quote_config = $this
->config('uc_quote.settings');
$form['display_debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display debug information to administrators.'),
'#default_value' => $quote_config
->get('display_debug'),
];
$form['require_quote'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Prevent the customer from completing an order if a shipping quote is not selected.'),
'#default_value' => $quote_config
->get('require_quote'),
];
$form['default_address'] = [
'#type' => 'details',
'#title' => $this
->t('Default pickup address'),
'#description' => $this
->t("When delivering products to customers, the original location of the product must be known in order to accurately quote the shipping cost and set up a delivery. This form provides the default location for all products in the store. If a product's individual pickup address is blank, Ubercart uses the store's default pickup address specified here."),
];
$form['default_address']['address'] = [
'#type' => 'uc_address',
'#hide' => [
'first_name',
'last_name',
'company',
'email',
],
'#default_value' => $quote_config
->get('ship_from_address'),
'#required' => FALSE,
];
$shipping_types = uc_quote_shipping_type_options();
if (is_array($shipping_types)) {
$form['type_weight'] = [
'#type' => 'details',
'#title' => $this
->t('List position'),
'#description' => $this
->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 = $this->moduleHandler
->invokeAll('uc_shipping_method');
$method_types = [];
foreach ($shipping_methods as $method) {
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['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.', [
'%list' => implode(', ', $method_types['order']),
]);
}
foreach ($shipping_types as $id => $title) {
$form['type_weight'][$id] = [
'#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'] = [
'#type' => 'select',
'#title' => $this
->t('Default order fulfillment type for products'),
'#options' => $shipping_types,
'#default_value' => $quote_config
->get('shipping_type'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$quote_config = $this
->config('uc_quote.settings');
$quote_config
->set('ship_from_address', $form_state
->getValue('address'))
->set('display_debug', $form_state
->getValue('display_debug'))
->set('require_quote', $form_state
->getValue('require_quote'))
->set('type_weight', $form_state
->getValue('type_weight'))
->set('shipping_type', $form_state
->getValue('uc_store_shipping_type'))
->save();
parent::submitForm($form, $form_state);
}
}