DisabledLanguagesEventSubscriber.php in Disable language 8
File
src/EventSubscriber/DisabledLanguagesEventSubscriber.php
View source
<?php
namespace Drupal\disable_language\EventSubscriber;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Url;
use Drupal\disable_language\DisableLanguageManager;
use Drupal\Core\Cache\CacheableMetadata;
class DisabledLanguagesEventSubscriber implements EventSubscriberInterface {
protected $disableLanguageManager;
protected $currentUser;
protected $config;
protected $schemes;
protected $conditionManager;
public function __construct(DisableLanguageManager $disableLanguageManager, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, ConditionManager $conditionManager, StreamWrapperManager $streamWrapperManager) {
$this->currentUser = $currentUser;
$this->disableLanguageManager = $disableLanguageManager;
$this->config = $configFactory
->get('disable_language.settings');
$this->conditionManager = $conditionManager;
$this->schemes = array_keys($streamWrapperManager
->getWrappers());
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'checkForDisabledLanguageAndRedirect',
];
$events[KernelEvents::EXCEPTION][] = [
'checkForDisabledLanguageAndRedirect',
0,
];
return $events;
}
public function checkForDisabledLanguageAndRedirect(GetResponseEvent $event) {
$params = $event
->getRequest()->attributes
->all();
if (isset($params['scheme']) && in_array($params['scheme'], $this->schemes)) {
return;
}
elseif ($this
->isPathExcluded()) {
return;
}
elseif (!$this->currentUser
->hasPermission('view disabled languages')) {
if ($this->disableLanguageManager
->isCurrentLanguageDisabled()) {
$redirect_language = $this->disableLanguageManager
->getFallbackLanguage();
if ($redirect_language) {
$language = \Drupal::languageManager()
->getLanguage($redirect_language);
}
else {
$language = $this->disableLanguageManager
->getFirstEnabledLanguage();
}
if (isset($language)) {
$url = Url::fromRoute('<front>', [], [
'language' => $language,
]);
$redirectOverrideRoutes = $this->config
->get('redirect_override_routes');
if (!empty($redirectOverrideRoutes)) {
$routeMatch = RouteMatch::createFromRequest($event
->getRequest());
$routeName = $routeMatch
->getRouteName();
if (in_array($routeName, $redirectOverrideRoutes)) {
$url = Url::fromRoute($routeMatch
->getRouteName(), $routeMatch
->getRawParameters()
->all(), [
'language' => $language,
'query' => $event
->getRequest()->query
->all(),
]);
}
}
$cache = new CacheableMetadata();
$cache
->addCacheContexts([
'languages',
'url',
'user.permissions',
]);
$cache
->addCacheableDependency($this->config);
$cache
->addCacheableDependency($language);
$response = new TrustedRedirectResponse($url
->toString(), '307');
$response
->addCacheableDependency($cache);
$event
->setResponse($response);
}
}
}
}
private function isPathExcluded() {
if (($excluded_path_config = $this->config
->get('exclude_request_path')) && !empty($excluded_path_config['pages'])) {
$condition = $this->conditionManager
->createInstance('request_path');
$condition
->setConfiguration($excluded_path_config);
return $this->conditionManager
->execute($condition);
}
return FALSE;
}
}