You are here

public function StockEditForm::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

uc_stock/src/Form/StockEditForm.php, line 25

Class

StockEditForm
Defines the stock edit form.

Namespace

Drupal\uc_stock\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
  $form['#title'] = $this
    ->t('<em>Edit @type stock</em> @title', [
    '@type' => node_get_type_label($node),
    '@title' => $node
      ->label(),
  ]);
  $form['stock'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => ' ' . $this
          ->t('Active'),
        'class' => [
          'select-all',
          'nowrap',
        ],
      ],
      $this
        ->t('SKU'),
      $this
        ->t('Stock'),
      $this
        ->t('Threshold'),
    ],
  ];
  $form['#attached']['library'][] = 'core/drupal.tableselect';
  $skus = uc_product_get_models($node
    ->id(), FALSE);
  foreach ($skus as $sku) {
    $stock = \Drupal::database()
      ->query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", [
      ':sku' => $sku,
    ])
      ->fetchAssoc();
    $form['stock'][$sku]['active'] = [
      '#type' => 'checkbox',
      '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
    ];
    $form['stock'][$sku]['sku'] = [
      '#markup' => $sku,
    ];
    $form['stock'][$sku]['stock'] = [
      '#type' => 'textfield',
      '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0,
      '#maxlength' => 9,
      '#size' => 9,
    ];
    $form['stock'][$sku]['threshold'] = [
      '#type' => 'textfield',
      '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0,
      '#maxlength' => 9,
      '#size' => 9,
    ];
  }
  $form['nid'] = [
    '#type' => 'value',
    '#value' => $node
      ->id(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save changes'),
  ];
  return $form;
}