/** * {@inheritdoc} */ public function build() { $menu_name = $this->getDerivativeId(); $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); $tree = $this->menuTree->load($menu_name, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); return $this->menuTree->build($tree); }
/** * Build the mega menu link/content tree. * * @param MegaMenuInterface $mega_menu * * @return array */ private function buildMegaMenuTree(MegaMenuInterface $mega_menu) { $tree = $this->loadMenuTree($mega_menu->getTargetMenu()); $build = $this->menuLinkTree->build($tree); $build['#mega_menu'] = $mega_menu; $cacheability = CacheableMetadata::createFromRenderArray($build); $cacheability->addCacheableDependency($mega_menu); // Add content from the mega menus to the link tree. foreach ($build['#items'] as $item_key => $item) { $safe_item_key = str_replace('.', '_', $item_key); $layout = $mega_menu->getLinkLayout($safe_item_key); if ($layout === MegaMenuInterface::NO_LAYOUT) { continue; } $build['#items'][$item_key]['attributes']['data-mega-menu-content-target'] = $item_key; /** @var LayoutInterface $layout_plugin */ $layout_plugin = $this->layoutPluginManager->createInstance($layout); $plugin_definition = $layout_plugin->getPluginDefinition(); // Build an array of the region names in the right order. $empty = array_fill_keys(array_keys($plugin_definition['region_names']), []); $full = $mega_menu->getBlocksByLink($safe_item_key)->getAllByRegion(); // Merge it with the actual values to maintain the ordering. $block_assignments = array_intersect_key(array_merge($empty, $full), $empty); $build['#items'][$item_key]['content'] = ['#prefix' => '<div data-mega-menu-content="' . $item_key . '" class="mega-menu-content">', '#suffix' => '</div>', '#theme' => $plugin_definition['theme'], '#settings' => [], '#layout' => $plugin_definition]; if (isset($plugin_definition['library'])) { $build['#items'][$item_key]['content']['#attached']['library'][] = $plugin_definition['library']; } foreach ($block_assignments as $region => $blocks) { $build['#items'][$item_key]['content'][$region] = []; /** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */ foreach ($blocks as $block_id => $block) { if ($block instanceof ContextAwarePluginInterface) { $contexts = $this->contextRepository->getRuntimeContexts($block->getContextMapping()); $this->contextHandler->applyContextMapping($block, $contexts); } // Make sure the user is allowed to view the block. $access = $block->access($this->account, TRUE); $cacheability->addCacheableDependency($access); // If the user is not allowed then do not render the block. if (!$access->isAllowed()) { continue; } $configuration = $block->getConfiguration(); // Create the render array for the block as a whole. // @see template_preprocess_block(). $block_build = ['#theme' => 'block', '#attributes' => [], '#weight' => $configuration['weight'], '#configuration' => $configuration, '#plugin_id' => $block->getPluginId(), '#base_plugin_id' => $block->getBaseId(), '#derivative_plugin_id' => $block->getDerivativeId(), '#block_plugin' => $block, '#pre_render' => [[$this, 'preRenderBlock']], '#cache' => ['keys' => ['mega_menu', $mega_menu->id(), 'block', $block_id], 'tags' => Cache::mergeTags($mega_menu->getCacheTags(), $block->getCacheTags()), 'contexts' => $block->getCacheContexts(), 'max-age' => $block->getCacheMaxAge()]]; $build['#items'][$item_key]['content'][$region][$block_id] = $block_build; $cacheability->addCacheableDependency($block); } } } $cacheability->applyTo($build); return $build; }
/** * {@inheritdoc} */ public function build() { $menu_name = $this->getDerivativeId(); $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); // Adjust the menu tree parameters based on the block's configuration. $level = $this->configuration['level']; $depth = $this->configuration['depth']; $parameters->setMinDepth($level); // When the depth is configured to zero, there is no depth limit. When depth // is non-zero, it indicates the number of levels that must be displayed. // Hence this is a relative depth that we must convert to an actual // (absolute) depth, that may never exceed the maximum depth. if ($depth > 0) { $parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth())); } $tree = $this->menuTree->load($menu_name, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); return $this->menuTree->build($tree); }