public function LanguageSwitcherBlock::build in Open Social 8.7
Same name and namespace in other branches
- 8.9 modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 8.4 modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 8.5 modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 8.6 modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 8.8 modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 10.3.x modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 10.0.x modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 10.1.x modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
- 10.2.x modules/custom/social_language/src/Plugin/Block/LanguageSwitcherBlock.php \Drupal\social_language\Plugin\Block\LanguageSwitcherBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides LanguageBlock::build
See also
\Drupal\block\BlockViewBuilder
File
- modules/
custom/ social_language/ src/ Plugin/ Block/ LanguageSwitcherBlock.php, line 28
Class
- LanguageSwitcherBlock
- Provides a 'LanguageSwitcherBlock' block.
Namespace
Drupal\social_language\Plugin\BlockCode
public function build() {
/** @var \Drupal\Core\Language\Language $currentLanguage */
$currentLanguage = $this->languageManager
->getCurrentLanguage();
// Build the menu.
$block = [
'#attributes' => [
'class' => [
'navbar-user',
],
],
'menu_items' => [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#attributes' => [
'class' => [
'nav',
'navbar-nav',
],
],
'#items' => [],
],
];
// Add `'#icon' => 'language',` to this array to replace the text
// with an icon.
$block['menu_items']['#items']['language'] = [
'#type' => 'account_header_element',
'#title' => $currentLanguage
->getName() . " (" . $currentLanguage
->getId() . ")",
'#label' => $currentLanguage
->getName(),
'#url' => Url::fromRoute('<none>'),
'#wrapper_attributes' => [
'class' => [
'dropdown',
],
],
];
// Generate the routes for the current page.
$route_name = $this->pathMatcher
->isFrontPage() ? '<front>' : '<current>';
$type = $this
->getDerivativeId();
$switchLinks = $this->languageManager
->getLanguageSwitchLinks($type, Url::fromRoute($route_name));
// Use the default URL generator that does not rewrite the language.
$url_generator = \Drupal::service('drupal_core_url_generator');
// Add languages as links.
foreach ($switchLinks->links as $langcode => $link) {
$link['url']
->setOption('language', $this->languageManager
->getLanguage($langcode));
$link['url']
->setUrlGenerator($url_generator);
$block['menu_items']['#items']['language'][$langcode] = [
'#type' => 'link',
'#label' => $link['title'] . " (" . $langcode . ")",
'#title' => $link['title'] . " (" . $langcode . ")",
'#url' => $link['url'],
'#attributes' => [
'class' => [
$langcode === $currentLanguage
->getId() ? 'active' : NULL,
],
],
];
}
return $block;
}