You are here

public function PackageEditForm::buildForm in Ubercart 8.4

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

shipping/uc_fulfillment/src/Form/PackageEditForm.php, line 33

Class

PackageEditForm
Rearranges the products in or out of a package.

Namespace

Drupal\uc_fulfillment\Form

Code

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;
}