You are here

public function CheckoutFlowForm::form in Commerce Core 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

modules/checkout/src/Form/CheckoutFlowForm.php, line 41

Class

CheckoutFlowForm

Namespace

Drupal\commerce_checkout\Form

Code

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

  /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
  $checkout_flow = $this->entity;
  $plugins = array_column($this->pluginManager
    ->getDefinitions(), 'label', 'id');
  asort($plugins);

  // Use the first available plugin as the default value.
  if (!$checkout_flow
    ->getPluginId()) {
    $plugin_ids = array_keys($plugins);
    $plugin = reset($plugin_ids);
    $checkout_flow
      ->setPluginId($plugin);
  }
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'commerce_checkout/admin';
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#maxlength' => 255,
    '#default_value' => $checkout_flow
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $checkout_flow
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_checkout\\Entity\\CheckoutFlow::load',
    ],
    '#disabled' => !$checkout_flow
      ->isNew(),
  ];
  $form['plugin'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Plugin'),
    '#options' => $plugins,
    '#default_value' => $checkout_flow
      ->getPluginId(),
    '#required' => TRUE,
    '#disabled' => !$checkout_flow
      ->isNew(),
  ];
  if (!$checkout_flow
    ->isNew()) {
    $form['configuration'] = [
      '#parents' => [
        'configuration',
      ],
    ];
    $form['configuration'] = $checkout_flow
      ->getPlugin()
      ->buildConfigurationForm($form['configuration'], $form_state);
  }
  return $form;
}