2

Question: How to define a custom menu and show it like an admin menu in Drupal 8 ?

Goal:

First of all, we have a custom administration theme where we are looking for a custom toolbar solution that stays visible for an "simple" user on the front and backend when logged in.

There is a need for a toolbar at top of page similar like the toolbar module in Drupal 8 core.

We want the following links shows to a user :

  • Back to site
  • User with possibility to view and edit their profile
  • Logout
  • The Edit option for edit content from the front of the site

Screenshot of the goal :

enter image description here

Research:

I have found there are several solutions for Drupal 7 with modules like Admin menu source, Webmaster menu and with Block Class styles module. But none of them is compatible for using in Drupal 8.

Expected solution:

This custom toolbar should only be for specific user roles("user") showed. I will do this by creating an custom module but I'm not really sure if I need to inherit from the toolbar core module in Drupal 8.

  • A complete new toolbar

    OR

  • I can work further on the default toolbar module in Drupal 8 but there is a need for 2 things: add item "boutique" as example that is decribed as an answer but also remove an item from the default toolbar like Devel or Manage tab included the sub links from these items.

    Are the two things together not possible then I prefer for creating a completely new toolbar module from scratch for Drupal 8.

Recap:

  • Not hide or show items based on permissions
  • Not just adding items to the default drupal 8 toolbar module

Is that possible ? Are they better/other methods for achieving this goal ?

1

This is how i add custom menu :

In a custom module :

xxxx.module :

/**
 * Implements hook_toolbar().
 */
function xxxx_toolbar() {
    $items = [];

    $items['Boutique'] = [
        '#cache' => [
            'contexts' => ['user.permissions'],
        ],
    ];

    if (!\Drupal::currentUser()->hasPermission('Access the Commande overview page')) {
        return $items;
    }


    $items['Boutique'] += [
        '#type' => 'toolbar_item',
        '#weight' => 1,
        'tab' => [
            '#type' => 'link',
            '#title' => 'Boutique',
            '#url' => \Drupal\Core\Url::fromUri("internal:/admin/boutique/commandes"),
            '#attributes' => [
                'title' => 'Boutique menu',
                'class' => ['toolbar-icon', 'toolbar-icon-boutique'],
            ],
        ],
        'tray' => [

            'configuration' => [
                '#type' => 'link',
                '#title' => 'Commandes',
                '#url' => \Drupal\Core\Url::fromUri("internal:/admin/boutique/commandes"),

            ],[
                '#type' => 'link',
                '#title' => 'Livraison',
                '#url' => \Drupal\Core\Url::fromUri("internal:/admin/boutique/livraison"),

            ],
            [
                '#type' => 'link',
                '#title' => 'Configuration',
                '#url' => \Drupal\Core\Url::fromUri("internal:/admin/boutique/configuration"),

            ],

        ],
        '#attached' => array(
            'library' => array(
                'boutique/admin',
            ),
        ),

    ];
    return $items;
}

And the result :

enter image description here

You can manage access with :

if (!\Drupal::currentUser()->hasPermission('Access the Commande overview page')) { return $items; }

in the xxxx.module like me.

0

I think that u can do this by adding permission for Toolbar on specific roles and ofc permissions for editing content.

  • No, the Toolbar permission is to show and hide the toolbar. I want a minified version of the Toolbar on specific user roles, not the whole default toolbar showing or hidden. – Antoon Verroken May 8 '17 at 15:26
  • Yes, but they will only see the options that is allowed in permissions, not the whole admin menu. i.stack.imgur.com/eJ4G1.png i.stack.imgur.com/T0H64.png – StevanRS May 9 '17 at 8:12
  • The options must remain available for the user, just not visible in the toolbar because we've built a custom admin theme, so we can't disallow the functionality. – Antoon Verroken May 9 '17 at 9:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.