class ShipmentEditForm in Ubercart 8.4
Creates or edits a shipment.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\uc_fulfillment\Form\ShipmentEditForm
Expanded class hierarchy of ShipmentEditForm
1 string reference to 'ShipmentEditForm'
- 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/ ShipmentEditForm.php, line 18
Namespace
Drupal\uc_fulfillment\FormView source
class ShipmentEditForm extends FormBase {
/**
* The order id.
*
* @var \Drupal\uc_order\OrderInterface
*/
protected $order_id;
/**
* The shipment.
*
* @var \Drupal\uc_fulfillment\Shipment
*/
protected $shipment;
/**
* The datetime.time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Form constructor.
*
* @param \Drupal\Component\Datetime\TimeInterface $time
* The datetime.time service.
*/
public function __construct(TimeInterface $time) {
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('datetime.time'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'uc_fulfillment_shipment_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, ShipmentInterface $uc_shipment = NULL) {
$this->order_id = $uc_order
->id();
$this->shipment = $uc_shipment;
$addresses = [];
$form['packages'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Packages'),
'#tree' => TRUE,
];
if (NULL != $this->shipment
->getOrigin()) {
$addresses[] = $this->shipment
->getOrigin();
}
foreach ($this->shipment
->getPackages() as $id => $package) {
foreach ($package
->getAddresses() as $address) {
if (!in_array($address, $addresses)) {
$addresses[] = $address;
}
}
// Create list of products and get a representative product (last one in
// the loop) to use for some default values.
$product_list = [];
$declared_value = 0;
$products_weight = 0;
foreach ($package
->getProducts() as $product) {
$product_list[] = $product->qty . ' x ' . $product->model;
$declared_value += $product->qty * $product->price;
$products_weight += $product->qty * $product->weight * uc_weight_conversion($product->weight_units, $package
->getWeightUnits());
}
$pkg_form = [
'#type' => 'fieldset',
'#title' => $this
->t('Package @id', [
'@id' => $id,
]),
];
$pkg_form['products'] = [
'#theme' => 'item_list',
'#items' => $product_list,
];
$pkg_form['pkg_type'] = [
'#type' => 'textfield',
'#title' => $this
->t('Package type'),
'#default_value' => $package
->getPackageType(),
'#description' => $this
->t('For example: Box, pallet, tube, envelope, etc.'),
];
if (isset($method) && is_array($method['ship']['pkg_types'])) {
$pkg_form['pkg_type']['#type'] = 'select';
$pkg_form['pkg_type']['#options'] = $method['ship']['pkg_types'];
$pkg_form['pkg_type']['#description'] = '';
}
$pkg_form['declared_value'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Declared value'),
'#default_value' => !empty($package
->getValue()) ? $package
->getValue() : $declared_value,
];
$pkg_form['weight'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'uc-inline-form',
'clearfix',
],
],
'#description' => $this
->t('Weight of the package. Default value is sum of product weights in the package.'),
'#weight' => 15,
];
$pkg_form['weight']['weight'] = [
'#type' => 'number',
'#title' => $this
->t('Weight'),
'#min' => 0,
'#step' => 'any',
'#default_value' => !empty($package
->getWeight()) ? $package
->getWeight() : $products_weight,
'#size' => 10,
];
$pkg_form['weight']['units'] = [
'#type' => 'select',
'#title' => $this
->t('Units'),
'#options' => [
'lb' => $this
->t('Pounds'),
'kg' => $this
->t('Kilograms'),
'oz' => $this
->t('Ounces'),
'g' => $this
->t('Grams'),
],
'#default_value' => $package
->getWeightUnits(),
];
$pkg_form['dimensions'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'uc-inline-form',
'clearfix',
],
],
'#title' => $this
->t('Dimensions'),
'#description' => $this
->t('Physical dimensions of the packaged product.'),
'#weight' => 20,
];
$pkg_form['dimensions']['length'] = [
'#type' => 'number',
'#title' => $this
->t('Length'),
'#min' => 0,
'#step' => 'any',
'#default_value' => $package
->getLength(),
'#size' => 8,
];
$pkg_form['dimensions']['width'] = [
'#type' => 'number',
'#title' => $this
->t('Width'),
'#min' => 0,
'#step' => 'any',
'#default_value' => $package
->getWidth(),
'#size' => 8,
];
$pkg_form['dimensions']['height'] = [
'#type' => 'number',
'#title' => $this
->t('Height'),
'#min' => 0,
'#step' => 'any',
'#default_value' => $package
->getHeight(),
'#size' => 8,
];
$pkg_form['dimensions']['units'] = [
'#type' => 'select',
'#title' => $this
->t('Units of measurement'),
'#options' => [
'in' => $this
->t('Inches'),
'ft' => $this
->t('Feet'),
'cm' => $this
->t('Centimeters'),
'mm' => $this
->t('Millimeters'),
],
'#default_value' => $package
->getLengthUnits(),
];
$pkg_form['tracking_number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Tracking number'),
'#default_value' => $package
->getTrackingNumber(),
];
$form['packages'][$id] = $pkg_form;
}
// If the destination address has been edited, copy it to the
// order's delivery address.
if ($this->shipment
->getDestination()) {
$uc_order
->setAddress('delivery', $this->shipment
->getDestination());
}
$form += \Drupal::formBuilder()
->getForm('\\Drupal\\uc_fulfillment\\Form\\AddressForm', $addresses, $uc_order);
$form['shipment'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Shipment data'),
];
// Determine shipping option chosen by the customer.
$message = '';
if (isset($uc_order->quote['method'])) {
// Order has a quote attached.
$method = $uc_order->quote['method'];
$methods = \Drupal::moduleHandler()
->invokeAll('uc_fulfillment_method');
if (isset($methods[$method])) {
// Quote is from a currently-active shipping method.
$services = $methods[$method]['quote']['accessorials'];
$method = $services[$uc_order->quote['accessorials']];
}
$message = $this
->t('Customer selected "@method" as the shipping method and paid @rate', [
'@method' => $method,
'@rate' => uc_currency_format($uc_order->quote['rate']),
]);
}
else {
// No quotes for this order.
$message = $this
->t('There are no shipping quotes attached to this order. Customer was not charged for shipping.');
}
// Inform administrator of customer's shipping choice.
$form['shipment']['shipping_choice'] = [
'#type' => 'container',
'#markup' => $message,
];
$form['shipment']['shipping_method'] = [
'#type' => 'hidden',
'#value' => $this->shipment
->getShippingMethod(),
];
$form['shipment']['carrier'] = [
'#type' => 'textfield',
'#title' => $this
->t('Carrier'),
'#default_value' => $this->shipment
->getCarrier(),
];
$form['shipment']['accessorials'] = [
'#type' => 'textfield',
'#title' => $this
->t('Shipment options'),
'#default_value' => $this->shipment
->getAccessorials(),
'#description' => $this
->t('Short notes about the shipment, e.g. residential, overnight, etc.'),
];
$form['shipment']['transaction_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Transaction ID'),
'#default_value' => $this->shipment
->getTransactionId(),
];
$form['shipment']['tracking_number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Tracking number'),
'#default_value' => $this->shipment
->getTrackingNumber(),
];
$ship_date = $this->time
->getRequestTime();
if ($this->shipment
->getShipDate()) {
$ship_date = $this->shipment
->getShipDate();
}
$exp_delivery = $this->time
->getRequestTime();
if ($this->shipment
->getExpectedDelivery()) {
$exp_delivery = $this->shipment
->getExpectedDelivery();
}
$form['shipment']['ship_date'] = [
'#type' => 'datetime',
'#title' => $this
->t('Ship date'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#default_value' => DrupalDateTime::createFromTimestamp($ship_date),
];
$form['shipment']['expected_delivery'] = [
'#type' => 'datetime',
'#title' => $this
->t('Expected delivery'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#default_value' => DrupalDateTime::createFromTimestamp($exp_delivery),
];
$form['shipment']['cost'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Shipping cost'),
'#default_value' => $this->shipment
->getCost(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save shipment'),
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->shipment
->setOrderId($this->order_id);
// The pickup_address and delivery_address form elements are defined in AddressForm.
$this->shipment
->setOrigin(Address::create($form_state
->getValue('pickup_address')));
$this->shipment
->setDestination(Address::create($form_state
->getValue('delivery_address')));
$packages = [];
foreach ($form_state
->getValue('packages') as $id => $pkg_form) {
$package = Package::load($id);
$package
->setPackageType($pkg_form['pkg_type']);
$package
->setValue($pkg_form['declared_value']);
$package
->setTrackingNumber($pkg_form['tracking_number']);
$package
->setWeight($pkg_form['weight']['weight']);
$package
->setWeightUnits($pkg_form['weight']['units']);
$package
->setLength($pkg_form['dimensions']['length']);
$package
->setWidth($pkg_form['dimensions']['width']);
$package
->setHeight($pkg_form['dimensions']['height']);
$package
->setLengthUnits($pkg_form['dimensions']['units']);
$packages[$id] = $package;
}
$this->shipment
->setPackages($packages);
$this->shipment
->setCarrier($form_state
->getValue('carrier'));
$this->shipment
->setShippingMethod($form_state
->getValue('shipping_method'));
$this->shipment
->setAccessorials($form_state
->getValue('accessorials'));
$this->shipment
->setTransactionId($form_state
->getValue('transaction_id'));
$this->shipment
->setTrackingNumber($form_state
->getValue('tracking_number'));
$this->shipment
->setShipDate($form_state
->getValue('ship_date')
->getTimestamp());
$this->shipment
->setExpectedDelivery($form_state
->getValue('expected_delivery')
->getTimestamp());
$this->shipment
->setCost($form_state
->getValue('cost'));
$this->shipment
->save();
$form_state
->setRedirect('uc_fulfillment.shipments', [
'uc_order' => $this->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:: |
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. | |
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. | |
ShipmentEditForm:: |
protected | property | The order id. | |
ShipmentEditForm:: |
protected | property | The shipment. | |
ShipmentEditForm:: |
protected | property | The datetime.time service. | |
ShipmentEditForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ShipmentEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ShipmentEditForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ShipmentEditForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ShipmentEditForm:: |
public | function | Form constructor. | |
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. |