function ToolbarHooks::toolbar

Implements hook_toolbar().

Attributes

#[Hook('toolbar')]

File

core/modules/toolbar/src/Hook/ToolbarHooks.php, line 80

Class

ToolbarHooks
Hook implementations for toolbar.

Namespace

Drupal\toolbar\Hook

Code

public function toolbar() : array {
  // The 'Home' tab is a simple link, with no corresponding tray.
  $items['home'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => $this->t('Back to site'),
      '#url' => Url::fromRoute('<front>'),
      '#attributes' => [
        'title' => $this->t('Return to site content'),
        'class' => [
          'toolbar-icon',
          'toolbar-icon-escape-admin',
        ],
        'data-toolbar-escape-admin' => TRUE,
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'home-toolbar-tab',
      ],
    ],
    '#attached' => [
      'library' => [
        'toolbar/toolbar.escapeAdmin',
      ],
    ],
    '#weight' => -20,
  ];
  // To conserve bandwidth, we only include the top-level links in the HTML.
  // The subtrees are fetched through a JSONP script that is generated at the
  // toolbar_subtrees route. We provide the JavaScript requesting that JSONP
  // script here with the hash parameter that is needed for that route.
  // @see toolbar_subtrees_jsonp()
  [
    $hash,
    $hash_cacheability,
  ] = _toolbar_get_subtrees_hash();
  $subtrees_attached['drupalSettings']['toolbar'] = [
    'subtreesHash' => $hash,
  ];
  // The administration element has a link that is themed to correspond to a
  // toolbar tray. The tray contains the full administrative menu of the site.
  $items['administration'] = [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => $this->t('Manage'),
      '#url' => Url::fromRoute('system.admin'),
      '#attributes' => [
        'title' => $this->t('Admin menu'),
        'class' => [
          'toolbar-icon',
          'toolbar-icon-menu',
        ],
        // A data attribute that indicates to the client to defer
        // loading of the admin menu subtrees until this tab is
        // activated. Admin menu subtrees will not render to the DOM
        // if this attribute is removed. The value of the attribute is
        // intentionally left blank. Only the presence of the
        // attribute is necessary.
'data-drupal-subtrees' => '',
      ],
    ],
    'tray' => [
      '#heading' => $this->t('Administration menu'),
      '#attached' => $subtrees_attached,
      'toolbar_administration' => [
        '#pre_render' => [
          [
            ToolbarController::class,
            'preRenderAdministrationTray',
          ],
        ],
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'toolbar-menu-administration',
          ],
        ],
      ],
    ],
    '#weight' => -15,
  ];
  $hash_cacheability->applyTo($items['administration']);
  return $items;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.