protected function PaymentMethodAddForm::buildCreditCardForm in Commerce Core 8.2
Same name in this branch
- 8.2 modules/payment/src/PluginForm/PaymentMethodAddForm.php \Drupal\commerce_payment\PluginForm\PaymentMethodAddForm::buildCreditCardForm()
- 8.2 modules/payment_example/src/PluginForm/Onsite/PaymentMethodAddForm.php \Drupal\commerce_payment_example\PluginForm\Onsite\PaymentMethodAddForm::buildCreditCardForm()
Builds the credit card form.
Parameters
array $element: The target element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.
Return value
array The built credit card form.
2 calls to PaymentMethodAddForm::buildCreditCardForm()
- PaymentMethodAddForm::buildConfigurationForm in modules/
payment/ src/ PluginForm/ PaymentMethodAddForm.php - Form constructor.
- PaymentMethodAddForm::buildCreditCardForm in modules/
payment_example/ src/ PluginForm/ Onsite/ PaymentMethodAddForm.php - Builds the credit card form.
1 method overrides PaymentMethodAddForm::buildCreditCardForm()
- PaymentMethodAddForm::buildCreditCardForm in modules/
payment_example/ src/ PluginForm/ Onsite/ PaymentMethodAddForm.php - Builds the credit card form.
File
- modules/
payment/ src/ PluginForm/ PaymentMethodAddForm.php, line 106
Class
Namespace
Drupal\commerce_payment\PluginFormCode
protected function buildCreditCardForm(array $element, FormStateInterface $form_state) {
// Build a month select list that shows months with a leading zero.
$months = [];
for ($i = 1; $i < 13; $i++) {
$month = str_pad($i, 2, '0', STR_PAD_LEFT);
$months[$month] = $month;
}
// Build a year select list that uses a 4 digit key with a 2 digit value.
$current_year_4 = date('Y');
$current_year_2 = date('y');
$years = [];
for ($i = 0; $i < 10; $i++) {
$years[$current_year_4 + $i] = $current_year_2 + $i;
}
$element['#attributes']['class'][] = 'credit-card-form';
// Placeholder for the detected card type. Set by validateCreditCardForm().
$element['type'] = [
'#type' => 'hidden',
'#value' => '',
];
$element['number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Card number'),
'#attributes' => [
'autocomplete' => 'off',
],
'#required' => TRUE,
'#maxlength' => 19,
'#size' => 20,
];
$element['expiration'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'credit-card-form__expiration',
],
],
];
$element['expiration']['month'] = [
'#type' => 'select',
'#title' => $this
->t('Month'),
'#options' => $months,
'#default_value' => date('m'),
'#required' => TRUE,
];
$element['expiration']['divider'] = [
'#type' => 'item',
'#title' => '',
'#markup' => '<span class="credit-card-form__divider">/</span>',
];
$element['expiration']['year'] = [
'#type' => 'select',
'#title' => $this
->t('Year'),
'#options' => $years,
'#default_value' => $current_year_4,
'#required' => TRUE,
];
$element['security_code'] = [
'#type' => 'textfield',
'#title' => $this
->t('CVV'),
'#attributes' => [
'autocomplete' => 'off',
],
'#required' => TRUE,
'#maxlength' => 4,
'#size' => 4,
];
return $element;
}