SelectPaymentMethodConfiguration.php in Payment 8.2
File
src/Controller/SelectPaymentMethodConfiguration.php
View source
<?php
namespace Drupal\payment\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SelectPaymentMethodConfiguration extends ControllerBase {
protected $paymentMethodConfigurationAccessControlHandler;
protected $paymentMethodConfigurationManager;
public function __construct(EntityAccessControlHandlerInterface $payment_method_configuration_access_control_handler, PaymentMethodConfigurationManagerInterface $payment_method_configuration_manager, AccountInterface $current_user) {
$this->currentUser = $current_user;
$this->paymentMethodConfigurationAccessControlHandler = $payment_method_configuration_access_control_handler;
$this->paymentMethodConfigurationManager = $payment_method_configuration_manager;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getAccessControlHandler('payment_method_configuration'), $container
->get('plugin.manager.payment.method_configuration'), $container
->get('current_user'));
}
public function execute() {
$definitions = $this->paymentMethodConfigurationManager
->getDefinitions();
unset($definitions['payment_unavailable']);
$items = [];
foreach ($definitions as $plugin_id => $definition) {
$access = $this->paymentMethodConfigurationAccessControlHandler
->createAccess($plugin_id);
if ($access) {
$items[] = [
'title' => $definition['label'],
'description' => $definition['description'],
'localized_options' => [],
'url' => new Url('payment.payment_method_configuration.add', [
'plugin_id' => $plugin_id,
]),
];
}
}
return [
'#theme' => 'admin_block_content',
'#content' => $items,
];
}
public function access() {
$definitions = $this->paymentMethodConfigurationManager
->getDefinitions();
unset($definitions['payment_unavailable']);
$access_result = AccessResult::forbidden();
foreach (array_keys($definitions) as $plugin_id) {
$access_result = $this->paymentMethodConfigurationAccessControlHandler
->createAccess($plugin_id, $this->currentUser, [], TRUE);
if ($access_result
->isAllowed()) {
return $access_result;
}
}
return $access_result;
}
}