You are here

public function Payment::buildForm in Ubercart 8.4

Form constructor.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being viewed.

array $form: An 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 EditableOrderPanePluginInterface::buildForm

File

payment/uc_payment/src/Plugin/Ubercart/OrderPane/Payment.php, line 86

Class

Payment
Specify and collect payment for an order.

Namespace

Drupal\uc_payment\Plugin\Ubercart\OrderPane

Code

public function buildForm(OrderInterface $order, array $form, FormStateInterface $form_state) {
  $options = [];
  $methods = PaymentMethod::loadMultiple();
  uasort($methods, 'Drupal\\uc_payment\\Entity\\PaymentMethod::sort');
  foreach ($methods as $method) {
    $options[$method
      ->id()] = $method
      ->label();
  }
  $form['payment_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Payment method'),
    '#default_value' => $order
      ->getPaymentMethodId(),
    '#options' => $options,
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxCallback',
      ],
      'progress' => [
        'type' => 'throbber',
      ],
      'wrapper' => 'payment-details',
    ],
  ];

  // An empty <div> for Ajax.
  $form['payment_details'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'payment-details',
    ],
    '#tree' => TRUE,
  ];
  $method = $form_state
    ->getValue('payment_method') ?: $order
    ->getPaymentMethodId();
  if ($method && ($details = PaymentMethod::load($method)
    ->getPlugin()
    ->orderEditDetails($order))) {
    if (is_array($details)) {
      $form['payment_details'] += $details;
    }
    else {
      $form['payment_details']['#markup'] = $details;
    }
  }
  return $form;
}