0

I've successfully added an item to the the admin toolbar and now would like to create a drop down menu from that (i.e. the way Manage > Structure drops down Content Types, etc.)

This is how I've created the tray links ...

/**
 * Implements hook_toolbar().
 */
function mymenu_toolbar() {

  // Create the toolbar render array
  $items['mymenu'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => t('MyMenu'),
      '#url' => Url::fromRoute('<front>'),
      '#attributes' => [
        'title' => t('MyMenu'),
        'class' => ['toolbar-icon', 'toolbar-icon-mymenu'],
      ],
    ],
    'tray' => [
      '#heading' => t('MyMenu'),
      'shortcuts' => [
        '#theme' => 'links__toolbar_mymenu',
        '#links' => [
          ['my_page_1'] = [
            'title' => t('My Page 1'),
            'url' => \Drupal\Core\Url::fromRoute('mymenu.mypage1'),
            'attributes' => [
              'title' => t('My Page 1 Description'),
            ],
          ],
          ['my_page_2'] = [
            'title' => t('My Page 2'),
            'url' => \Drupal\Core\Url::fromRoute('mymenu.mypage2'),
            'attributes' => [
              'title' => t('My Page 2 Description'),
            ],
          ],
        ],  
        '#attributes' => [
          'class' => ['toolbar-menu'],
        ],
      ],
    ],
    '#weight' => 999,
    '#attached' => [
      'library' => [
        'mymenu/mymenu.icons',
      ],
    ],
  ];

  return $items;
}

This produces ...

enter image description here

I thought perhaps adding my drop down items as children of the items in the #links array would work, but I haven't yet been able to work it out or find a good example of that being done.

Any suggestions?

  • Asking to recommend off-site resources (demos) is off-topic. That said you have your demos right there. Simply look how the existing menu items (Devel, Core) did it. There you go. – leymannx Jul 22 at 16:04
  • You might want to look at the admin toolbar module. This updates the default admin toolbar to support dropdowns with multiple children. – CG Monroe Jul 22 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.