public function DropdownLanguage::build in Dropdown Language 8
Same name and namespace in other branches
- 8.3 src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::build()
- 8.2 src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::build()
- 3.0.x src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::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 BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ DropdownLanguage.php, line 85
Class
- DropdownLanguage
- Provides an alternative language switcher block.
Namespace
Drupal\dropdown_language\Plugin\BlockCode
public function build() {
$block = [];
$languages = $this->languageManager
->getLanguages();
if (count($languages) > 1) {
$current_language = $this->languageManager
->getCurrentLanguage()
->getId();
$links = $this->languageManager
->getLanguageSwitchLinks("language_interface", Url::fromRoute('<current>'))->links;
// Place active language ontop of list.
if (isset($links[$current_language])) {
$links = [
$current_language => $links[$current_language],
] + $links;
// Set an active class for styling.
$links[$current_language]['attributes']['class'][] = 'active-language';
}
// Get block instance and general settings.
$block_config = $this
->getConfiguration();
$general_config = $this->configFactory
->get('dropdown_language.setting');
$wrapper_default = $general_config
->get('wrapper');
$display_language_id = $general_config
->get('display_language_id');
// Re-label as per general setting.
foreach ($links as $lid => $link) {
switch ($display_language_id) {
case '0':
$links[$lid]['title'] = Unicode::strtoupper($lid);
break;
case '2':
$links[$lid]['title'] = isset($block_config['labels'][$lid]) ? $block_config['labels'][$lid] : $link['language']
->getName();
break;
}
}
$dropdown_button = [
'#type' => 'dropbutton',
'#subtype' => 'dropdown_language',
'#links' => $links,
'#attributes' => [
'class' => [
'dropdown-language-item',
],
],
'#attached' => [
'library' => [
'dropdown_language/dropdown-language-selector',
],
],
];
if ($wrapper_default == 1) {
$block['switcher'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Switch Language'),
];
$block['switcher']['switch-language'] = $dropdown_button;
}
else {
$block['switch-language'] = $dropdown_button;
}
}
return $block;
}