public function NewPackageForm::buildForm in Ubercart 8.4
Form constructor.
Parameters
array $form: An associative 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 FormInterface::buildForm
File
- shipping/
uc_fulfillment/ src/ Form/ NewPackageForm.php, line 26
Class
- NewPackageForm
- Puts ordered products into a package.
Namespace
Drupal\uc_fulfillment\FormCode
public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL) {
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'uc_fulfillment/uc_fulfillment.scripts';
$shipping_types_products = [];
foreach ($uc_order->products as $product) {
if (uc_order_product_is_shippable($product)) {
$shipping_type = uc_product_get_shipping_type($product);
$shipping_types_products[$shipping_type][] = $product;
}
}
$quote_config = $this
->config('uc_quote.settings');
$shipping_type_weights = $quote_config
->get('type_weight');
$result = \Drupal::database()
->query('SELECT op.order_product_id, SUM(pp.qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE p.order_id = :id GROUP BY op.order_product_id', [
':id' => $uc_order
->id(),
]);
$packaged_products = $result
->fetchAllKeyed();
$form['shipping_types'] = [];
$header = [
// Fake out tableselect JavaScript into operating on our table.
[
'data' => '',
'class' => [
'select-all',
],
],
'model' => $this
->t('SKU'),
'name' => $this
->t('Title'),
'qty' => $this
->t('Quantity'),
'package' => $this
->t('Package'),
];
$shipping_type_options = uc_quote_shipping_type_options();
foreach ($shipping_types_products as $shipping_type => $products) {
$form['shipping_types'][$shipping_type] = [
'#type' => 'fieldset',
'#title' => isset($shipping_type_options[$shipping_type]) ? $shipping_type_options[$shipping_type] : Unicode::ucwords(str_replace('_', ' ', $shipping_type)),
'#weight' => isset($shipping_type_weights[$shipping_type]) ? $shipping_type_weights[$shipping_type] : 0,
];
$form['shipping_types'][$shipping_type]['table'] = [
'#type' => 'table',
'#header' => $header,
'#empty' => $this
->t('There are no products available for this type of package.'),
];
foreach ($products as $product) {
$unboxed_qty = $product->qty->value;
if (isset($packaged_products[$product->order_product_id->value])) {
$unboxed_qty -= $packaged_products[$product->order_product_id->value];
}
if ($unboxed_qty > 0) {
$row = [];
$row['checked'] = [
'#type' => 'checkbox',
'#default_value' => 0,
];
$row['model'] = [
'#plain_text' => $product->model->value,
];
$row['name'] = [
'#markup' => $product->title->value,
];
$range = range(1, $unboxed_qty);
$row['qty'] = [
'#type' => 'select',
'#title' => $this
->t('Quantity'),
'#title_display' => 'invisible',
'#options' => array_combine($range, $range),
'#default_value' => $unboxed_qty,
];
$range = range(0, count($uc_order->products));
$options = array_combine($range, $range);
$options[0] = $this
->t('Sep.');
$row['package'] = [
'#type' => 'select',
'#title' => $this
->t('Package'),
'#title_display' => 'invisible',
'#options' => $options,
'#default_value' => 0,
];
$form['shipping_types'][$shipping_type]['table'][$product->order_product_id->value] = $row;
}
}
}
$form['order_id'] = [
'#type' => 'hidden',
'#value' => $uc_order
->id(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['create'] = [
'#type' => 'submit',
'#value' => $this
->t('Make packages'),
];
$form['actions']['combine'] = [
'#type' => 'submit',
'#value' => $this
->t('Create one package'),
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
];
return $form;
}