/** * {@inheritdoc} */ 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 = array(0 => array()); 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']])) { $packages[$product['package']] = Package::create(); } $packages[$product['package']]->products[$id] = (object) $product; if (!isset($packages[$product['package']]->shipping_type)) { $packages[$product['package']]->shipping_type = $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->order_id = $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')]); } }