View source
<?php
declare (strict_types=1);
namespace Drupal\language_selection_page\Plugin\LanguageSelectionPageCondition;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\MainContent\MainContentRendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\language_selection_page\LanguageSelectionPageConditionBase;
use Drupal\language_selection_page\LanguageSelectionPageConditionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
final class LanguageSelectionPageConditionType extends LanguageSelectionPageConditionBase implements LanguageSelectionPageConditionInterface {
protected $configFactory;
protected $currentRouteMatch;
protected $mainContentRenderer;
protected $requestStack;
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack, RouteMatchInterface $current_route_match, MainContentRendererInterface $main_content_renderer, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->requestStack = $request_stack;
$this->currentRouteMatch = $current_route_match;
$this->mainContentRenderer = $main_content_renderer;
}
public function alterPageContent(array &$content = [], $destination = '<front>') {
$config = $this->configFactory
->get('language_selection_page.negotiation');
if ($config
->get('type') === 'standalone' && !$content instanceof RedirectResponse) {
$content = [
'#type' => 'page',
'#title' => $config
->get('title'),
'content' => $content,
];
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[$this
->getPluginId()] = [
'#type' => 'select',
'#multiple' => FALSE,
'#default_value' => $this->configuration[$this
->getPluginId()],
'#options' => [
'standalone' => 'Standalone',
'embedded' => 'Embedded',
'block' => 'Block',
],
'#description' => $this
->t('<ul>
<li><b>Standalone - Template only</b>: Display the Language Selection Page template only.</li>
<li><b>Embedded - Template in theme</b>: Insert the Language Selection Page body as <i>{{ content }}</i> in the page template for the current theme.</li>
<li><b>Block - In a Drupal block</b>: Insert the Language Selection Page in a block <em>Language Selection Block</em>.</li>
</ul>'),
];
return $form;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('config.factory'), $container
->get('request_stack'), $container
->get('current_route_match'), $container
->get('main_content_renderer.html'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
if ($this->configuration[$this
->getPluginId()] === 'block') {
return $this
->block();
}
return $this
->pass();
}
public function getDestination($destination) {
$config = $this->configFactory
->get('language_selection_page.negotiation');
$request = $this->requestStack
->getCurrentRequest();
if ($config
->get('type') !== 'block') {
if (!empty($request
->getQueryString())) {
list(, $destination) = explode('=', $request
->getQueryString(), 2);
$destination = urldecode($destination);
}
}
else {
$destination = $request
->getPathInfo();
}
return $destination;
}
}