View source
<?php
namespace Drupal\payment\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\payment\Plugin\Payment\Type\PaymentTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ListPaymentTypes extends ControllerBase {
protected $paymentTypeManager;
public function __construct(ModuleHandlerInterface $module_handler, PaymentTypeManagerInterface $payment_type_manager, AccountInterface $current_user, TranslationInterface $string_translation) {
$this->moduleHandler = $module_handler;
$this->paymentTypeManager = $payment_type_manager;
$this->stringTranslation = $string_translation;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('plugin.manager.payment.type'), $container
->get('current_user'), $container
->get('string_translation'));
}
public function execute() {
$table = [
'#empty' => $this
->t('There are no available payment types.'),
'#header' => [
$this
->t('Type'),
$this
->t('Description'),
$this
->t('Operations'),
],
'#type' => 'table',
];
$definitions = $this->paymentTypeManager
->getDefinitions();
unset($definitions['payment_unavailable']);
foreach ($definitions as $plugin_id => $definition) {
$operations_provider = $this->paymentTypeManager
->getOperationsProvider($plugin_id);
$operations = $operations_provider ? $operations_provider
->getOperations($plugin_id) : [];
$operations['configure'] = [
'url' => new Url('payment.payment_type', [
'bundle' => $plugin_id,
]),
'title' => $this
->t('Configure'),
];
if ($this->moduleHandler
->moduleExists('field_ui')) {
if ($this->currentUser
->hasPermission('administer payment fields')) {
$operations['manage-fields'] = [
'title' => $this
->t('Manage fields'),
'url' => new Url('entity.payment.field_ui_fields', [
'bundle' => $plugin_id,
]),
];
}
if ($this->currentUser
->hasPermission('administer payment form display')) {
$operations['manage-form-display'] = [
'title' => $this
->t('Manage form display'),
'url' => new Url('entity.entity_form_display.payment.default', [
'bundle' => $plugin_id,
]),
];
}
if ($this->currentUser
->hasPermission('administer payment display')) {
$operations['manage-display'] = [
'title' => $this
->t('Manage display'),
'url' => new Url('entity.entity_view_display.payment.default', [
'bundle' => $plugin_id,
]),
];
}
}
$table[$plugin_id]['label'] = [
'#markup' => $definition['label'],
];
$table[$plugin_id]['description'] = [
'#markup' => isset($definition['description']) ? $definition['description'] : NULL,
];
$table[$plugin_id]['operations'] = [
'#links' => $operations,
'#type' => 'operations',
];
}
return $table;
}
}