PaymentStatusForm.php in Payment 8.2
File
src/Entity/Payment/PaymentStatusForm.php
View source
<?php
namespace Drupal\payment\Entity\Payment;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentMethodUpdatePaymentStatusInterface;
use Drupal\payment\Plugin\Payment\PaymentAwarePluginManagerDecorator;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginDiscovery\LimitedPluginDiscoveryDecorator;
use Drupal\plugin\PluginType\PluginTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentStatusForm extends ContentEntityForm {
protected $currentUser;
protected $pluginSelectorManager;
protected $paymentStatusPluginType;
public static function create(ContainerInterface $container) {
$form = parent::create($container);
$plugin_type_manager = $container
->get('plugin.plugin_type_manager');
$form->currentUser = $container
->get('current_user');
$form->paymentStatusPluginType = $plugin_type_manager
->getPluginType('payment_status');
$form->pluginSelectorManager = $container
->get('plugin.manager.plugin.plugin_selector');
$form->urlGenerator = $container
->get('url_generator');
$form->stringTranslation = $container
->get('string_translation');
return $form;
}
public function setPluginSelectorManager(PluginSelectorManagerInterface $plugin_selector_manager) {
$this->pluginSelectorManager = $plugin_selector_manager;
}
public function setPaymentStatusPluginType(PluginTypeInterface $payment_status_plugin_type) {
$this->paymentStatusPluginType = $payment_status_plugin_type;
}
public function setCurrentUser(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
protected function currentUser() {
return $this->currentUser;
}
public function form(array $form, FormStateInterface $form_state) {
$form['payment_status'] = $this
->getPluginSelector($form_state)
->buildSelectorForm([], $form_state);
return parent::form($form, $form_state);
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions = array(
$actions['submit'],
);
return $actions;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->getPluginSelector($form_state)
->validateSelectorForm($form['payment_status'], $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$payment = $this
->getEntity();
$plugin_selector = $this
->getPluginSelector($form_state);
$plugin_selector
->submitSelectorForm($form['payment_status'], $form_state);
$payment
->setPaymentStatus($plugin_selector
->getSelectedPlugin());
$payment
->save();
$form_state
->setRedirectUrl($payment
->toUrl());
}
protected function getPluginSelector(FormStateInterface $form_state) {
if ($form_state
->has('plugin_selector')) {
$plugin_selector = $form_state
->get('plugin_selector');
}
else {
$payment = $this
->getEntity();
$payment_method = $payment
->getPaymentMethod();
$payment_status_discovery = new LimitedPluginDiscoveryDecorator($this->paymentStatusPluginType
->getPluginManager());
if ($payment_method instanceof PaymentMethodUpdatePaymentStatusInterface) {
$payment_status_discovery
->setDiscoveryLimit($payment_method
->getSettablePaymentStatuses($this->currentUser, $payment));
}
$payment_status_manager = new PaymentAwarePluginManagerDecorator($payment, $this->paymentStatusPluginType
->getPluginManager(), $payment_status_discovery);
$plugin_selector = $this->pluginSelectorManager
->createInstance('payment_select_list');
$plugin_selector
->setSelectablePluginType($this->paymentStatusPluginType, $payment_status_manager);
$plugin_selector
->setRequired();
$plugin_selector
->setLabel($this
->t('Payment status'));
$form_state
->set('plugin_selector', $plugin_selector);
}
return $plugin_selector;
}
}