You are here

public function OrderAddForm::buildForm in Commerce Core 8.2

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

modules/order/src/Form/OrderAddForm.php, line 61

Class

OrderAddForm
Provides the order add form.

Namespace

Drupal\commerce_order\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Skip building the form if there are no available stores.
  $store_query = $this->storeStorage
    ->getQuery()
    ->accessCheck(TRUE);
  if ($store_query
    ->count()
    ->execute() == 0) {
    $link = Link::createFromRoute('Add a new store.', 'entity.commerce_store.add_page');
    $form['warning'] = [
      '#markup' => $this
        ->t("Orders can't be created until a store has been added. @link", [
        '@link' => $link
          ->toString(),
      ]),
    ];
    return $form;
  }
  $form['type'] = [
    '#type' => 'commerce_entity_select',
    '#title' => $this
      ->t('Order type'),
    '#target_type' => 'commerce_order_type',
    '#required' => TRUE,
  ];
  $form['store_id'] = [
    '#type' => 'commerce_entity_select',
    '#title' => $this
      ->t('Store'),
    '#target_type' => 'commerce_store',
    '#required' => TRUE,
  ];
  $form = $this
    ->buildCustomerForm($form, $form_state);
  $form['custom_placed_date'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Place the order on a different date'),
    '#default_value' => FALSE,
  ];

  // The datetime element needs to be wrapped in order for #states to work
  // properly. See https://www.drupal.org/node/2419131
  $form['placed_wrapper'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        ':input[name="custom_placed_date"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['placed_wrapper']['placed'] = [
    '#type' => 'datetime',
    '#title_display' => 'invisible',
    '#title' => $this
      ->t('Placed'),
    '#default_value' => new DrupalDateTime(),
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Create'),
    '#button_type' => 'primary',
  ];
  return $form;
}