You are here

protected function Products::editProductsForm in Ubercart 8.4

Form to allow ordered products' data to be changed.

1 call to Products::editProductsForm()
Products::buildForm in uc_order/src/Plugin/Ubercart/OrderPane/Products.php
Form constructor.

File

uc_order/src/Plugin/Ubercart/OrderPane/Products.php, line 337

Class

Products
Manage the products an order contains.

Namespace

Drupal\uc_order\Plugin\Ubercart\OrderPane

Code

protected function editProductsForm($form, FormStateInterface $form_state, $products) {
  $form['products'] = [
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => [
      $this
        ->t('Remove'),
      $this
        ->t('Quantity'),
      $this
        ->t('Name'),
      $this
        ->t('SKU'),
      $this
        ->t('Weight'),
      $this
        ->t('Units'),
      $this
        ->t('Cost'),
      $this
        ->t('Price'),
    ],
    '#attributes' => [
      'id' => 'order-edit-products',
      'class' => [
        'order-pane-table',
      ],
    ],
    '#empty' => $this
      ->t('This order contains no products.'),
  ];
  $form['data'] = [
    '#tree' => TRUE,
    '#parents' => [
      'products',
    ],
  ];
  foreach ($products as $i => $product) {
    $form['products'][$i]['remove'] = [
      '#type' => 'image_button',
      '#title' => $this
        ->t('Remove this product.'),
      '#name' => "products[{$i}][remove]",
      '#src' => drupal_get_path('module', 'uc_store') . '/images/error.gif',
      '#button_type' => 'remove',
      '#submit' => [
        [
          $this,
          'removeProduct',
        ],
        '::submitForm',
      ],
      '#return_value' => $product->order_product_id->value,
    ];
    $form['data'][$i]['order_product_id'] = [
      '#type' => 'hidden',
      '#value' => $product->order_product_id->value,
    ];
    $form['data'][$i]['nid'] = [
      '#type' => 'hidden',
      '#value' => $product->nid->target_id,
    ];
    $form['products'][$i]['qty'] = [
      '#type' => 'uc_quantity',
      '#title' => $this
        ->t('Quantity'),
      '#title_display' => 'invisible',
      '#default_value' => $product->qty->value,
    ];
    $form['products'][$i]['title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Title'),
      '#title_display' => 'invisible',
      '#default_value' => $product->title->value,
      '#size' => 32,
      '#maxlength' => 255,
    ];
    $form['products'][$i]['model'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('SKU'),
      '#title_display' => 'invisible',
      '#default_value' => $product->model->value,
      '#size' => 6,
    ];
    $form['products'][$i]['weight'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $product->weight->value,
      '#size' => 3,
    ];
    $units = [
      'lb' => $this
        ->t('Pounds'),
      'kg' => $this
        ->t('Kilograms'),
      'oz' => $this
        ->t('Ounces'),
      'g' => $this
        ->t('Grams'),
    ];
    $form['products'][$i]['weight_units'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Units'),
      '#title_display' => 'invisible',
      '#default_value' => $product->weight->units,
      '#options' => $units,
    ];
    $form['products'][$i]['cost'] = [
      '#type' => 'uc_price',
      '#title' => $this
        ->t('Cost'),
      '#title_display' => 'invisible',
      '#default_value' => $product->cost->value,
      '#size' => 5,
    ];
    $form['products'][$i]['price'] = [
      '#type' => 'uc_price',
      '#title' => $this
        ->t('Price'),
      '#title_display' => 'invisible',
      '#default_value' => $product->price->value,
      '#size' => 5,
    ];
    $form['data'][$i]['data'] = [
      '#type' => 'hidden',
      '#value' => serialize($product->data
        ->first() ? $product->data
        ->first()
        ->toArray() : NULL),
    ];
  }
  return $form;
}