Mercurial > hg > isophonics-drupal-site
comparison core/modules/language/src/Plugin/Block/LanguageBlock.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\language\Plugin\Block; | |
4 | |
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; | |
11 use Drupal\Core\Url; | |
12 use Symfony\Component\DependencyInjection\ContainerInterface; | |
13 | |
14 /** | |
15 * Provides a 'Language switcher' block. | |
16 * | |
17 * @Block( | |
18 * id = "language_block", | |
19 * admin_label = @Translation("Language switcher"), | |
20 * category = @Translation("System"), | |
21 * deriver = "Drupal\language\Plugin\Derivative\LanguageBlock" | |
22 * ) | |
23 */ | |
24 class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface { | |
25 | |
26 /** | |
27 * The language manager. | |
28 * | |
29 * @var \Drupal\Core\Language\LanguageManagerInterface | |
30 */ | |
31 protected $languageManager; | |
32 | |
33 /** | |
34 * The path matcher. | |
35 * | |
36 * @var \Drupal\Core\Path\PathMatcherInterface | |
37 */ | |
38 protected $pathMatcher; | |
39 | |
40 /** | |
41 * Constructs an LanguageBlock object. | |
42 * | |
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 | |
52 * The path matcher. | |
53 */ | |
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; | |
58 } | |
59 | |
60 | |
61 /** | |
62 * {@inheritdoc} | |
63 */ | |
64 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
65 return new static( | |
66 $configuration, | |
67 $plugin_id, | |
68 $plugin_definition, | |
69 $container->get('language_manager'), | |
70 $container->get('path.matcher') | |
71 ); | |
72 } | |
73 | |
74 | |
75 /** | |
76 * {@inheritdoc} | |
77 */ | |
78 protected function blockAccess(AccountInterface $account) { | |
79 $access = $this->languageManager->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden(); | |
80 return $access->addCacheTags(['config:configurable_language_list']); | |
81 } | |
82 | |
83 /** | |
84 * {@inheritdoc} | |
85 */ | |
86 public function build() { | |
87 $build = []; | |
88 $route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>'; | |
89 $type = $this->getDerivativeId(); | |
90 $links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRoute($route_name)); | |
91 | |
92 if (isset($links->links)) { | |
93 $build = [ | |
94 '#theme' => 'links__language_block', | |
95 '#links' => $links->links, | |
96 '#attributes' => [ | |
97 'class' => [ | |
98 "language-switcher-{$links->method_id}", | |
99 ], | |
100 ], | |
101 '#set_active_class' => TRUE, | |
102 ]; | |
103 } | |
104 return $build; | |
105 } | |
106 | |
107 /** | |
108 * {@inheritdoc} | |
109 * | |
110 * @todo Make cacheable in https://www.drupal.org/node/2232375. | |
111 */ | |
112 public function getCacheMaxAge() { | |
113 return 0; | |
114 } | |
115 | |
116 } |