You are here

public function PackageCancelForm::submitForm in Ubercart 8.4

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

shipping/uc_fulfillment/src/Form/PackageCancelForm.php, line 114

Class

PackageCancelForm
Confirms cancellation of a package's shipment.

Namespace

Drupal\uc_fulfillment\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $shipment = Shipment::load($this->package
    ->getSid());
  $methods = $this->moduleHandler
    ->invokeAll('uc_fulfillment_method');

  // The notion of "cancel" is specific to the fulfillment method, therefore
  // we delegate all the work to the plugin.
  // @todo Replace this with calls to the plugin cancel method instead of hooks.
  if (isset($methods[$shipment
    ->getShippingMethod()]['cancel']) && function_exists($methods[$shipment
    ->getShippingMethod()]['cancel'])) {
    $result = call_user_func($methods[$shipment
      ->getShippingMethod()]['cancel'], $shipment
      ->getTrackingNumber(), [
      $this->package
        ->getTrackingNumber(),
    ]);
    if ($result) {
      \Drupal::database()
        ->update('uc_packages')
        ->fields([
        'sid' => NULL,
        'label_image' => NULL,
        'tracking_number' => NULL,
      ])
        ->condition('package_id', $this->package
        ->id())
        ->execute();
      if ($this->package
        ->getLabelImage()) {
        file_usage_delete($this->package
          ->getLabelImage(), 'uc_fulfillment', 'package', $this->package
          ->id());
        file_delete($this->package
          ->getLabelImage());
        $this->package
          ->setLabelImage('');
      }
      unset($shipment->packages[$this->package
        ->id()]);
      if (!count($shipment
        ->getPackages())) {
        $shipment
          ->delete();
      }
    }
  }
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}