class ShortcutNavigationBlock

Same name and namespace in other branches
  1. 11.x core/modules/shortcut/src/Plugin/Block/ShortcutNavigationBlock.php \Drupal\shortcut\Plugin\Block\ShortcutNavigationBlock

Defines a shortcuts navigation block class.

@internal

Attributes

#[Block(id: 'navigation_shortcuts', admin_label: new TranslatableMarkup('Navigation Shortcuts'))]

Hierarchy

  • class \Drupal\shortcut\Plugin\Block\ShortcutNavigationBlock implements \Drupal\Core\Plugin\ContainerFactoryPluginInterface extends \Drupal\Core\Block\BlockBase

Expanded class hierarchy of ShortcutNavigationBlock

1 file declares its use of ShortcutNavigationBlock
NavigationShortcutsBlock.php in core/modules/navigation/src/Plugin/Block/NavigationShortcutsBlock.php

File

core/modules/shortcut/src/Plugin/Block/ShortcutNavigationBlock.php, line 21

Namespace

Drupal\shortcut\Plugin\Block
View source
class ShortcutNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface {
  
  /**
   * Constructs a new ShortcutsNavigationBlock.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected ModuleHandlerInterface $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }
  
  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) : AccessResultInterface {
    return AccessResult::allowedIfHasPermission($account, 'access shortcuts');
  }
  
  /**
   * {@inheritdoc}
   */
  public function build() : array {
    if (!$this->moduleHandler
      ->moduleExists('navigation')) {
      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.