class PaymentForm in Payment 8.2
Same name in this branch
- 8.2 modules/payment_form/src/Entity/Payment/PaymentForm.php \Drupal\payment_form\Entity\Payment\PaymentForm
- 8.2 modules/payment_form/src/Plugin/Payment/Type/PaymentForm.php \Drupal\payment_form\Plugin\Payment\Type\PaymentForm
- 8.2 modules/payment_form/src/Plugin/Field/FieldFormatter/PaymentForm.php \Drupal\payment_form\Plugin\Field\FieldFormatter\PaymentForm
- 8.2 modules/payment_form/src/Plugin/Field/FieldWidget/PaymentForm.php \Drupal\payment_form\Plugin\Field\FieldWidget\PaymentForm
- 8.2 modules/payment_form/src/Plugin/Field/FieldType/PaymentForm.php \Drupal\payment_form\Plugin\Field\FieldType\PaymentForm
Provides the payment form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\payment_form\Entity\Payment\PaymentForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of PaymentForm
2 files declare their use of PaymentForm
- PaymentFormTest.php in modules/
payment_form/ tests/ src/ Unit/ Entity/ Payment/ PaymentFormTest.php - payment_form.module in modules/
payment_form/ payment_form.module - Hook implementations and general functions.
File
- modules/
payment_form/ src/ Entity/ Payment/ PaymentForm.php, line 18
Namespace
Drupal\payment_form\Entity\PaymentView source
class PaymentForm extends ContentEntityForm {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The payment method plugin type.
*
* @var \Drupal\plugin\PluginType\PluginTypeInterface
*/
protected $paymentMethodType;
/**
* The plugin selector manager.
*
* @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface
*/
protected $pluginSelectorManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\plugin\PluginType\PluginTypeManagerInterface $plugin_type_manager */
$plugin_type_manager = $container
->get('plugin.plugin_type_manager');
/** @var static $form */
$form = parent::create($container);
$form->currentUser = $container
->get('current_user');
$form->paymentMethodType = $plugin_type_manager
->getPluginType('payment_method');
$form->pluginSelectorManager = $container
->get('plugin.manager.plugin.plugin_selector');
$form->urlGenerator = $container
->get('url_generator');
$form->stringTranslation = $container
->get('string_translation');
return $form;
}
/**
* Sets the plugin selector manager.
*
* @param \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface $plugin_selector_manager
* The plugin selector manager.
*/
public function setPluginSelectorManager(PluginSelectorManagerInterface $plugin_selector_manager) {
$this->pluginSelectorManager = $plugin_selector_manager;
}
/**
* Sets the payment method plugin type.
*
* @param \Drupal\plugin\PluginType\PluginTypeInterface $payment_method_type
* The plugin type.
*/
public function setPaymentMethodPluginType(PluginTypeInterface $payment_method_type) {
$this->paymentMethodType = $payment_method_type;
}
/**
* Sets the current user.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function setCurrentUser(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
protected function currentUser() {
return $this->currentUser;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$payment = $this
->getEntity();
$form['line_items'] = [
'#payment_line_items' => $payment,
'#type' => 'payment_line_items_display',
];
$form['payment_method'] = $this
->getPluginSelector($form_state)
->buildSelectorForm([], $form_state);
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->getPluginSelector($form_state)
->validateSelectorForm($form['payment_method'], $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\payment\Entity\PaymentInterface $payment */
$payment = $this
->getEntity();
$plugin_selector = $this
->getPluginSelector($form_state);
$plugin_selector
->submitSelectorForm($form['payment_method'], $form_state);
$payment
->setPaymentMethod($plugin_selector
->getSelectedPlugin());
$payment
->save();
$result = $payment
->execute();
if (!$result
->isCompleted()) {
$form_state
->setResponse($result
->getCompletionResponse()
->getResponse());
}
}
/**
* Returns an array of supported actions for the current entity form.
*/
protected function actions(array $form, FormStateInterface $form_state) {
// Only use the existing submit action.
$actions = parent::actions($form, $form_state);
$actions = [
'submit' => $actions['submit'],
];
$actions['submit']['#value'] = $this
->t('Pay');
$actions['submit']['#disabled'] = count($this
->getPaymentMethodManager()
->getDefinitions()) == 0;
return $actions;
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// Remove this override once https://drupal.org/node/2409143 has been fixed.
$this
->getFormDisplay($form_state)
->extractFormValues($entity, $form, $form_state);
}
/**
* Gets the plugin selector.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
*/
protected function getPluginSelector(FormStateInterface $form_state) {
if ($form_state
->has('plugin_selector')) {
$plugin_selector = $form_state
->get('plugin_selector');
}
else {
$config = $this
->config('payment_form.payment_type');
$plugin_selector_id = $config
->get('plugin_selector_id');
$plugin_selector = $this->pluginSelectorManager
->createInstance($plugin_selector_id);
$plugin_selector
->setSelectablePluginType($this->paymentMethodType);
$plugin_selector
->setSelectablePluginDiscovery($this
->getPaymentMethodManager());
$plugin_selector
->setSelectablePluginFactory($this
->getPaymentMethodManager());
$plugin_selector
->setRequired();
$plugin_selector
->setLabel($this
->t('Payment method'));
$form_state
->set('plugin_selector', $plugin_selector);
}
return $plugin_selector;
}
/**
* Gets the payment method manager.
*
* @return \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface
*/
protected function getPaymentMethodManager() {
$config = $this
->config('payment_form.payment_type');
$limit_allowed_plugins = $config
->get('limit_allowed_plugins');
$payment_method_discovery = $this->paymentMethodType
->getPluginManager();
if ($limit_allowed_plugins) {
$allowed_plugin_ids = $config
->get('allowed_plugin_ids');
$payment_method_discovery = new LimitedPluginDiscoveryDecorator($payment_method_discovery);
$payment_method_discovery
->setDiscoveryLimit($allowed_plugin_ids);
}
$payment_method_manager = new PaymentExecutionPaymentMethodManager($this
->getEntity(), $this->currentUser, $this->paymentMethodType
->getPluginManager(), $payment_method_discovery);
return $payment_method_manager;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentEntityForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
9 |
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
3 |
ContentEntityForm:: |
protected | function | Flags violations for the current form. | 4 |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | 4 |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. | |
ContentEntityForm:: |
public | function | Constructs a ContentEntityForm object. | 9 |
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 | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentForm:: |
protected | property | The current user. | |
PaymentForm:: |
protected | property | The payment method plugin type. | |
PaymentForm:: |
protected | property | The plugin selector manager. | |
PaymentForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
PaymentForm:: |
protected | function |
Copies top-level form values to entity properties Overrides ContentEntityForm:: |
|
PaymentForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContentEntityForm:: |
|
PaymentForm:: |
protected | function |
Gets the current user. Overrides FormBase:: |
|
PaymentForm:: |
public | function |
Gets the actual form array to be built. Overrides ContentEntityForm:: |
|
PaymentForm:: |
protected | function | Gets the payment method manager. | |
PaymentForm:: |
protected | function | Gets the plugin selector. | |
PaymentForm:: |
public | function | Sets the current user. | |
PaymentForm:: |
public | function | Sets the payment method plugin type. | |
PaymentForm:: |
public | function | Sets the plugin selector manager. | |
PaymentForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides ContentEntityForm:: |
|
PaymentForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-level… Overrides ContentEntityForm:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |