class DisabledLanguagesEventSubscriber in Disable language 8
Subscribe to KernelEvents::REQUEST events.
Hierarchy
- class \Drupal\disable_language\EventSubscriber\DisabledLanguagesEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of DisabledLanguagesEventSubscriber
1 string reference to 'DisabledLanguagesEventSubscriber'
1 service uses DisabledLanguagesEventSubscriber
File
- src/
EventSubscriber/ DisabledLanguagesEventSubscriber.php, line 23
Namespace
Drupal\disable_language\EventSubscriberView source
class DisabledLanguagesEventSubscriber implements EventSubscriberInterface {
/**
* Contains disable_language.disable_language_manager service.
*
* @var \Drupal\disable_language\DisableLanguageManager
*/
protected $disableLanguageManager;
/**
* Contains current_user service.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* This module's settings configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The schemes of all available StreamWrapper.
*
* @var array
*/
protected $schemes;
/**
* A plugin manager for conditions plugins.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $conditionManager;
/**
* DisabledLanguagesEventSubscriber constructor.
*
* @param \Drupal\disable_language\DisableLanguageManager $disableLanguageManager
* Class DisableLanguageManager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* A proxied implementation of AccountInterface.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
* @param \Drupal\Core\Condition\ConditionManager $conditionManager
* A plugin manager for conditions plugins.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManager $streamWrapperManager
*/
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());
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// On a normal request.
$events[KernelEvents::REQUEST][] = [
'checkForDisabledLanguageAndRedirect',
];
// On an access denied request.
$events[KernelEvents::EXCEPTION][] = [
'checkForDisabledLanguageAndRedirect',
0,
];
return $events;
}
/**
* {@inheritdoc}
*/
public function checkForDisabledLanguageAndRedirect(GetResponseEvent $event) {
// Do not redirect if this is a file.
$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()) {
// Get the configured redirect language if there is one.
$redirect_language = $this->disableLanguageManager
->getFallbackLanguage();
if ($redirect_language) {
$language = \Drupal::languageManager()
->getLanguage($redirect_language);
}
else {
$language = $this->disableLanguageManager
->getFirstEnabledLanguage();
}
if (isset($language)) {
// Create url object to redirect to in the correct language. By
// default we redirect to the frontpage.
$url = Url::fromRoute('<front>', [], [
'language' => $language,
]);
// Check our configuration to see which routes should redirect to the
// current page but in the correct language instead of the frontpage.
$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(),
]);
}
}
// Set the response.
$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);
}
}
}
}
/**
* Check if the path belong to the list of excluded ones.
*
* @return bool
* Whether or not the requested path being accessed is excluded.
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\Core\Executable\ExecutableException
*/
private function isPathExcluded() {
if (($excluded_path_config = $this->config
->get('exclude_request_path')) && !empty($excluded_path_config['pages'])) {
/** @var \Drupal\system\Plugin\Condition\RequestPath $condition */
$condition = $this->conditionManager
->createInstance('request_path');
$condition
->setConfiguration($excluded_path_config);
return $this->conditionManager
->execute($condition);
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DisabledLanguagesEventSubscriber:: |
protected | property | A plugin manager for conditions plugins. | |
DisabledLanguagesEventSubscriber:: |
protected | property | This module's settings configuration. | |
DisabledLanguagesEventSubscriber:: |
protected | property | Contains current_user service. | |
DisabledLanguagesEventSubscriber:: |
protected | property | Contains disable_language.disable_language_manager service. | |
DisabledLanguagesEventSubscriber:: |
protected | property | The schemes of all available StreamWrapper. | |
DisabledLanguagesEventSubscriber:: |
public | function | ||
DisabledLanguagesEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
DisabledLanguagesEventSubscriber:: |
private | function | Check if the path belong to the list of excluded ones. | |
DisabledLanguagesEventSubscriber:: |
public | function | DisabledLanguagesEventSubscriber constructor. |