PaymentMethodConfigurationBase.php in Payment 8.2
File
src/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBase.php
View source
<?php
namespace Drupal\payment\Plugin\Payment\MethodConfiguration;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PaymentMethodConfigurationBase extends PluginBase implements PaymentMethodConfigurationInterface, ConfigurableInterface, DependentPluginInterface {
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('module_handler'));
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return array(
'message_text' => '',
'message_text_format' => 'plain_text',
);
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
public function setMessageText($text) {
$this->configuration['message_text'] = $text;
return $this;
}
public function getMessageText() {
return $this->configuration['message_text'];
}
public function setMessageTextFormat($format) {
$this->configuration['message_text_format'] = $format;
return $this;
}
public function getMessageTextFormat() {
return $this->configuration['message_text_format'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$elements['message'] = array(
'#tree' => TRUE,
'#type' => 'textarea',
'#title' => $this
->t('Payment form message'),
'#default_value' => $this
->getMessageText(),
);
if ($this->moduleHandler
->moduleExists('filter')) {
$elements['message']['#type'] = 'text_format';
$elements['message']['#format'] = $this
->getMessageTextFormat();
}
return $elements;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$message = NestedArray::getValue($values, $form['message']['#parents']);
if ($this->moduleHandler
->moduleExists('filter')) {
$this
->setMessageText($message['value']);
$this
->setMessageTextFormat($message['format']);
}
else {
$this
->setMessageText($message);
}
}
}