How can we alter/manage the links in toolbar user-menu?

Created on 27 May 2022, about 2 years ago

Right now, the user-menu in my toolbar contains "View Profile", "Edit Profile", and "Logout." If I wanted to remove one of these, how would I do that? Similarly, I'd like to add another link to the menu, but I'm not sure how to do that either. Any insights?

πŸ’¬ Support request
Status

Closed: works as designed

Version

3.0

Component

Documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States mrweiner

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States mrweiner

    In case anybody else stumbles upon this, I implemented the following to use a specific menu to populate the links in the toolbar.

    function module_preprocess_links__toolbar_user(&$variables) {
      $menu_tree_service = \Drupal::service('menu.link_tree');
      $menu_name = 'user-toolbar';
      $parameters = new MenuTreeParameters();
      $parameters->setActiveTrail([$menu_name]);
    
      $main_tree_loaded = $menu_tree_service->load($menu_name, $parameters);
      $main_tree_built = $menu_tree_service->build($main_tree_loaded);
    
      // Note that this uses laravel collections, but you could do the same with a standard array_map
      $links = collect($main_tree_built['#items'])
        ->map(function (array $item) use ($variables) {
          return [
            'link' => [
              '#type' => 'link',
              '#title' => t($item['title']),
              '#url' => $item['url'],
            ],
            'text' => t($item['title']),
            'attributes' => $item['attributes'],
          ];
        });
      $variables['links'] = $links->toArray();
    }
Production build 0.69.0 2024