class PackageEditForm in Ubercart 8.4
Rearranges the products in or out of a package.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\uc_fulfillment\Form\PackageEditForm
Expanded class hierarchy of PackageEditForm
1 string reference to 'PackageEditForm'
- 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/ PackageEditForm.php, line 14
Namespace
Drupal\uc_fulfillment\FormView source
class PackageEditForm extends FormBase {
/**
* The package.
*
* @var \Drupal\uc_fulfillment\PackageInterface
*/
protected $package;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'uc_fulfillment_package_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, PackageInterface $uc_package = NULL) {
$this->package = $uc_package;
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'uc_fulfillment/uc_fulfillment.scripts';
$products = [];
$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->order_product_id->value] = $product;
$products[$product->order_product_id->value] = $product;
}
}
$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'),
];
$result = \Drupal::database()
->query('SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id WHERE p.order_id = :id GROUP BY order_product_id', [
':id' => $uc_order
->id(),
]);
foreach ($result as $packaged_product) {
// Make already packaged products unavailable,
// except those in this package.
$products[$packaged_product->order_product_id]->qty->value -= $packaged_product->quantity;
if (isset($this->package
->getProducts()[$packaged_product->order_product_id])) {
$products[$packaged_product->order_product_id]->qty->value += $this->package
->getProducts()[$packaged_product->order_product_id]->qty;
}
}
$form['products'] = [
'#type' => 'table',
'#header' => $header,
'#empty' => $this
->t('There are no products available for this type of package.'),
];
foreach ($products as $product) {
if ($product->qty->value > 0) {
$row = [];
$row['checked'] = [
'#type' => 'checkbox',
'#default_value' => isset($this->package
->getProducts()[$product->order_product_id->value]),
];
$row['model'] = [
'#markup' => $product->model->value,
];
$row['name'] = [
'#markup' => $product->title->value,
];
$range = range(1, $product->qty->value);
$row['qty'] = [
'#type' => 'select',
'#options' => array_combine($range, $range),
'#default_value' => isset($this->package
->getProducts()[$product->order_product_id->value]) ? $this->package
->getProducts()[$product->order_product_id->value]->qty : 1,
];
$form['products'][$product->order_product_id->value] = $row;
}
}
$options = [];
$shipping_type_options = uc_quote_shipping_type_options();
foreach (array_keys($shipping_types_products) as $type) {
$options[$type] = isset($shipping_type_options[$type]) ? $shipping_type_options[$type] : Unicode::ucwords(str_replace('_', ' ', $type));
}
$form['shipping_type'] = [
'#type' => 'select',
'#title' => $this
->t('Shipping type'),
'#options' => $options,
'#default_value' => $this->package
->getShippingType() ? $this->package
->getShippingType() : 'small_package',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$products = [];
foreach ($form_state
->getValue('products') as $id => $product) {
if ($product['checked']) {
$products[$id] = (object) $product;
}
}
$this->package
->setProducts($products);
$this->package
->setShippingType($form_state
->getValue('shipping_type'));
$this->package
->save();
$form_state
->setRedirect('uc_fulfillment.packages', [
'uc_order' => $this->package
->getOrderId(),
]);
}
}
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
PackageEditForm:: |
protected | property | The package. | |
PackageEditForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
PackageEditForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PackageEditForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
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. |