class PaymentMethodAddForm in Commerce Purchase Order 8
PaymentMethodAddForm for Purchase Order.
Hierarchy
- class \Drupal\Core\Plugin\PluginFormBase implements PluginAwareInterface, PluginFormInterface
- class \Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase implements PaymentGatewayFormInterface uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\commerce_payment\PluginForm\PaymentMethodFormBase implements ContainerInjectionInterface
- class \Drupal\commerce_purchase_order\PluginForm\PurchaseOrder\PaymentMethodAddForm
- class \Drupal\commerce_payment\PluginForm\PaymentMethodFormBase implements ContainerInjectionInterface
- class \Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase implements PaymentGatewayFormInterface uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of PaymentMethodAddForm
File
- src/
PluginForm/ PurchaseOrder/ PaymentMethodAddForm.php, line 14
Namespace
Drupal\commerce_purchase_order\PluginForm\PurchaseOrderView source
class PaymentMethodAddForm extends PaymentMethodFormBase {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$form['payment_details'] = [
'#parents' => array_merge($form['#parents'], [
'payment_details',
]),
'#type' => 'container',
'#payment_method_type' => 'purchase_order',
];
$form['payment_details'] = $this
->buildPurchaseOrderForm($form['payment_details'], $form_state);
// Move the billing information below the payment details.
if (isset($form['billing_information'])) {
$form['billing_information']['#weight'] = 10;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $this->entity;
$values = $form_state
->getValue($form['#parents']);
/** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface $payment_gateway_plugin */
$payment_gateway_plugin = $this->plugin;
// The payment method form is customer facing. For security reasons
// the returned errors need to be more generic.
// The Purchase Order Gateway will handle processing the form values.
try {
$payment_gateway_plugin
->createPaymentMethod($payment_method, $values['payment_details']);
} catch (DeclineException $e) {
\Drupal::logger('commerce_payment')
->warning($e
->getMessage());
throw new DeclineException('We encountered an error processing your payment method. Please verify your details and try again.');
} catch (PaymentGatewayException $e) {
\Drupal::logger('commerce_payment')
->error($e
->getMessage());
throw new PaymentGatewayException('We encountered an unexpected error processing your payment method. Please try again later.');
}
}
/**
* Builds the purchase order form.
*
* @param array $element
* The target element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*
* @return array
* The built form.
*/
protected function buildPurchaseOrderForm(array $element, FormStateInterface $form_state) {
$element['#attributes']['class'][] = 'purchase-order-form';
$element['number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Purchase Order number'),
'#attributes' => [
'autocomplete' => 'off',
],
'#required' => TRUE,
'#maxlength' => 19,
'#size' => 20,
];
return $element;
}
/**
* Handles the submission of the billing profile form.
*
* @param array $element
* The billing profile form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the complete form.
*/
protected function submitBillingProfileForm(array $element, FormStateInterface $form_state) {
$billing_profile = $element['#entity'];
$form_display = EntityFormDisplay::collectRenderDisplay($billing_profile, 'default');
$form_display
->extractFormValues($billing_profile, $element, $form_state);
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $this->entity;
$payment_method
->setBillingProfile($billing_profile);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
PaymentGatewayFormBase:: |
protected | property | The form entity. | |
PaymentGatewayFormBase:: |
public | function |
Gets the form entity. Overrides PaymentGatewayFormInterface:: |
|
PaymentGatewayFormBase:: |
public | function |
Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormInterface:: |
1 |
PaymentGatewayFormBase:: |
public | function |
Sets the form entity. Overrides PaymentGatewayFormInterface:: |
|
PaymentMethodAddForm:: |
public | function |
Form constructor. Overrides PaymentMethodFormBase:: |
|
PaymentMethodAddForm:: |
protected | function | Builds the purchase order form. | |
PaymentMethodAddForm:: |
protected | function | Handles the submission of the billing profile form. | |
PaymentMethodAddForm:: |
public | function |
Form submission handler. Overrides PaymentMethodFormBase:: |
|
PaymentMethodFormBase:: |
protected | property | The current store. | |
PaymentMethodFormBase:: |
protected | property | The entity type manager. | |
PaymentMethodFormBase:: |
protected | property | The inline form manager. | |
PaymentMethodFormBase:: |
protected | property | The logger. | |
PaymentMethodFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
PaymentMethodFormBase:: |
public | function |
Form validation handler. Overrides PluginFormBase:: |
2 |
PaymentMethodFormBase:: |
public | function | Constructs a new PaymentMethodFormBase. | |
PluginFormBase:: |
protected | property | The plugin this form is for. | 3 |
PluginFormBase:: |
public | function |
Sets the plugin for this object. Overrides PluginAwareInterface:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |