本文整理汇总了PHP中Drupal::menuTree方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::menuTree方法的具体用法?PHP Drupal::menuTree怎么用?PHP Drupal::menuTree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::menuTree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: expandAll
/**
* Loads the whole menu tree.
*/
public function expandAll($tree)
{
foreach ($tree as $key => $element) {
if ($element->hasChildren && null !== $element->link && !$element->link instanceof InaccessibleMenuLink) {
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$parameters->setRoot($element->link->getPluginId())->excludeRoot()->setMaxDepth(1)->onlyEnabledLinks();
$subtree = $menu_tree->load(NULL, $parameters);
if ($subtree) {
$tree[$key]->subtree = $this->expandAll($subtree);
}
}
}
return $tree;
}
示例2: hubPage
/**
* Provide a hub page for other ea_* modules to attach to.
*/
public function hubPage()
{
$content = array();
// Include all menu children on page.
$menu_tree = \Drupal::menuTree();
$menu_name = 'hub';
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
$links = array();
if ($tree::count()) {
$children = $tree::$subtree;
foreach ($children as $child) {
$links[] = $child::$link;
}
}
drupal_set_message('Debug: ' . print_r($links, TRUE));
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
$menu_html = drupal_render($menu);
$content['title'] = array('#markup' => $this->t('Hub page for EA modules.'));
$content['menu_links'] = array('#markup' => $menu_html);
return $content;
}
示例3: doGetActiveTrailIds
/**
* Helper method for ::getActiveTrailIds().
*/
protected function doGetActiveTrailIds($menu_name)
{
// Parent ids; used both as key and value to ensure uniqueness.
// We always want all the top-level links with parent == ''.
$active_trail = array('' => '');
// If a link in the given menu indeed matches the route, then use it to
// complete the active trail.
if ($active_link = $this->getActiveLink($menu_name)) {
if ($parents = $this->menuLinkManager->getParentIds($active_link->getPluginId())) {
$active_trail = $parents + $active_trail;
}
} else {
// No matching link, so check paths against current link.
$path = $this->getCurrentPathAlias();
$menu_parameters = new MenuTreeParameters();
$tree = \Drupal::menuTree()->load($menu_name, $menu_parameters);
foreach ($tree as $menu_link_route => $menu_link) {
$menu_url = $menu_link->link->getUrlObject();
$menu_path = $menu_url->toString();
// Check if this item's path exists in the current path.
// Also check if there is a langcode prefix.
$lang_prefix = '/' . $this->language_manager->getCurrentLanguage()->getId();
if (strpos($path, $menu_path) === 0 || strpos($lang_prefix . $path, $menu_path) === 0) {
if ($this->pathIsMoreSimilar($path, $menu_path)) {
$parents = array($menu_link_route => $menu_link_route);
$active_trail = $parents + $active_trail;
}
}
}
}
return $active_trail;
}
示例4: testRediscover
/**
* Tests the rediscovering.
*/
public function testRediscover()
{
\Drupal::state()->set('menu_link_content_dynamic_route.routes', ['route_name_1' => new Route('/example-path')]);
\Drupal::service('router.builder')->rebuild();
// Set up a custom menu link pointing to a specific path.
MenuLinkContent::create(['title' => '<script>alert("Welcome to the discovered jungle!")</script>', 'link' => [['uri' => 'internal:/example-path']], 'menu_name' => 'tools'])->save();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_1', $tree_element->link->getRouteName());
// Change the underlying route and trigger the rediscovering.
\Drupal::state()->set('menu_link_content_dynamic_route.routes', ['route_name_2' => new Route('/example-path')]);
\Drupal::service('router.builder')->rebuild();
// Ensure that the new route name / parameters are captured by the tree.
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_2', $tree_element->link->getRouteName());
$title = $tree_element->link->getTitle();
$this->assertFalse($title instanceof TranslationWrapper);
$this->assertIdentical('<script>alert("Welcome to the discovered jungle!")</script>', $title);
$this->assertFalse(SafeMarkup::isSafe($title));
}
示例5: testSecondaryMenu
/**
* Tests the secondary menu.
*/
function testSecondaryMenu()
{
// Create a regular user.
$user = $this->drupalCreateUser(array());
// Log in and get the homepage.
$this->drupalLogin($user);
$this->drupalGet('<front>');
// For a logged-in user, expect the secondary menu to have links for "My
// account" and "Log out".
$link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', array(':menu_class' => 'menu', ':href' => 'user', ':text' => 'My account'));
$this->assertEqual(count($link), 1, 'My account link is in secondary menu.');
$link = $this->xpath('//ul[@class=:menu_class]/li/a[contains(@href, :href) and text()=:text]', array(':menu_class' => 'menu', ':href' => 'user/logout', ':text' => 'Log out'));
$this->assertEqual(count($link), 1, 'Log out link is in secondary menu.');
// Log out and get the homepage.
$this->drupalLogout();
$this->drupalGet('<front>');
// For a logged-out user, expect no secondary links.
$menu_tree = \Drupal::menuTree();
$tree = $menu_tree->load('account', new MenuTreeParameters());
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'));
$tree = $menu_tree->transform($tree, $manipulators);
$this->assertEqual(count($tree), 1, 'The secondary links menu contains only one menu link.');
$element = reset($tree);
$this->assertFalse($element->link->isEnabled(), 'The menu link is disabled.');
}
示例6: get
/**
* Responds to GET requests.
*
* Returns a menu tree for the specified menu name.
*
* @param int $menu_name
* The machine name of the Drupal menu.
*
* @return \Drupal\rest\ResourceResponse
* The response containing the menu tree.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function get($menu_name = NULL)
{
if ($menu_name) {
// Get menu tree resource config, set at /admin/config/services/menutree.
$config = \Drupal::config('menutree_resource.services_settings');
$services_menus = $config->get('services_menus');
// Only allow a response if the menu is in config
if (in_array($menu_name, array_filter(array_values($services_menus)))) {
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$parameters->onlyEnabledLinks();
$tree = $menu_tree->load($menu_name, $parameters);
if (!empty($tree)) {
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
$tree = $menu_tree->transform($tree, $manipulators);
$build = $menu_tree->build($tree);
// Clean the menu tree so it's ready for serialisation in a resource response.
$items = $this->clean_tree($build['#items']);
return new ResourceResponse($items);
}
throw new NotFoundHttpException(t('Menu with name @menu_name was not found', array('@menu_name' => $menu_name)));
}
throw new NotFoundHttpException(t('Menu tree @menu_name not allowed.', array('@menu_name' => $menu_name)));
}
throw new HttpException(t('No menu name was provided'));
}
示例7: getMenuItem
public static function getMenuItem($menu_name, $plugin_id)
{
$tree = \Drupal::menuTree()->load($menu_name, (new MenuTreeParameters())->onlyEnabledLinks());
// Need to review this.
// if (function_exists('i18n_menu_localize_tree')) {
// $tree = i18n_menu_localize_tree($tree);
// }
$item = self::findMenuItem($tree, $plugin_id);
return $item;
}
示例8: testOutboundPathAndRouteProcessing
/**
* Tests bubbleable metadata of menu links' outbound route/path processing.
*/
public function testOutboundPathAndRouteProcessing()
{
\Drupal::service('router.builder')->rebuild();
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
$request = Request::create('/');
$request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$request_stack->push($request);
$request_context->fromRequest($request);
$menu_tree = \Drupal::menuTree();
$renderer = \Drupal::service('renderer');
$default_menu_cacheability = (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT)->setCacheTags(['config:system.menu.tools'])->setCacheContexts(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions']);
User::create(['uid' => 1, 'name' => $this->randomString()])->save();
User::create(['uid' => 2, 'name' => $this->randomString()])->save();
// Five test cases, four asserting one outbound path/route processor, and
// together covering one of each:
// - no cacheability metadata,
// - a cache context,
// - a cache tag,
// - a cache max-age.
// Plus an additional test case to verify that multiple links adding
// cacheability metadata of the same type is working (two links with cache
// tags).
$test_cases = [['uri' => 'route:<current>', 'cacheability' => (new BubbleableMetadata())->setCacheContexts(['route'])], ['uri' => 'route:outbound_processing_test.route.csrf', 'cacheability' => (new BubbleableMetadata())->setCacheContexts(['session'])->setAttachments(['placeholders' => []])], ['uri' => 'internal:/', 'cacheability' => new BubbleableMetadata()], ['uri' => 'internal:/user/1', 'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(1)->getCacheTags())], ['uri' => 'internal:/user/2', 'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(2)->getCacheTags())]];
// Test each expectation individually.
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create(['link' => ['uri' => $expectation['uri']], 'menu_name' => 'tools']);
$menu_link_content->save();
$tree = $menu_tree->load('tools', new MenuTreeParameters());
$build = $menu_tree->build($tree);
$renderer->renderRoot($build);
$expected_cacheability = $default_menu_cacheability->merge($expectation['cacheability']);
$this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
$menu_link_content->delete();
}
// Now test them all together in one menu: the rendered menu's cacheability
// metadata should be the combination of the cacheability of all links, and
// thus of all tested outbound path & route processors.
$expected_cacheability = new BubbleableMetadata();
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create(['link' => ['uri' => $expectation['uri']], 'menu_name' => 'tools']);
$menu_link_content->save();
$expected_cacheability = $expected_cacheability->merge($expectation['cacheability']);
}
$tree = $menu_tree->load('tools', new MenuTreeParameters());
$build = $menu_tree->build($tree);
$renderer->renderRoot($build);
$expected_cacheability = $expected_cacheability->merge($default_menu_cacheability);
$this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
}
示例9: build
/**
* {@inheritdoc}
*/
public function build()
{
$depth = \Drupal::config('responsive_menu.settings')->get('horizontal_depth');
$menu_name = \Drupal::config('responsive_menu.settings')->get('horizontal_menu');
$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 = array();
$tree = $menu_tree->load($menu_name, $parameters);
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkNodeAccess'), array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
$tree = $menu_tree->transform($tree, $manipulators);
$menu = $menu_tree->build($tree);
$menu['#theme'] = 'responsive_menu_horizontal';
$output = array('#theme' => 'responsive_menu_block_wrapper', '#element_type' => \Drupal::config('responsive_menu.settings')->get('horizontal_wrapping_element'), '#content' => $menu);
// Check whether the generated breakpoint css exists and if not create it.
if (!file_exists(_get_breakpoint_css_filepath() . RESPONSIVE_MENU_BREAKPOINT_FILENAME)) {
$breakpoint = \Drupal::config('responsive_menu.settings')->get('horizontal_media_query');
responsive_menu_generate_breakpoint_css($breakpoint);
}
// Add the dynamically generated library with breakpoint styles.
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.breakpoint';
// Add the module's css file if the user does not want to disable it.
if (\Drupal::config('responsive_menu.settings')->get('include_css')) {
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.styling';
}
// 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';
}
// Add the hammerjs library if the user has requested it.
$hammerjs_setting = \Drupal::config('responsive_menu.settings')->get('hammerjs');
if ($hammerjs_setting) {
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.hammerjs';
}
// Add the javascript behaviours.
$output['#attached']['library'][] = 'responsive_menu/responsive_menu.javascript';
$output['#attached']['drupalSettings']['responsive_menu'] = array('position' => \Drupal::config('responsive_menu.settings')->get('off_canvas_position'), 'theme' => \Drupal::config('responsive_menu.settings')->get('off_canvas_theme'), 'breakpoint' => \Drupal::config('responsive_menu.settings')->get('horizontal_media_query'), 'superfish' => array('active' => \Drupal::config('responsive_menu.settings')->get('horizontal_superfish'), 'delay' => \Drupal::config('responsive_menu.settings')->get('horizontal_superfish_delay'), 'speed' => \Drupal::config('responsive_menu.settings')->get('horizontal_superfish_speed'), 'speedOut' => \Drupal::config('responsive_menu.settings')->get('horizontal_superfish_speed_out')));
$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;
return $output;
}
示例10: display_menu
/**
* Provides display_menu function for page layouts.
*
* @param Twig_Environment $env
* The twig environment instance.
* @param array $context
* An array of parameters passed to the template.
*/
public function display_menu(\Twig_Environment $env, array $context, $menu_name)
{
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
return array('#markup' => drupal_render($menu));
}
示例11: testRediscover
/**
* Tests the rediscovering.
*/
public function testRediscover()
{
\Drupal::state()->set('menu_link_content_dynamic_route.routes', ['route_name_1' => new Route('/example-path')]);
\Drupal::service('router.builder')->rebuild();
// Set up a custom menu link pointing to a specific path.
$parent = MenuLinkContent::create(['title' => '<script>alert("Welcome to the discovered jungle!")</script>', 'link' => [['uri' => 'internal:/example-path']], 'menu_name' => 'tools']);
$parent->save();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_1', $tree_element->link->getRouteName());
// Change the underlying route and trigger the rediscovering.
\Drupal::state()->set('menu_link_content_dynamic_route.routes', ['route_name_2' => new Route('/example-path')]);
\Drupal::service('router.builder')->rebuild();
// Ensure that the new route name / parameters are captured by the tree.
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertEqual('route_name_2', $tree_element->link->getRouteName());
$title = $tree_element->link->getTitle();
$this->assertFalse($title instanceof TranslatableMarkup);
$this->assertIdentical('<script>alert("Welcome to the discovered jungle!")</script>', $title);
$this->assertFalse(SafeMarkup::isSafe($title));
// Create a hierarchy.
\Drupal::state()->set('menu_link_content_dynamic_route.routes', ['route_name_1' => new Route('/example-path'), 'route_name_2' => new Route('/example-path/child')]);
$child = MenuLinkContent::create(['title' => 'Child', 'link' => [['uri' => 'entity:/example-path/child']], 'menu_name' => 'tools', 'parent' => 'menu_link_content:' . $parent->uuid()]);
$child->save();
$parent->set('link', [['uri' => 'entity:/example-path']]);
$parent->save();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertTrue($tree_element->hasChildren);
$this->assertEqual(1, count($tree_element->subtree));
// Edit child element link to use 'internal' instead of 'entity'.
$child->set('link', [['uri' => 'internal:/example-path/child']]);
$child->save();
\Drupal::service('plugin.manager.menu.link')->rebuild();
$menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
$this->assertEqual(1, count($menu_tree));
/** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
$tree_element = reset($menu_tree);
$this->assertTrue($tree_element->hasChildren);
$this->assertEqual(1, count($tree_element->subtree));
}
示例12: bootstrap_preprocess_page
/**
* Pre-processes variables for the "page" theme hook.
*
* See template for list of available variables.
*
* @see page.tpl.php
*
* @ingroup theme_preprocess
*/
function bootstrap_preprocess_page(&$variables)
{
// Add information about the number of sidebars.
$variables['content_column_attributes'] = new Attribute();
$variables['content_column_attributes']['class'] = array();
if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
$variables['content_column_attributes']['class'][] = 'col-sm-6';
} elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['sidebar_second'])) {
$variables['content_column_attributes']['class'][] = 'col-sm-9';
} else {
$variables['content_column_attributes']['class'][] = 'col-sm-12';
}
$variables['navbar_attributes'] = new Attribute();
$variables['navbar_attributes']['class'] = array('navbar');
if (bootstrap_setting('navbar_position') !== '') {
$variables['navbar_attributes']['class'][] = 'navbar-' . bootstrap_setting('navbar_position');
} else {
$variables['navbar_attributes']['class'][] = 'container';
}
if (bootstrap_setting('navbar_inverse')) {
$variables['navbar_attributes']['class'][] = 'navbar-inverse';
} else {
$variables['navbar_attributes']['class'][] = 'navbar-default';
}
// Primary nav.
$menu_tree = \Drupal::menuTree();
// Render the top-level administration menu links.
$parameters = new MenuTreeParameters();
$tree = $menu_tree->load('main', $parameters);
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
$tree = $menu_tree->transform($tree, $manipulators);
$variables['primary_nav'] = $menu_tree->build($tree);
$variables['primary_nav']['#attributes']['class'][] = 'navbar-nav';
// Primary nav.
$menu_tree = \Drupal::menuTree();
// Render the top-level administration menu links.
$parameters = new MenuTreeParameters();
$tree = $menu_tree->load('account', $parameters);
$manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'));
$tree = $menu_tree->transform($tree, $manipulators);
$variables['secondary_nav'] = $menu_tree->build($tree);
$variables['secondary_nav']['#attributes']['class'][] = 'navbar-nav';
$variables['secondary_nav']['#attributes']['class'][] = 'secondary';
}
示例13: content
public function content($menu_name) {
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
$menu_item_weight = 0;
foreach($menu['#items'] as $key => $item){
$menu_link_attributes = menu_link_attributes_get_attributes($menu['#items'][$key]['original_link']);
$menu['#items'][$key]['attributes'] = $menu_link_attributes;
$menu['#items'][$key]['url'] = str_replace('/drupal', '', $item['url']->toString());
$menu['#items'][$key]['weight'] = $menu_item_weight;
$menu_item_weight++;
if (!$menu['#items'][$key]['below']){
unset($menu['#items'][$key]['below']);
} else {
foreach($menu['#items'][$key]['below'] as $subKey => $subItem){
$menu['#items'][$key]['below'][$subKey]['url'] = str_replace('/drupal', '', $subItem['url']->toString());
}
}
}
return new JsonResponse($menu);
}
示例14: saveConfiguration
/**
* This is menu callback. Save configuration of TB Mega Menu.
*/
public function saveConfiguration()
{
$action = isset($_POST['action']) ? $_POST['action'] : NULL;
$result = '';
switch ($action) {
case 'load':
$renderable_array = TBMegaMenuBuilder::renderBlock($_POST['menu_name'], $_POST['theme']);
$result = \Drupal::service('renderer')->render($renderable_array)->__toString();
break;
case 'save':
$menu_config = isset($_POST['menu_config']) ? $_POST['menu_config'] : NULL;
$block_config = isset($_POST['block_config']) ? $_POST['block_config'] : NULL;
$menu_name = isset($_POST['menu_name']) ? $_POST['menu_name'] : NULL;
$theme = isset($_POST['theme']) ? $_POST['theme'] : NULL;
if ($menu_config && $menu_name) {
// This is parameter to load menu_tree with the enabled links.
$menu_tree_parameters = (new MenuTreeParameters())->onlyEnabledLinks();
// Load menu items with condition.
$menu_items = \Drupal::menuTree()->load($menu_name, $menu_tree_parameters);
// Sync mega menu before store.
TBMegaMenuBuilder::syncConfigAll($menu_items, $menu_config, 'backend');
TBMegaMenuBuilder::syncOrderMenus($menu_config);
$result = \Drupal::service('database')->merge('tb_megamenus')->key(array('menu_name' => $menu_name, 'theme' => $theme))->fields(array('block_config' => json_encode($block_config), 'menu_config' => json_encode($menu_config)))->execute();
}
break;
case 'load_block':
$block_id = isset($_POST['block_id']) ? $_POST['block_id'] : NULL;
$id = isset($_POST['id']) ? $_POST['id'] : NULL;
$showblocktitle = isset($_POST['showblocktitle']) ? $_POST['showblocktitle'] : NULL;
if ($block_id) {
$render = array('#theme' => 'tb_megamenu_block', '#block_id' => $block_id, '#section' => 'backend', '#showblocktitle' => $showblocktitle);
$content = \Drupal::service('renderer')->render($render)->__toString();
$result = json_encode(array('content' => $content, 'id' => $id));
}
break;
default:
break;
}
return new Response($result);
}
示例15: get
/**
* @param null $menu_name
* @return ResourceResponse
*/
public function get($menu_name = null) {
$menu_tree = \Drupal::menuTree( );
$generator = \Drupal::urlGenerator();
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, new \Drupal\Core\Menu\MenuTreeParameters());
// Transform the tree using the manipulators you want.
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
foreach ($tree as $element) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $element->link;
$link_param = $link->pluginDefinition['route_parameters'];
// echo "<pre>" . print_r($link->pluginDefinition, true) . "</pre>";
$path = $generator->getPathFromRoute($link->getRouteName(), $link_param);
$menu[$link->getRouteName()]['title'] = $link->getTitle();
$menu[$link->getRouteName()]['url'] = $path;
if ($element->subtree) {
$subtree = $menu_tree->build($element->subtree);
foreach ($subtree['#items'] as $key => $value) {
// print_r($value['url']->getRouteParameters());
//$path = $generator->getPathFromRoute($key, $value['url']->getRouteParameters());
$menu[$key]['title'] = $value['title'];
$menu[$key]['url'] = $path;
}
}
}
return new ResourceResponse($menu);
}