Add Language Switcher on admin toolbar
-
30-01-2021 - |
解决方案
Well, I got something up and running via hook_toolbar
. Only thing left is, that you can't click directly on the "Language: EN" string in the toolbar. You have to click slightly next to it. Maybe you can fix this.
Apart from that, please also have a look at the Devel module and their implementation of hook_toolbar
where they have an extra src/ToolbarHandler.php
class for everything. You better should rebuild the code below into a dedicated handler.
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Implements hook_toolbar().
*/
function MYMODULE_toolbar() {
// Get languages, get current route.
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$languages = \Drupal::languageManager()->getLanguages();
$route = \Drupal::service('path.matcher')
->isFrontPage() ? '<front>' : '<current>';
// Get links.
$links = [];
foreach ($languages as $language) {
$url = new Url($route, [], ['language' => $language]);
$links[] = [
'#markup' => Link::fromTextAndUrl($language->getName(), $url)
->toString(),
];
}
// Set cache.
$items['admin_toolbar_langswitch'] = [
'#cache' => [
'contexts' => [
'languages:language_interface',
'url',
],
],
];
// Build toolbar item and tray.
$items['admin_toolbar_langswitch'] += [
'#type' => 'toolbar_item',
'#weight' => 999,
'tab' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => '<span>' . t('Language') . '</span>: <span class="current-language">' . strtoupper($current_language) . '</span>',
'#attributes' => [
'class' => ['toolbar-item-admin-toolbar-langswitch'],
'title' => t('Admin Toolbar Langswitch'),
],
],
'tray' => [
'#heading' => t('Admin Toolbar Langswitch'),
'content' => [
'#theme' => 'item_list',
'#items' => $links,
'#attributes' => [
'class' => ['toolbar-menu'],
],
],
],
];
return $items;
}
其他提示
The example above for hook_toolbar is almost perfect. I fixed it, see code below. You just have to remove the span on the Language : EN then the click functionality works perfectly.
see my code:
/**
* Implements hook_toolbar().
*/
function examplemodule_toolbar() {
// Get languages, get current route.
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$languages = \Drupal::languageManager()->getLanguages();
$route = \Drupal::service('path.matcher')
->isFrontPage() ? '<front>' : '<current>';
// Get links.
$links = [];
foreach ($languages as $language) {
$url = new Url($route, [], ['language' => $language]);
$links[] = [
'#markup' => Link::fromTextAndUrl($language->getName(), $url)
->toString(),
];
}
// Set cache.
$items['admin_toolbar_langswitch'] = [
'#cache' => [
'contexts' => [
'languages:language_interface',
'url',
],
],
];
// Build toolbar item and tray.
$items['admin_toolbar_langswitch'] += [
'#type' => 'toolbar_item',
'#weight' => 999,
'tab' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => t('Language') . ': ' . strtoupper($current_language),
'#attributes' => [
'class' => ['toolbar-item-admin-toolbar-langswitch'],
'title' => t('Admin Toolbar Langswitch'),
],
],
'tray' => [
'#heading' => t('Admin Toolbar Langswitch'),
'content' => [
'#theme' => 'item_list',
'#items' => $links,
'#attributes' => [
'class' => ['toolbar-menu'],
],
],
],
];
return $items;
}
The link in the reply berramou posted doesn't mention the contrib module toolbar_menu, which makes adding stuff in the toolbar more convenient than what is mentioned in the link.