function NavigationShortcutsBlock::build

Same name and namespace in other branches
  1. 10 core/modules/navigation/src/Plugin/Block/NavigationShortcutsBlock.php \Drupal\navigation\Plugin\Block\NavigationShortcutsBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

File

core/modules/navigation/src/Plugin/Block/NavigationShortcutsBlock.php, line 68

Class

NavigationShortcutsBlock
Defines a shortcuts navigation block class.

Namespace

Drupal\navigation\Plugin\Block

Code

public function build() : array {
  // This navigation block requires shortcut module. Once the plugin is moved
  // to the module, this should not be necessary.
  if (!$this->moduleHandler
    ->moduleExists('shortcut')) {
    return [];
  }
  return [
    'shortcuts' => [
      // @phpstan-ignore-next-line
'#lazy_builder' => [
        'navigation.shortcut_lazy_builder:lazyLinks',
        [
          $this->configuration['label'],
        ],
      ],
      '#create_placeholder' => TRUE,
      '#cache' => [
        'keys' => [
          'shortcut_set_navigation_links',
        ],
        'contexts' => [
          'user',
        ],
      ],
      '#lazy_builder_preview' => [
        [
          '#theme' => 'navigation_menu',
          '#menu_name' => 'shortcuts',
          '#title' => $this->configuration['label'],
          '#items' => [
            [
              'title' => $this->configuration['label'],
              'class' => 'shortcuts',
              'icon' => [
                'icon_id' => 'shortcuts',
              ],
            ],
          ],
        ],
      ],
    ],
  ];
}

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