public function CouponRedemption::buildInlineForm in Commerce Core 8.2
Builds the inline form.
Parameters
array $inline_form: The inline form, containing the following basic properties:
- #parents: Identifies the location of the field values in $form_state.
\Drupal\Core\Form\FormStateInterface $form_state: The form state of the complete form.
Return value
array The built inline form.
Overrides InlineFormBase::buildInlineForm
File
- modules/
promotion/ src/ Plugin/ Commerce/ InlineForm/ CouponRedemption.php, line 83
Class
- CouponRedemption
- Provides an inline form for redeeming a coupon.
Namespace
Drupal\commerce_promotion\Plugin\Commerce\InlineFormCode
public function buildInlineForm(array $inline_form, FormStateInterface $form_state) {
$inline_form = parent::buildInlineForm($inline_form, $form_state);
$order = $this->entityTypeManager
->getStorage('commerce_order')
->load($this->configuration['order_id']);
if (!$order) {
throw new \RuntimeException('Invalid order_id given to the coupon_redemption inline form.');
}
assert($order instanceof OrderInterface);
/** @var \Drupal\commerce_promotion\Entity\CouponInterface[] $coupons */
$coupons = $order
->get('coupons')
->referencedEntities();
$inline_form = [
'#tree' => TRUE,
'#attached' => [
'library' => [
'commerce_promotion/coupon_redemption_form',
],
],
'#theme' => 'commerce_coupon_redemption_form',
'#configuration' => $this
->getConfiguration(),
] + $inline_form;
$inline_form['code'] = [
'#type' => 'textfield',
'#title' => $this
->t('Coupon code'),
// Chrome autofills this field with the address line 1, and ignores
// autocomplete => 'off', but respects 'new-password'.
'#attributes' => [
'autocomplete' => 'new-password',
],
];
$inline_form['apply'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply coupon'),
'#name' => 'apply_coupon',
'#limit_validation_errors' => [
$inline_form['#parents'],
],
'#submit' => [
[
get_called_class(),
'applyCoupon',
],
],
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefreshForm',
],
'element' => $inline_form['#parents'],
],
];
$max_coupons = $this->configuration['max_coupons'];
if ($max_coupons && count($coupons) >= $max_coupons) {
// Don't allow additional coupons to be added.
$inline_form['code']['#access'] = FALSE;
$inline_form['apply']['#access'] = FALSE;
}
foreach ($coupons as $index => $coupon) {
$inline_form['coupons'][$index]['code'] = [
'#plain_text' => $coupon
->getCode(),
];
$inline_form['coupons'][$index]['display_name'] = [
// @todo Use the promotion display name once added.
'#plain_text' => $coupon
->getPromotion()
->label(),
];
$inline_form['coupons'][$index]['remove_button'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove coupon'),
'#name' => 'remove_coupon_' . $index,
'#ajax' => [
'callback' => [
get_called_class(),
'ajaxRefreshForm',
],
'element' => $inline_form['#parents'],
],
'#weight' => 50,
'#limit_validation_errors' => [
$inline_form['#parents'],
],
'#coupon_id' => $coupon
->id(),
'#submit' => [
[
get_called_class(),
'removeCoupon',
],
],
// Simplify ajaxRefresh() by having all triggering elements
// on the same level.
'#parents' => array_merge($inline_form['#parents'], [
'remove_coupon_' . $index,
]),
];
}
return $inline_form;
}