View source
<?php
declare (strict_types=1);
namespace Drupal\language_selection_page\Plugin\LanguageSelectionPageCondition;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\language_selection_page\LanguageSelectionPageConditionBase;
use Drupal\language_selection_page\LanguageSelectionPageConditionInterface;
use Drupal\path_alias\AliasManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
final class LanguageSelectionPageConditionPath extends LanguageSelectionPageConditionBase implements LanguageSelectionPageConditionInterface {
protected $aliasManager;
protected $cacheConfig;
protected $configFactory;
protected $currentPath;
protected $messenger;
protected $pathValidator;
protected $requestStack;
protected $routeBuilder;
public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, RequestStack $request_stack, CurrentPathStack $current_path, RouteBuilderInterface $route_builder, CacheBackendInterface $cache_config, PathValidatorInterface $path_validator, MessengerInterface $messenger, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->aliasManager = $alias_manager;
$this->requestStack = $request_stack;
$this->currentPath = $current_path;
$this->routeBuilder = $route_builder;
$this->cacheConfig = $cache_config;
$this->pathValidator = $path_validator;
$this->messenger = $messenger;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
global $base_url;
$form[$this
->getPluginId()] = [
'#type' => 'textfield',
'#default_value' => $this->configuration[$this
->getPluginId()],
'#description' => t('The path of the page displaying the Language Selection Page'),
'#required' => TRUE,
'#size' => 40,
'#field_prefix' => $base_url,
];
return $form;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('config.factory'), $container
->get('path_alias.manager'), $container
->get('request_stack'), $container
->get('path.current'), $container
->get('router.builder'), $container
->get('cache.config'), $container
->get('path.validator'), $container
->get('messenger'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
$configuration = $this
->getConfiguration();
$configuration_lsp = $this->configFactory
->getEditable('language_selection_page.negotiation');
$system_site = $this->configFactory
->getEditable('system.site');
if ($configuration['path'] === $system_site
->get('page.front')) {
$configuration_lsp
->set('path', '/language_selection_page')
->save();
$system_site
->set('page.front', '/node')
->save();
$this->messenger
->addError($this
->t('The Language Selection Page cannot be used as frontpage. To avoid infinite redirect loops, the language selection page path has been reset to <strong>/language_selection_page</strong> and the default frontpage setting has been reset to <strong>/node</strong>.'));
return $this
->block();
}
$current_path = $this->currentPath
->getPath($this->requestStack
->getCurrentRequest());
$alias = $this->aliasManager
->getAliasByPath($current_path);
foreach ([
$current_path,
$alias,
] as $path) {
if ($path === $configuration[$this
->getPluginId()]) {
return $this
->block();
}
}
return $this
->pass();
}
public function postConfigSave(array &$form, FormStateInterface $form_state) {
$this->routeBuilder
->rebuildIfNeeded();
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if ($this->configuration[$this
->getPluginId()] !== $form_state
->getValue($this
->getPluginId())) {
$this->routeBuilder
->setRebuildNeeded();
}
parent::submitConfigurationForm($form, $form_state);
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$configuration_lsp = $this->configFactory
->getEditable('language_selection_page.negotiation');
if ($form_state
->isValueEmpty($this
->getPluginId())) {
$form_state
->setValueForElement($form['conditions'][$this
->getPluginId()], '/language_selection_page');
}
else {
$form_state
->setValueForElement($form['conditions'][$this
->getPluginId()], $this->aliasManager
->getPathByAlias($form_state
->getValue($this
->getPluginId())));
}
if (($value = $form_state
->getValue($this
->getPluginId())) && $value[0] !== '/') {
$form_state
->setErrorByName($this
->getPluginId(), $this
->t("The path '%path' has to start with a slash.", [
'%path' => $form_state
->getValue($this
->getPluginId()),
]));
}
if ($this->pathValidator
->isValid($form_state
->getValue($this
->getPluginId())) && $form_state
->getValue($this
->getPluginId()) !== $configuration_lsp
->get('path')) {
$form_state
->setErrorByName($this
->getPluginId(), $this
->t("The path '%path' is invalid.", [
'%path' => $form_state
->getValue($this
->getPluginId()),
]));
}
}
}