You are here

public function CheckoutFlowWithPanesBase::buildConfigurationForm in Commerce Core 8.2

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides CheckoutFlowBase::buildConfigurationForm

File

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesBase.php, line 212

Class

CheckoutFlowWithPanesBase
Provides a base checkout flow that uses checkout panes.

Namespace

Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  if (!$form_state
    ->has('panes')) {
    $form_state
      ->set('panes', $this
      ->getPanes());
  }

  // Group the panes by step id for region display.
  $grouped_panes = [];
  foreach ($form_state
    ->get('panes') as $pane_id => $pane) {

    /** @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface $pane */
    $step_id = $pane
      ->getStepId();
    $grouped_panes[$step_id][$pane_id] = $pane;
  }
  $wrapper_id = Html::getUniqueId('checkout-pane-overview-wrapper');
  $form['panes'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Pane'),
      $this
        ->t('Weight'),
      $this
        ->t('Step'),
      [
        'data' => $this
          ->t('Settings'),
        'colspan' => 2,
      ],
    ],
    '#attributes' => [
      'class' => [
        'checkout-pane-overview',
      ],
      // Used by the JS code when attaching behaviors.
      'id' => 'checkout-pane-overview',
    ],
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
    '#wrapper_id' => $wrapper_id,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'pane-weight',
      ],
      [
        'action' => 'match',
        'relationship' => 'self',
        'group' => 'pane-step',
        'subgroup' => 'pane-step',
        'source' => 'pane-id',
      ],
    ],
  ];
  foreach ($this
    ->getTableRegions() as $step_id => $region) {
    $form['panes']['region-' . $step_id] = [
      '#attributes' => [
        'class' => [
          'region-title',
        ],
        'no_striping' => TRUE,
      ],
    ];
    $form['panes']['region-' . $step_id]['title'] = [
      '#markup' => $region['title'],
      '#wrapper_attributes' => [
        'colspan' => 5,
      ],
    ];
    $form['panes']['region-' . $step_id . '-message'] = [
      '#attributes' => [
        'class' => [
          'region-message',
          'region-' . $step_id . '-message',
          empty($grouped_panes[$step_id]) ? 'region-empty' : 'region-populated',
        ],
        'no_striping' => TRUE,
      ],
    ];
    $form['panes']['region-' . $step_id . '-message']['message'] = [
      '#markup' => $region['message'],
      '#wrapper_attributes' => [
        'colspan' => 5,
      ],
    ];
    if (!empty($grouped_panes[$step_id])) {
      foreach ($grouped_panes[$step_id] as $pane_id => $pane) {
        $form['panes'][$pane_id] = $this
          ->buildPaneRow($pane, $form, $form_state);
      }
    }
  }
  return $form;
}