View source
<?php
declare (strict_types=1);
namespace Drupal\language_selection_page\Plugin\LanguageSelectionPageCondition;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\language_selection_page\LanguageSelectionPageConditionBase;
use Drupal\language_selection_page\LanguageSelectionPageConditionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class LanguageSelectionPageConditionLanguagePrefixes extends LanguageSelectionPageConditionBase implements LanguageSelectionPageConditionInterface {
protected $configFactory;
protected $languageManager;
public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
}
public function alterPageContent(array &$content = [], $destination = '<front>') {
$links = [];
$cacheable_metadata = new CacheableMetadata();
$cacheable_metadata
->addCacheTags([
'config:configurable_language_list',
'config:language_selection_page.negotiation',
]);
$cacheable_metadata
->addCacheContexts([
'url',
]);
try {
$links_array = [];
foreach ($this->languageManager
->getNativeLanguages() as $language) {
$url = Url::fromUserInput($destination, [
'language' => $language,
]);
$links_array[$language
->getId()] = [
'url' => clone $url,
'title' => $language
->getName(),
'language' => $language,
'attributes' => [
'class' => [
'language-link',
],
],
];
}
foreach ($this->languageManager
->getNativeLanguages() as $language) {
$url = Url::fromUserInput($destination, [
'language' => $language,
]);
$project_link = Link::fromTextAndUrl($language
->getName(), $url);
$project_link = $project_link
->toRenderable();
$project_link['#attributes'] = [
'class' => [
'language_selection_page_link_' . $language
->getId(),
],
];
$links[$language
->getId()] = $project_link;
}
} catch (\InvalidArgumentException $exception) {
$destination = '<front>';
}
$content[] = [
'#theme' => 'language_selection_page_content',
'#destination' => $destination,
'#language_links' => [
'#theme' => 'item_list',
'#items' => $links,
],
];
$cacheable_metadata
->applyTo($content);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('language_manager'), $container
->get('config.factory'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
$languages = $this->languageManager
->getNativeLanguages();
$language_negotiation_config = $this->configFactory
->get('language.negotiation')
->get('url');
$prefixes = array_filter($language_negotiation_config['prefixes']);
if (\count($languages) !== \count($prefixes)) {
return $this
->block();
}
return $this
->pass();
}
}