class NewPackageForm in Ubercart 8.4
Puts ordered products into a package.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\uc_fulfillment\Form\NewPackageForm
Expanded class hierarchy of NewPackageForm
1 string reference to 'NewPackageForm'
- uc_fulfillment.routing.yml in shipping/
uc_fulfillment/ uc_fulfillment.routing.yml - shipping/uc_fulfillment/uc_fulfillment.routing.yml
File
- shipping/
uc_fulfillment/ src/ Form/ NewPackageForm.php, line 14
Namespace
Drupal\uc_fulfillment\FormView source
class NewPackageForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'uc_fulfillment_new_package';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this
->t('Cancel') != (string) $form_state
->getValue('op')) {
// See if any products have been checked.
foreach ($form_state
->getValue('shipping_types') as $shipping_type => $products) {
foreach ($products['table'] as $id => $product) {
if ($product['checked']) {
// At least one has been checked, that's all we need.
return;
}
}
}
// If nothing is checked, set error.
$form_state
->setErrorByName($shipping_type, $this
->t('Packages must contain at least one product.'));
}
}
/**
* {@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 = [
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'),
]);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
NewPackageForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
NewPackageForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
NewPackageForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
NewPackageForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |