You are here

public function DemoCouponCallout::buildPaneForm in Commerce Demo 8

Builds the pane form.

Parameters

array $pane_form: The pane form, containing the following basic properties:

  • #parents: Identifies the position of the pane form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

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

array $complete_form: The complete form structure.

Overrides CheckoutPaneInterface::buildPaneForm

File

src/Plugin/Commerce/CheckoutPane/DemoCouponCallout.php, line 34

Class

DemoCouponCallout
Provides the Order summary pane.

Namespace

Drupal\commerce_demo\Plugin\Commerce\CheckoutPane

Code

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'well',
      ],
    ],
  ];
  $build['heading'] = [
    '#type' => 'inline_template',
    '#template' => '<h4 class="text-muted"><strong>{{ title }}</strong></h4>',
    '#context' => [
      'title' => $this
        ->t('Try out a coupon in our demo!'),
    ],
  ];
  $build['coupon_codes'] = [
    '#theme' => 'item_list',
    '#items' => [],
    '#attributes' => [
      'class' => [
        'text-muted',
      ],
    ],
  ];

  /** @var \Drupal\commerce_promotion\CouponStorageInterface $coupon_storage */
  $coupon_storage = $this->entityTypeManager
    ->getStorage('commerce_promotion_coupon');
  $coupons = array_filter($coupon_storage
    ->loadMultiple(), function (CouponInterface $coupon) {
    return $coupon
      ->available($this->order) && $coupon
      ->getPromotion()
      ->applies($this->order);
  });
  $build['coupon_codes']['#items'] = array_map(function (CouponInterface $coupon) {
    return $coupon
      ->getCode();
  }, $coupons);
  return $build;
}