You are here

public function OrderForm::form in Commerce Core 8.2

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/order/src/Form/OrderForm.php, line 58

Class

OrderForm
Form controller for the commerce_order entity edit forms.

Namespace

Drupal\commerce_order\Form

Code

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

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $this->entity;
  $form = parent::form($form, $form_state);
  $form['#tree'] = TRUE;
  $form['#theme'] = 'commerce_order_edit_form';
  $form['#attached']['library'][] = 'commerce_order/form';

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = [
    '#type' => 'hidden',
    '#default_value' => $order
      ->getChangedTime(),
  ];

  // Version must be sent to the client, for later overwrite error checking.
  $form['version'] = [
    '#type' => 'hidden',
    '#default_value' => $order
      ->getVersion(),
  ];
  $last_saved = $this->dateFormatter
    ->format($order
    ->getChangedTime(), 'short');
  $form['advanced'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'entity-meta',
      ],
    ],
    '#weight' => 99,
  ];
  $form['meta'] = [
    '#attributes' => [
      'class' => [
        'entity-meta__header',
      ],
    ],
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'state' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $order
        ->getState()
        ->getLabel(),
      '#attributes' => [
        'class' => [
          'entity-meta__title',
        ],
      ],
      // Hide the rendered state if there's a widget for it.
      '#access' => empty($form['store_id']),
    ],
    'date' => NULL,
    'changed' => $this
      ->fieldAsReadOnly($this
      ->t('Last saved'), $last_saved),
  ];
  $form['customer'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Customer information'),
    '#group' => 'advanced',
    '#open' => TRUE,
    '#attributes' => [
      'class' => [
        'order-form-author',
      ],
    ],
    '#weight' => 91,
  ];
  if ($placed_time = $order
    ->getPlacedTime()) {
    $date = $this->dateFormatter
      ->format($placed_time, 'short');
    $form['meta']['date'] = $this
      ->fieldAsReadOnly($this
      ->t('Placed'), $date);
  }

  // Show the order's store only if there are multiple available.
  $store_query = $this->entityTypeManager
    ->getStorage('commerce_store')
    ->getQuery()
    ->accessCheck(TRUE);
  $store_count = $store_query
    ->count()
    ->execute();
  if ($store_count > 1) {
    $store_link = $order
      ->getStore()
      ->toLink()
      ->toString();
    $form['meta']['store'] = $this
      ->fieldAsReadOnly($this
      ->t('Store'), $store_link);
  }

  // Move uid/mail widgets to the sidebar, or provide read-only alternatives.
  $customer = $order
    ->getCustomer();
  if (isset($form['uid'])) {
    $form['uid']['#group'] = 'customer';
  }
  elseif ($customer
    ->isAuthenticated()) {
    $customer_link = $customer
      ->toLink()
      ->toString();
    $form['customer']['uid'] = $this
      ->fieldAsReadOnly($this
      ->t('Customer'), $customer_link);
  }
  if (isset($form['mail'])) {
    $form['mail']['#group'] = 'customer';
  }
  elseif (!empty($order
    ->getEmail())) {
    $form['customer']['mail'] = $this
      ->fieldAsReadOnly($this
      ->t('Contact email'), $order
      ->getEmail());
  }

  // All additional customer information should come after uid/mail.
  $form['customer']['ip_address'] = $this
    ->fieldAsReadOnly($this
    ->t('IP address'), $order
    ->getIpAddress());
  return $form;
}