Commit ea33d0f2 authored by Kyle Einecker's avatar Kyle Einecker Committed by john.oltman
Browse files

Issue #3316599 by ctrlADel: Refactor checkout pane form to make it more extensible

parent 9f844eb7
Loading
Loading
Loading
Loading
+47 −24
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormStateInterface;
@@ -151,23 +152,29 @@ class RegistrationInformation extends CheckoutPaneBase implements CheckoutPaneIn
          $inline_form = $this->inlineFormManager->createInstance('registration', [
            'order_id' => $this->order->id(),
          ], $registration);
          $id = $this->getPaneSubformId($variation, $index);
          $form_path = $this->getPaneSubformPath($variation, $index);

          $parents = $pane_form['#parents'];
          $pane_form[$id]['registration'] = [
            '#parents' => array_merge($parents, [$id, 'registration']),
            '#inline_form' => $inline_form,
          ];
          $pane_form[$id]['registration'] = [
            '#type' => 'details',
            '#open' => TRUE,
            '#title' => $this->t('@label - Registrant #@index', [
          $title = $variation->label();
          if ($quantity > 1) {
            $title = $this->t('@label - Registrant #@index', [
              '@label' => $variation->label(),
              '@index' => $index,
            ]),
          ] + $inline_form->buildInlineForm($pane_form[$id]['registration'], $form_state);
          if ($quantity == 1) {
            $pane_form[$id]['registration']['#title'] = $variation->label();
            ]);
          }
          $reg_form = [
            'registration' => [
              '#parents' => array_merge($parents, $form_path, ['registration']),
              '#inline_form' => $inline_form,
              '#type' => 'details',
              '#open' => TRUE,
              '#title' => $title,
            ],
          ];

          $reg_form['registration'] += $inline_form->buildInlineForm($reg_form['registration'], $form_state);

          NestedArray::setValue($pane_form, $form_path, $reg_form);
        }
      }
    }
@@ -192,16 +199,16 @@ class RegistrationInformation extends CheckoutPaneBase implements CheckoutPaneIn
          $emails = [];
          $quantity = (int) $item->getQuantity();
          for ($index = 1; $index <= $quantity; $index++) {
            $id = $this->getPaneSubformId($variation, $index);
            $form_path = $this->getPaneSubformPath($variation, $index);
            $variation_form = NestedArray::getValue($pane_form, $form_path);
            /** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
            $inline_form = $variation_form['registration']['#inline_form'];
            /** @var \Drupal\registration\Entity\RegistrationInterface $registration */
            $inline_form = $pane_form[$id]['registration']['#inline_form'];
            // Clone the entity to avoid messing with the inline form entity.
            $registration = clone $inline_form->getEntity();
            $form_display = $this->loadFormDisplay($registration);
            $inline_form = $pane_form[$id]['registration'];
            $form_display->extractFormValues($registration, $inline_form, $form_state);
            $email = $this->getEmail($registration, $inline_form, $form_state);
            $form_display->extractFormValues($registration, $variation_form, $form_state);
            $email = $this->getEmail($registration, $variation_form, $form_state);
            if (in_array($email, $emails)) {
              $form_state->setError($pane_form, $this->t('Duplicate registration for %email.', [
                '%email' => $email,
@@ -231,9 +238,10 @@ class RegistrationInformation extends CheckoutPaneBase implements CheckoutPaneIn
        $item->set('registration', NULL);
        $quantity = (int) $item->getQuantity();
        for ($index = 1; $index <= $quantity; $index++) {
          $id = $this->getPaneSubformId($variation, $index);
          $form_path = $this->getPaneSubformPath($variation, $index);
          $variation_form = NestedArray::getValue($pane_form, $form_path);
          /** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
          $inline_form = $pane_form[$id]['registration']['#inline_form'];
          $inline_form = $variation_form['registration']['#inline_form'];
          $registration = $inline_form->getEntity();
          $item->get('registration')->appendItem(['target_id' => $registration->id()]);
          $item->save();
@@ -305,6 +313,21 @@ class RegistrationInformation extends CheckoutPaneBase implements CheckoutPaneIn
    return 'variation-' . $variation->id() . '-' . $index;
  }

  /**
   * Gets the pane subform path for a given product variation and index.
   *
   * @param \Drupal\commerce_product\Entity\ProductVariationInterface $variation
   *   The product variation.
   * @param int $index
   *   The index.
   *
   * @return array
   *   The path to the subform.
   */
  protected function getPaneSubformPath(ProductVariationInterface $variation, int $index): array {
    return [$this->getPaneSubformId($variation, $index), 'registration'];
  }

  /**
   * Loads the form display used to build the registration form.
   *