Answers:
您可以通过两种方式将项目添加到管理工具栏:
作为内容:
在ui中 /admin/structure/menu/manage/admin
或在代码中:
$item = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
'link' => ['uri' => 'internal:/<front>'],
'title' => 'Front Page',
'menu_name' => 'admin',
]);
$item->save();
或在静态配置文件中:
system.admin:
title: Administration
route_name: system.admin
weight: 9
menu_name: admin
system.admin_content:
title: Content
description: 'Find and manage content.'
route_name: system.admin_content
parent: system.admin
weight: -10
system.admin_structure:
route_name: system.admin_structure
parent: system.admin
description: 'Administer blocks, content types, menus, etc.'
title: Structure
weight: -8
system.themes_page:
route_name: system.themes_page
title: Appearance
description: 'Select and configure themes.'
parent: system.admin
weight: -6
这是system.links.menu.yml的开始,它定义了D8中的管理菜单。您可以在mymodule.links.menu.yml中添加自己的条目。
编辑:
要将项目添加到第一行,请使用钩子mymodule_toolbar()
。这是来自tour模块的示例:
/**
* Implements hook_toolbar().
*/
function tour_toolbar() {
$items = [];
$items['tour'] = [
'#cache' => [
'contexts' => [
'user.permissions',
],
],
];
if (!\Drupal::currentUser()->hasPermission('access tour')) {
return $items;
}
$items['tour'] += array(
'#type' => 'toolbar_item',
'tab' => array(
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => t('Tour'),
'#attributes' => array(
'class' => array('toolbar-icon', 'toolbar-icon-help'),
'aria-pressed' => 'false',
),
),
'#wrapper_attributes' => array(
'class' => array('tour-toolbar-tab', 'hidden'),
'id' => 'toolbar-tab-tour',
),
'#attached' => array(
'library' => array(
'tour/tour',
),
),
);
return $items;
}
hook_toolbar
和ToolbarHandler
。
对于所有想知道他们可以将先前答案的代码放在哪里的人–例如,您可以在MYMODULE.install中使用它
function MYMODULE_install(){
$item = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
'link' => ['uri' => 'internal:/admin/link'],
'title' => 'Link title',
'menu_name' => 'admin',
]);
$item->save();
}