3 namespace Drupal\language\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockBase;
7 use Drupal\Core\Path\PathMatcherInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Provides a 'Language switcher' block.
18 * id = "language_block",
19 * admin_label = @Translation("Language switcher"),
20 * category = @Translation("System"),
21 * deriver = "Drupal\language\Plugin\Derivative\LanguageBlock"
24 class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface {
27 * The language manager.
29 * @var \Drupal\Core\Language\LanguageManagerInterface
31 protected $languageManager;
36 * @var \Drupal\Core\Path\PathMatcherInterface
38 protected $pathMatcher;
41 * Constructs an LanguageBlock object.
43 * @param array $configuration
44 * A configuration array containing information about the plugin instance.
45 * @param string $plugin_id
46 * The plugin_id for the plugin instance.
47 * @param mixed $plugin_definition
48 * The plugin implementation definition.
49 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
50 * The language manager.
51 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
54 public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher) {
55 parent::__construct($configuration, $plugin_id, $plugin_definition);
56 $this->languageManager = $language_manager;
57 $this->pathMatcher = $path_matcher;
64 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69 $container->get('language_manager'),
70 $container->get('path.matcher')
78 protected function blockAccess(AccountInterface $account) {
79 $access = $this->languageManager->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden();
80 return $access->addCacheTags(['config:configurable_language_list']);
86 public function build() {
88 $route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>';
89 $type = $this->getDerivativeId();
90 $links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRoute($route_name));
92 if (isset($links->links)) {
94 '#theme' => 'links__language_block',
95 '#links' => $links->links,
98 "language-switcher-{$links->method_id}",
101 '#set_active_class' => TRUE,
110 * @todo Make cacheable in https://www.drupal.org/node/2232375.
112 public function getCacheMaxAge() {