You are here

public function OrderForm::submitForm in Ubercart 8.4

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ContentEntityForm::submitForm

File

uc_order/src/OrderForm.php, line 110

Class

OrderForm
Form controller for the Ubercart order form.

Namespace

Drupal\uc_order

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  /** @var \Drupal\uc_order\OrderInterface $order */
  $order = $this->entity;
  $original = clone $order;

  // Build list of changes to be applied.
  $panes = $this->orderPaneManager
    ->getPanes();
  foreach ($panes as $pane) {
    if ($pane instanceof EditableOrderPanePluginInterface) {
      $pane
        ->submitForm($order, $form, $form_state);
    }
  }
  $log = [];
  foreach (array_keys($order
    ->getFieldDefinitions()) as $key) {
    if ($order->{$key}->value != $original->{$key}->value) {
      if (!is_array($order->{$key}->value)) {
        $log[$key] = [
          'old' => $original->{$key}->value,
          'new' => $order->{$key}->value,
        ];
      }
    }
  }

  // Load line items again, since some may have been updated by the form.
  $order->line_items = $order
    ->getLineItems();
  $order
    ->logChanges($log);
  $order
    ->save();
  $this
    ->messenger()
    ->addMessage($this
    ->t('Order changes saved.'));
}