public function HorizontalMenu::build in Responsive and off-canvas menu 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
- 4.4.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
- 4.0.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
- 4.1.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
- 4.3.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::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
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ HorizontalMenu.php, line 61
Class
- HorizontalMenu
- Provides the HorizontalMenu block.
Namespace
Drupal\responsive_menu\Plugin\BlockCode
public function build() {
$depth = \Drupal::config('responsive_menu.settings')
->get('horizontal_depth');
$menu_name = \Drupal::config('responsive_menu.settings')
->get('horizontal_menu');
// Allow other modules to modify the menu name.
\Drupal::ModuleHandler()
->alter('responsive_menu_horizontal_menu_name', $menu_name);
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters
->setMaxDepth($depth);
// Force the entire tree to be build be setting expandParents to an
// empty array.
$parameters->expandedParents = [];
$tree = $menu_tree
->load($menu_name, $parameters);
$manipulators = [
// Show links to nodes that are accessible for the current user.
[
'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
],
// Only show links that are accessible for the current user.
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
// Use the default sorting of menu links.
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $menu_tree
->transform($tree, $manipulators);
$menu = $menu_tree
->build($tree);
// Allow other modules to manipulate the built tree data.
\Drupal::ModuleHandler()
->alter('responsive_menu_horizontal_tree', $menu);
$menu['#theme'] = 'responsive_menu_horizontal';
$output = [
'#theme' => 'responsive_menu_block_wrapper',
'#element_type' => \Drupal::config('responsive_menu.settings')
->get('horizontal_wrapping_element'),
'#content' => $menu,
];
// Add the superfish library if the user has requested it.
$superfish_setting = \Drupal::config('responsive_menu.settings')
->get('horizontal_superfish');
if ($superfish_setting) {
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.superfish';
}
// Add superfish's hoverIntent library if the user has requested it.
if ($superfish_setting && \Drupal::config('responsive_menu.settings')
->get('horizontal_superfish_hoverintent')) {
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.superfish_hoverintent';
}
$media_query = \Drupal::config('responsive_menu.settings')
->get('horizontal_media_query');
// Attempt to clean up a media query in case it isn't properly enclosed in
// brackets.
$media_query = preg_replace('/^(min|max)(.+?)$/', '($1$2)', $media_query);
$output['#attached']['drupalSettings']['responsive_menu']['mediaQuery'] = $media_query;
// Add a contextual link to edit the menu.
$output['#contextual_links']['menu'] = [
'route_parameters' => [
'menu' => $menu_name,
],
];
return $output;
}