You are here

function shortcut_renderable_links in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/shortcut/shortcut.module \shortcut_renderable_links()
  2. 7 modules/shortcut/shortcut.module \shortcut_renderable_links()

Returns an array of shortcut links, suitable for rendering.

Parameters

\Drupal\shortcut\ShortcutSetInterface $shortcut_set: (optional) An object representing the set whose links will be displayed. If not provided, the user's current set will be displayed.

Return value

\Drupal\shortcut\ShortcutInterface[] An array of shortcut links, in the format returned by the menu system.

2 calls to shortcut_renderable_links()
ShortcutLazyBuilders::lazyLinks in core/modules/shortcut/src/ShortcutLazyBuilders.php
#lazy_builder callback; builds shortcut toolbar links.
ShortcutsBlock::build in core/modules/shortcut/src/Plugin/Block/ShortcutsBlock.php
Builds and returns the renderable array for this block plugin.

File

core/modules/shortcut/shortcut.module, line 199
Allows users to manage customizable lists of shortcut links.

Code

function shortcut_renderable_links($shortcut_set = NULL) {
  $shortcut_links = [];
  if (!isset($shortcut_set)) {
    $shortcut_set = shortcut_current_displayed_set();
  }
  $cache_tags = [];
  foreach ($shortcut_set
    ->getShortcuts() as $shortcut) {
    $shortcut = \Drupal::service('entity.repository')
      ->getTranslationFromContext($shortcut);
    $url = $shortcut
      ->getUrl();
    if ($url
      ->access()) {
      $links[$shortcut
        ->id()] = [
        'type' => 'link',
        'title' => $shortcut
          ->label(),
        'url' => $shortcut
          ->getUrl(),
      ];
      $cache_tags = Cache::mergeTags($cache_tags, $shortcut
        ->getCacheTags());
    }
  }
  if (!empty($links)) {
    $shortcut_links = [
      '#theme' => 'links__toolbar_shortcuts',
      '#links' => $links,
      '#attributes' => [
        'class' => [
          'toolbar-menu',
        ],
      ],
      '#cache' => [
        'tags' => $cache_tags,
      ],
    ];
  }
  return $shortcut_links;
}