public function NewPackageForm::submitForm in Ubercart 8.4
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- shipping/
uc_fulfillment/ src/ Form/ NewPackageForm.php, line 155
Class
- NewPackageForm
- Puts ordered products into a package.
Namespace
Drupal\uc_fulfillment\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this
->t('Cancel') != (string) $form_state
->getValue('op')) {
// Package 0 is a temporary array, all other elements are Package objects.
$packages = [
0 => [],
];
foreach ($form_state
->getValue('shipping_types') as $shipping_type => $products) {
foreach ($products['table'] as $id => $product) {
if ($product['checked']) {
if ($this
->t('Create one package') == (string) $form_state
->getValue('op')) {
$product['package'] = 1;
}
if ($product['package'] != 0) {
if (empty($packages[$product['package']])) {
// Create an empty package.
$packages[$product['package']] = Package::create();
}
$packages[$product['package']]
->addProducts([
$id => (object) $product,
]);
if (!$packages[$product['package']]
->getShippingType()) {
$packages[$product['package']]
->setShippingType($shipping_type);
}
}
else {
$packages[0][$shipping_type][$id] = (object) $product;
}
}
}
if (isset($packages[0][$shipping_type])) {
// We reach here if some packages were checked and marked "Separate".
// That can only happen when "Make packages" button was pushed.
foreach ($packages[0][$shipping_type] as $id => $product) {
$qty = $product->qty;
$product->qty = 1;
// Create a package for each product.
for ($i = 0; $i < $qty; $i++) {
$packages[] = Package::create([
'products' => [
$id => $product,
],
'shipping_type' => $shipping_type,
]);
}
}
}
// "Separate" packaging is now finished.
unset($packages[0][$shipping_type]);
}
if (empty($packages[0])) {
// This should always be true?
unset($packages[0]);
}
foreach ($packages as $package) {
$package
->setOrderId($form_state
->getValue('order_id'));
$package
->save();
}
$form_state
->setRedirect('uc_fulfillment.packages', [
'uc_order' => $form_state
->getValue('order_id'),
]);
}
else {
// Fall through, if user chose "Cancel".
$form_state
->setRedirect('entity.uc_order.canonical', [
'uc_order' => $form_state
->getValue('order_id'),
]);
}
}