View source
<?php
namespace Drupal\payment\Plugin\Payment\Status;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Url;
use Drupal\payment\PaymentAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PaymentStatusBase extends PluginBase implements ContainerFactoryPluginInterface, PaymentStatusInterface, PluginFormInterface, ConfigurableInterface {
use PaymentAwareTrait;
protected $currentUser;
protected $defaultDateTime;
protected $moduleHandler;
protected $paymentStatusManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, PaymentStatusManagerInterface $payment_status_manager, DrupalDateTime $default_datetime) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->defaultDateTime = $default_datetime;
$this->moduleHandler = $module_handler;
$this->paymentStatusManager = $payment_status_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('plugin.manager.payment.status'), new DrupalDateTime());
}
public function defaultConfiguration() {
return [
'created' => time(),
'id' => 0,
];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
public function setCreated($created) {
$this->configuration['created'] = $created;
return $this;
}
public function getCreated() {
return $this->configuration['created'];
}
function getAncestors() {
return $this->paymentStatusManager
->getAncestors($this
->getPluginId());
}
public function getChildren() {
return $this->paymentStatusManager
->getChildren($this
->getPluginId());
}
function getDescendants() {
return $this->paymentStatusManager
->getDescendants($this
->getPluginId());
}
function hasAncestor($plugin_id) {
return $this->paymentStatusManager
->hasAncestor($this
->getPluginId(), $plugin_id);
}
function isOrHasAncestor($plugin_id) {
return $this->paymentStatusManager
->isOrHasAncestor($this
->getPluginId(), $plugin_id);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if ($this->moduleHandler
->moduleExists('datetime')) {
$elements['created'] = [
'#default_value' => $this->defaultDateTime,
'#required' => TRUE,
'#title' => $this
->t('Date and time'),
'#type' => 'datetime',
];
}
else {
$elements['created'] = [
'#default_value' => $this->defaultDateTime,
'#type' => 'value',
];
if ($this->currentUser
->hasPermission('administer modules')) {
$elements['created_message'] = [
'#type' => 'markup',
'#markup' => $this
->t('Enable the <a href="@url">Datetime</a> module to set the date and time of the new payment status.', [
'@url' => new Url('system.modules_list', [], [
'fragment' => 'module-datetime',
]),
]),
];
}
}
return $elements;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}