class PaymentFormConfigurationForm in Payment 8.2
Provides the configuration form for the payment_form payment type plugin.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\payment_form\Plugin\Payment\Type\PaymentFormConfigurationForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of PaymentFormConfigurationForm
1 file declares its use of PaymentFormConfigurationForm
- PaymentFormConfigurationFormTest.php in modules/
payment_form/ tests/ src/ Unit/ Plugin/ Payment/ Type/ PaymentFormConfigurationFormTest.php
File
- modules/
payment_form/ src/ Plugin/ Payment/ Type/ PaymentFormConfigurationForm.php, line 19
Namespace
Drupal\payment_form\Plugin\Payment\TypeView source
class PaymentFormConfigurationForm extends ConfigFormBase {
/**
* The payment method manager.
*
* @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface
*/
protected $paymentMethodManager;
/**
* The plugin selector plugin type.
*
* @var \Drupal\plugin\PluginType\PluginTypeInterface
*/
protected $pluginSelectorType;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* @param \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface $payment_method_manager
* @param \Drupal\plugin\PluginType\PluginTypeInterface $plugin_selector_type
*/
public function __construct(ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, PaymentMethodManagerInterface $payment_method_manager, PluginTypeInterface $plugin_selector_type) {
parent::__construct($config_factory);
$this->paymentMethodManager = $payment_method_manager;
$this->pluginSelectorType = $plugin_selector_type;
$this->stringTranslation = $string_translation;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Drupal\plugin\PluginType\PluginTypeManagerInterface $plugin_type_manager */
$plugin_type_manager = $container
->get('plugin.plugin_type_manager');
return new static($container
->get('config.factory'), $container
->get('string_translation'), $container
->get('plugin.manager.payment.method'), $plugin_type_manager
->getPluginType('plugin_selector'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'payment_form_configuration';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'payment_form.payment_type',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('payment_form.payment_type');
$form['plugin_selector'] = $this
->getPluginSelector($form_state)
->buildSelectorForm([], $form_state);
$limit_allowed_plugins_id = Html::getUniqueId('limit_allowed_plugins');
$form['limit_allowed_plugins'] = [
'#default_value' => $config
->get('limit_allowed_plugins'),
'#id' => $limit_allowed_plugins_id,
'#title' => $this
->t('Limit allowed payment methods'),
'#type' => 'checkbox',
];
$allowed_plugin_ids = $config
->get('allowed_plugin_ids');
$options = [];
foreach ($this->paymentMethodManager
->getDefinitions() as $definition) {
$options[$definition['id']] = $definition['label'];
}
$form['allowed_plugin_ids'] = [
'#default_value' => $allowed_plugin_ids,
'#multiple' => TRUE,
'#options' => $options,
'#states' => [
'visible' => [
'#' . $limit_allowed_plugins_id => [
'checked' => TRUE,
],
],
],
'#title' => $this
->t('Allowed payment methods'),
'#type' => 'select',
];
return $form + parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this
->getPluginSelector($form_state)
->validateSelectorForm($form['plugin_selector'], $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$plugin_selector = $this
->getPluginSelector($form_state);
$plugin_selector
->submitSelectorForm($form['plugin_selector'], $form_state);
$selected_plugin = $plugin_selector
->getSelectedPlugin();
$config = $this
->config('payment_form.payment_type');
$values = $form_state
->getValues();
$config
->set('plugin_selector_id', $selected_plugin
->getPluginId());
if (PluginHelper::isConfigurable($selected_plugin)) {
$selected_plugin_configuration = $selected_plugin
->getConfiguration();
}
else {
$selected_plugin_configuration = [];
}
$config
->set('plugin_selector_configuration', $selected_plugin_configuration);
$config
->set('limit_allowed_plugins', $values['limit_allowed_plugins']);
$config
->set('allowed_plugin_ids', $values['allowed_plugin_ids']);
$config
->save();
parent::submitForm($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) {
$config = $this
->config('payment_form.payment_type');
if ($form_state
->has('plugin_selector')) {
$plugin_selector = $form_state
->get('plugin_selector');
}
else {
$plugin_selector_manager = $this->pluginSelectorType
->getPluginManager();
$plugin_selector = $plugin_selector_manager
->createInstance('payment_radios');
$plugin_selector
->setSelectablePluginType($this->pluginSelectorType);
$plugin_selector
->setLabel($this
->t('Payment method selector'));
$plugin_selector
->setRequired();
$plugin_selector
->setSelectedPlugin($plugin_selector_manager
->createInstance($config
->get('plugin_selector_id')));
$form_state
->set('plugin_selector', $plugin_selector);
}
return $plugin_selector;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
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. | |
PaymentFormConfigurationForm:: |
protected | property | The payment method manager. | |
PaymentFormConfigurationForm:: |
protected | property | The plugin selector plugin type. | |
PaymentFormConfigurationForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
PaymentFormConfigurationForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
PaymentFormConfigurationForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
PaymentFormConfigurationForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PaymentFormConfigurationForm:: |
protected | function | Gets the plugin selector. | |
PaymentFormConfigurationForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
PaymentFormConfigurationForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
PaymentFormConfigurationForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase:: |
|
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. |