You are here

public static function PaymentMethodAddForm::addAddressAttributes in Commerce Stripe 8

Element #after_build callback: adds "data-stripe" to address properties.

This allows our JavaScript to pass these values to Stripe as customer information, enabling CVC, Zip, and Street checks.

Parameters

array $element: The form element.

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

Return value

array The modified form element.

File

src/PluginForm/Stripe/PaymentMethodAddForm.php, line 144

Class

PaymentMethodAddForm
Provides payment form for Stripe.

Namespace

Drupal\commerce_stripe\PluginForm\Stripe

Code

public static function addAddressAttributes(array $element, FormStateInterface $form_state) {
  if (isset($element['address'])) {
    $element['address']['widget'][0]['address']['given_name']['#attributes']['data-stripe'] = 'given_name';
    $element['address']['widget'][0]['address']['family_name']['#attributes']['data-stripe'] = 'family_name';
    $element['address']['widget'][0]['address']['address_line1']['#attributes']['data-stripe'] = 'address_line1';
    $element['address']['widget'][0]['address']['address_line2']['#attributes']['data-stripe'] = 'address_line2';
    $element['address']['widget'][0]['address']['locality']['#attributes']['data-stripe'] = 'address_city';
    $element['address']['widget'][0]['address']['postal_code']['#attributes']['data-stripe'] = 'address_zip';

    // Country code is a sub-element and needs another callback.
    $element['address']['widget'][0]['address']['country_code']['#pre_render'][] = [
      get_called_class(),
      'addCountryCodeAttributes',
    ];
  }
  return $element;
}