ShipmentDeleteForm.php in Ubercart 8.4
File
shipping/uc_fulfillment/src/Form/ShipmentDeleteForm.php
View source
<?php
namespace Drupal\uc_fulfillment\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\uc_fulfillment\ShipmentInterface;
use Drupal\uc_order\OrderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ShipmentDeleteForm extends ConfirmFormBase {
protected $order_id;
protected $shipment;
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'));
}
public function getFormId() {
return 'uc_fulfillment_shipment_delete_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this shipment?');
}
public function getDescription() {
return $this
->t('The shipment will be canceled and the packages it contains will be available for reshipment.');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function getCancelUrl() {
return Url::fromRoute('uc_fulfillment.shipments', [
'uc_order' => $this->order_id,
]);
}
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;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$methods = $this->moduleHandler
->invokeAll('uc_fulfillment_method');
if ($this->shipment
->getTrackingNumber() && isset($methods[$this->shipment
->getShippingMethod()]['cancel']) && function_exists($methods[$this->shipment
->getShippingMethod()]['cancel'])) {
$result = call_user_func($methods[$this->shipment
->getShippingMethod()]['cancel'], $this->shipment
->getTrackingNumber());
if ($result) {
$this->shipment
->delete();
}
else {
$this
->messenger()
->addWarning($this
->t('The shipment %tracking could not be canceled with %carrier. To delete it anyway, remove the tracking number and try again.', [
'%tracking' => $this->shipment
->getTrackingNumber(),
'%carrier' => $this->shipment
->getCarrier(),
]));
}
}
else {
$this->shipment
->delete();
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}