class NavigationMenuBlock
Same name and namespace in other branches
- 11.x core/modules/navigation/src/Plugin/Block/NavigationMenuBlock.php \Drupal\navigation\Plugin\Block\NavigationMenuBlock
Provides a generic menu navigation block.
@internal
Hierarchy
- class \Drupal\navigation\Plugin\Block\NavigationMenuBlock
Expanded class hierarchy of NavigationMenuBlock
2 files declare their use of NavigationMenuBlock
- NavigationMenuBlockTest.php in core/
modules/ navigation/ tests/ src/ Kernel/ NavigationMenuBlockTest.php - NavigationMenuMarkupTest.php in core/
modules/ navigation/ tests/ src/ Kernel/ NavigationMenuMarkupTest.php
File
-
core/
modules/ navigation/ src/ Plugin/ Block/ NavigationMenuBlock.php, line 21
Namespace
Drupal\navigation\Plugin\BlockView source
final class NavigationMenuBlock extends SystemMenuBlock implements ContainerFactoryPluginInterface {
const NAVIGATION_MAX_DEPTH = 3;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('navigation.menu_tree'), $container->get('menu.active_trail'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() : array {
return [
'level' => 1,
'depth' => 0,
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) : array {
$form = parent::blockForm($form, $form_state);
unset($form['menu_levels']['expand_all_items']);
$form['menu_levels']['depth']['#options'] = range(1, static::NAVIGATION_MAX_DEPTH);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) : void {
$this->configuration['level'] = $form_state->getValue('level');
$this->configuration['depth'] = $form_state->getValue('depth');
}
/**
* {@inheritdoc}
*/
public function build() : array {
$menu_name = $this->getDerivativeId();
$level = $this->configuration['level'];
$depth = $this->configuration['depth'];
$parameters = new MenuTreeParameters();
$parameters->setMinDepth($level)
->setMaxDepth(min($level + $depth, $this->menuTree
->maxDepth()))
->onlyEnabledLinks();
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
$build = $this->menuTree
->build($tree);
if (!empty($build)) {
$build['#title'] = $this->configuration['label'];
}
return $build;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [
'module' => [
'system',
],
];
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() : array {
// We don't use menu active trails here.
return array_filter(parent::getCacheContexts(), static fn(string $tag) => !str_starts_with($tag, 'route.menu_active_trails'));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
NavigationMenuBlock::blockForm | public | function | |
NavigationMenuBlock::blockSubmit | public | function | |
NavigationMenuBlock::build | public | function | Builds and returns the renderable array for this block plugin. |
NavigationMenuBlock::calculateDependencies | public | function | |
NavigationMenuBlock::create | public static | function | Creates an instance of the plugin. |
NavigationMenuBlock::defaultConfiguration | public | function | |
NavigationMenuBlock::getCacheContexts | public | function | |
NavigationMenuBlock::NAVIGATION_MAX_DEPTH | constant |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.