protected function PaymentMethodAddForm::buildPaymentOptions in Commerce Core 8.2
Builds the payment options.
This will build the payment options for payment gateways that support creating payment methods.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
\Drupal\commerce_payment\PaymentOption[] The payment options.
1 call to PaymentMethodAddForm::buildPaymentOptions()
- PaymentMethodAddForm::buildForm in modules/
payment/ src/ Form/ PaymentMethodAddForm.php - Form constructor.
File
- modules/
payment/ src/ Form/ PaymentMethodAddForm.php, line 143
Class
- PaymentMethodAddForm
- Provides the payment method add form.
Namespace
Drupal\commerce_payment\FormCode
protected function buildPaymentOptions(FormStateInterface $form_state) {
$payment_gateway_storage = $this->entityTypeManager
->getStorage('commerce_payment_gateway');
$payment_gateways = $payment_gateway_storage
->loadByProperties([
'status' => TRUE,
]);
$payment_gateways = array_filter($payment_gateways, function ($payment_gateway) {
return $payment_gateway
->getPlugin() instanceof SupportsCreatingPaymentMethodsInterface;
});
if (!$payment_gateways) {
return [];
}
$form_state
->set('payment_gateways', $payment_gateways);
$payment_options = [];
// 3) Add options to create new stored payment methods of supported types.
$payment_method_type_counts = [];
// Count how many new payment method options will be built per gateway.
foreach ($payment_gateways as $payment_gateway) {
$payment_method_types = $payment_gateway
->getPlugin()
->getPaymentMethodTypes();
foreach ($payment_method_types as $payment_method_type_id => $payment_method_type) {
if (!isset($payment_method_type_counts[$payment_method_type_id])) {
$payment_method_type_counts[$payment_method_type_id] = 1;
}
else {
$payment_method_type_counts[$payment_method_type_id]++;
}
}
}
foreach ($payment_gateways as $payment_gateway) {
$payment_gateway_plugin = $payment_gateway
->getPlugin();
$payment_method_types = $payment_gateway_plugin
->getPaymentMethodTypes();
foreach ($payment_method_types as $payment_method_type_id => $payment_method_type) {
$option_id = 'new--' . $payment_method_type_id . '--' . $payment_gateway
->id();
$option_label = $payment_method_type
->getCreateLabel();
// If there is more than one option for this payment method type,
// append the payment gateway label to avoid duplicate option labels.
if ($payment_method_type_counts[$payment_method_type_id] > 1) {
$option_label = $this
->t('@payment_method_label (@payment_gateway_label)', [
'@payment_method_label' => $payment_method_type
->getCreateLabel(),
'@payment_gateway_label' => $payment_gateway_plugin
->getDisplayLabel(),
]);
}
$payment_options[$option_id] = new PaymentOption([
'id' => $option_id,
'label' => $option_label,
'payment_gateway_id' => $payment_gateway
->id(),
'payment_method_type_id' => $payment_method_type_id,
]);
}
}
return $payment_options;
}