You are here

public function OrderWorkflowForm::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 ConfigFormBase::buildForm

File

uc_order/src/Form/OrderWorkflowForm.php, line 33

Class

OrderWorkflowForm
Displays the order workflow form for order state and status customization.

Namespace

Drupal\uc_order\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $states = uc_order_state_options_list();
  $statuses = OrderStatus::loadMultiple();
  $form['order_states'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Order states'),
  ];
  $form['order_states']['order_states'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('State'),
      $this
        ->t('Default order status'),
    ],
  ];
  foreach ($states as $state_id => $title) {
    $form['order_states']['order_states'][$state_id]['title'] = [
      '#markup' => $title,
    ];

    // Create the select box for specifying a default status per order state.
    $options = [];
    foreach ($statuses as $status) {
      if ($state_id == $status
        ->getState()) {
        $options[$status
          ->id()] = $status
          ->getName();
      }
    }
    if (empty($options)) {
      $form['order_states']['order_states'][$state_id]['default'] = [
        '#markup' => $this
          ->t('- N/A -'),
      ];
    }
    else {
      $form['order_states']['order_states'][$state_id]['default'] = [
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => uc_order_state_default($state_id),
      ];
    }
  }
  $form['order_statuses'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Order statuses'),
    '#open' => TRUE,
  ];
  $form['order_statuses']['order_statuses'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('ID'),
      $this
        ->t('Title'),
      $this
        ->t('List position'),
      $this
        ->t('State'),
      $this
        ->t('Remove'),
    ],
  ];
  foreach ($statuses as $status) {
    $form['order_statuses']['order_statuses'][$status
      ->id()]['id'] = [
      '#markup' => $status
        ->id(),
    ];
    $form['order_statuses']['order_statuses'][$status
      ->id()]['name'] = [
      '#type' => 'textfield',
      '#default_value' => $status
        ->getName(),
      '#size' => 32,
      '#required' => TRUE,
    ];
    $form['order_statuses']['order_statuses'][$status
      ->id()]['weight'] = [
      '#type' => 'weight',
      '#delta' => 20,
      '#default_value' => $status
        ->getWeight(),
    ];
    if ($status
      ->isLocked()) {
      $form['order_statuses']['order_statuses'][$status
        ->id()]['state'] = [
        '#markup' => $states[$status
          ->getState()],
      ];
    }
    else {
      $form['order_statuses']['order_statuses'][$status
        ->id()]['state'] = [
        '#type' => 'select',
        '#options' => $states,
        '#default_value' => $status
          ->getState(),
      ];
      $form['order_statuses']['order_statuses'][$status
        ->id()]['remove'] = [
        '#type' => 'checkbox',
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}