AdminRoutes.php in Administration Language Negotiation 8
File
src/Plugin/AdministrationLanguageNegotiationCondition/AdminRoutes.php
View source
<?php
declare (strict_types=1);
namespace Drupal\administration_language_negotiation\Plugin\AdministrationLanguageNegotiationCondition;
use Drupal\administration_language_negotiation\AdministrationLanguageNegotiationConditionBase;
use Drupal\administration_language_negotiation\AdministrationLanguageNegotiationConditionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\Router;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class AdminRoutes extends AdministrationLanguageNegotiationConditionBase implements AdministrationLanguageNegotiationConditionInterface {
protected $adminContext;
protected $requestStack;
protected $router;
public function __construct(RequestStack $request_stack, Router $router, AdminContext $admin_context, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->requestStack = $request_stack;
$this->router = $router;
$this->adminContext = $admin_context;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[$this
->getPluginId()] = [
'#title' => $this
->t('Enable'),
'#type' => 'checkbox',
'#default_value' => $this->configuration[$this
->getPluginId()],
'#description' => $this
->t('Detects if the current path is admin route.'),
];
return $form;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('request_stack'), $container
->get('router.no_access_checks'), $container
->get('router.admin_context'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
$active = $this->configuration[$this
->getPluginId()];
return $active && $this
->isAdminRoute() ? $this
->block() : $this
->pass();
}
public function isAdminRoute() {
try {
$match = $this->router
->matchRequest($this->requestStack
->getCurrentRequest());
} catch (ResourceNotFoundException $e) {
return FALSE;
} catch (AccessDeniedHttpException $e) {
return FALSE;
}
if ($match && isset($match[RouteObjectInterface::ROUTE_OBJECT])) {
return $this->adminContext
->isAdminRoute($match[RouteObjectInterface::ROUTE_OBJECT]);
}
return FALSE;
}
}
Classes
Name |
Description |
AdminRoutes |
Class for the Blacklisted paths condition plugin. |