You are here

function responsive_menu_off_canvas_pre_render in Responsive and off-canvas menu 8.2

Same name and namespace in other branches
  1. 8.3 responsive_menu.module \responsive_menu_off_canvas_pre_render()

Pre render callback to assemble the menu as markup.

Parameters

array $build: The render array to modify.

Return value

array The built render array.

1 string reference to 'responsive_menu_off_canvas_pre_render'
responsive_menu_page_bottom in ./responsive_menu.module
Implements hook_page_bottom().

File

./responsive_menu.module, line 210
Contains procedural code.

Code

function responsive_menu_off_canvas_pre_render(array $build) {
  $off_canvas_menus = \Drupal::config('responsive_menu.settings')
    ->get('off_canvas_menus');

  // Other modules can modify the menu names so we need to take this into
  // account when building the menu.
  \Drupal::ModuleHandler()
    ->alter('responsive_menu_off_canvas_menu_names', $off_canvas_menus);
  $menus = explode(',', $off_canvas_menus);
  $combined_tree = [];
  $menu_tree = \Drupal::menuTree();
  $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',
    ],
  ];

  // Iterate over the menus and merge them together.
  foreach ($menus as $menu_name) {
    $menu_name = trim($menu_name);
    $parameters = $menu_tree
      ->getCurrentRouteMenuTreeParameters($menu_name);

    // Force the entire tree to be build by setting expandParents to an
    // empty array.
    $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;
  }
  $menu = $menu_tree
    ->build($combined_tree);

  // Allow other modules to manipulate the built tree data.
  \Drupal::ModuleHandler()
    ->alter('responsive_menu_off_canvas_tree', $menu);
  $build['#markup'] = \Drupal::service("renderer")
    ->renderRoot($menu);
  return $build;
}