n How to add a menu item to the toolbar in Drupal 8 & 9 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

How to add a menu item to the toolbar in Drupal 8 & 9



You can add items to the admin toolbar in two ways:

As content:

In ui /admin/structure/menu/manage/admin


or in code:

function MYMODULE_install(){
    $item = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
      'link' => ['uri' => 'internal:/admin/link'],
      'title' => 'Link title',
      'menu_name' => 'admin',


Or in static configuration files:

  title: Administration
  route_name: system.admin
  weight: 9
  menu_name: admin
  title: Content
  description: 'Find and manage content.'
  route_name: system.admin_content
  parent: system.admin
  weight: -10
  route_name: system.admin_structure
  parent: system.admin
  description: 'Administer blocks, content types, menus, etc.'
  title: Structure
  weight: -8
  route_name: system.themes_page
  title: Appearance
  description: 'Select and configure themes.'
  parent: system.admin
  weight: -6

This is the start of system.links.menu.yml, which defines the admin menu as we know it from D8. You can add your own entries in mymodule.links.menu.yml.


  title: 'MyModule Title'
  route_name: mymodule.custom_settings_form
  description: 'Custom Link'
  parent: system.admin
  weight: 102


To add an item to the top row, use the hook mymodule_toolbar(). This is an example from the tour module:

* Implements hook_toolbar().
function tour_toolbar() {
  $items = [];
  $items['tour'] = [
    '#cache' => [
      'contexts' => [

  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(
return $items;


Dont forget to clear the cache. 


Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts


Page Facebook