You are here

function dashboards_toolbar in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x dashboards.module \dashboards_toolbar()

Implements hook_toolbar().

File

./dashboards.module, line 54
Contains dashboards.module.

Code

function dashboards_toolbar() {
  $items['dashboards'] = [
    '#cache' => [
      'tags' => [
        'config:dashboard_list',
      ],
      'contexts' => [
        'user.permissions',
      ],
    ],
  ];
  $entityTypeManager = \Drupal::entityTypeManager();

  /**
   * @var \Drupal\dashboards\Entity\DashboardStorage
   */
  $storage = $entityTypeManager
    ->getStorage('dashboard');
  $boards = $storage
    ->loadMultipleOrderedByWeight();
  foreach ($boards as $key => $board) {
    if (!$board
      ->access('view', \Drupal::currentUser())) {
      unset($boards[$key]);
    }
  }
  if (count($boards) == 0) {
    return $items;
  }
  $boards = array_values($boards);
  $links = [];
  foreach ($boards as $board) {
    $link = $board
      ->toLink($board
      ->label())
      ->toRenderable();
    $links[] = $link;
  }
  $items['dashboards'] += [
    '#type' => 'toolbar_item',
    'tab' => [
      '#type' => 'link',
      '#title' => t('Dashboards'),
      '#url' => Url::fromRoute('<current>'),
      '#attributes' => [
        'class' => [
          'toolbar-icon',
          'toolbar-menu-administration-dashboard',
        ],
      ],
    ],
    'tray' => [
      '#heading' => t('Dashboards'),
      'dashboards' => [
        '#theme' => 'item_list',
        '#list_type' => 'ul',
        '#attributes' => [
          'class' => [
            'toolbar-menu',
          ],
        ],
        '#items' => $links,
      ],
    ],
    '#weight' => 150,
    '#attached' => [
      'library' => [
        'dashboards/core',
      ],
    ],
  ];
  return $items;
}