You are here

class PaymentFormConfigurationForm in Payment 8.2

Provides the configuration form for the payment_form payment type plugin.

Hierarchy

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\Type
View 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

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentFormConfigurationForm::$paymentMethodManager protected property The payment method manager.
PaymentFormConfigurationForm::$pluginSelectorType protected property The plugin selector plugin type.
PaymentFormConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PaymentFormConfigurationForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
PaymentFormConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PaymentFormConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PaymentFormConfigurationForm::getPluginSelector protected function Gets the plugin selector.
PaymentFormConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
PaymentFormConfigurationForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
PaymentFormConfigurationForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.