View source
<?php
namespace Drupal\responsive_menu;
use Drupal\Core\Security\TrustedCallbackInterface;
class OffCanvas implements TrustedCallbackInterface {
public static function trustedCallbacks() {
return [
'preRender',
];
}
public static function preRender(array $build) {
$off_canvas_menus = \Drupal::config('responsive_menu.settings')
->get('off_canvas_menus');
\Drupal::ModuleHandler()
->alter('responsive_menu_off_canvas_menu_names', $off_canvas_menus);
$menus = explode(',', $off_canvas_menus);
$combined_tree = [];
$menu_tree = \Drupal::menuTree();
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
],
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
foreach ($menus as $menu_name) {
$menu_name = trim($menu_name);
$parameters = $menu_tree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters->expandedParents = [];
$tree_items = $menu_tree
->load($menu_name, $parameters);
$tree_manipulated = $menu_tree
->transform($tree_items, $manipulators);
$combined_tree = array_merge($combined_tree, $tree_manipulated);
$build['#cache']['contexts'][] = 'route.menu_active_trails:' . $menu_name;
$build['#cache']['tags'][] = 'config:system.menu.' . $menu_name;
$build['#cache']['tags'][] = 'offcanvas_render';
}
$menu = $menu_tree
->build($combined_tree);
\Drupal::ModuleHandler()
->alter('responsive_menu_off_canvas_tree', $menu);
$build['#markup'] = \Drupal::service("renderer")
->renderRoot($menu);
return $build;
}
}