/** * Tests creating links with an expected tree structure. */ public function testCreateLinksInMenu() { // This creates a tree with the following structure: // - 1 // - 2 // - 3 // - 4 // - 5 // - 7 // - 6 // - 8 // With link 6 being the only external link. $links = array(1 => MenuLinkMock::create(array('id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '')), 2 => MenuLinkMock::create(array('id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => 'test.example1', 'route_parameters' => array('foo' => 'bar'))), 3 => MenuLinkMock::create(array('id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'route_parameters' => array('baz' => 'qux'))), 4 => MenuLinkMock::create(array('id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3')), 5 => MenuLinkMock::create(array('id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '')), 6 => MenuLinkMock::create(array('id' => 'test.example6', 'route_name' => '', 'url' => 'https://www.drupal.org/', 'title' => 'barbar', 'parent' => '')), 7 => MenuLinkMock::create(array('id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => '')), 8 => MenuLinkMock::create(array('id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => ''))); foreach ($links as $instance) { $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition()); } $parameters = new MenuTreeParameters(); $tree = $this->linkTree->load('mock', $parameters); $count = function (array $tree) { $sum = function ($carry, MenuLinkTreeElement $item) { return $carry + $item->count(); }; return array_reduce($tree, $sum); }; $this->assertEqual($count($tree), 8); $parameters = new MenuTreeParameters(); $parameters->setRoot('test.example2'); $tree = $this->linkTree->load($instance->getMenuName(), $parameters); $top_link = reset($tree); $this->assertEqual(count($top_link->subtree), 1); $child = reset($top_link->subtree); $this->assertEqual($child->link->getPluginId(), $links[3]->getPluginId()); $height = $this->linkTree->getSubtreeHeight('test.example2'); $this->assertEqual($height, 3); }
/** * Create menu entries associate with the vocabulary of this term. * * @param \Drupal\taxonomy\TermInterface $term */ public function generateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) { // Load relevant taxonomy menus. $tax_menus = $this->getTermMenusByVocabulary($term->getVocabularyId()); foreach ($tax_menus as $menu) { foreach ($menu->getLinks([], TRUE) as $plugin_id => $plugin_def) { if (!$rebuild_all) { $plugin_id_parts = explode('.', $plugin_id); $term_id = array_pop($plugin_id_parts); if ($term->id() != $term_id) { continue; } } if ($this->manager->hasDefinition($plugin_id)) { $this->manager->updateDefinition($plugin_id, $plugin_def); } else { $this->manager->addDefinition($plugin_id, $plugin_def); } } } }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installSchema('system', 'sequences'); $this->installEntitySchema('user'); $this->installSchema('system', array('router')); $this->installEntitySchema('menu_link_content'); $account = User::create(['name' => $this->randomMachineName(), 'status' => 1]); $account->save(); $this->container->get('current_user')->setAccount($account); $this->menuLinkManager = $this->container->get('plugin.manager.menu.link'); $this->linkTree = $this->container->get('menu.link_tree'); $this->blockManager = $this->container->get('plugin.manager.block'); $routes = new RouteCollection(); $requirements = array('_access' => 'TRUE'); $options = array('_access_checks' => array('access_check.default')); $routes->add('example1', new Route('/example1', array(), $requirements, $options)); $routes->add('example2', new Route('/example2', array(), $requirements, $options)); $routes->add('example3', new Route('/example3', array(), $requirements, $options)); $routes->add('example4', new Route('/example4', array(), $requirements, $options)); $routes->add('example5', new Route('/example5', array(), $requirements, $options)); $routes->add('example6', new Route('/example6', array(), $requirements, $options)); $routes->add('example7', new Route('/example7', array(), $requirements, $options)); $routes->add('example8', new Route('/example8', array(), $requirements, $options)); $mock_route_provider = new MockRouteProvider($routes); $this->container->set('router.route_provider', $mock_route_provider); // Add a new custom menu. $menu_name = 'mock'; $label = $this->randomMachineName(16); $this->menu = entity_create('menu', array('id' => $menu_name, 'label' => $label, 'description' => 'Description text')); $this->menu->save(); // This creates a tree with the following structure: // - 1 // - 2 // - 3 // - 4 // - 5 // - 7 // - 6 // - 8 // With link 6 being the only external link. $links = array(1 => MenuLinkMock::create(array('id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '', 'weight' => 0)), 2 => MenuLinkMock::create(array('id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => '', 'route_parameters' => array('foo' => 'bar'), 'weight' => 1)), 3 => MenuLinkMock::create(array('id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'weight' => 2)), 4 => MenuLinkMock::create(array('id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3', 'weight' => 3)), 5 => MenuLinkMock::create(array('id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '', 'expanded' => TRUE, 'weight' => 4)), 6 => MenuLinkMock::create(array('id' => 'test.example6', 'route_name' => '', 'url' => 'https://drupal.org/', 'title' => 'barbar', 'parent' => '', 'weight' => 5)), 7 => MenuLinkMock::create(array('id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => 'test.example5', 'weight' => 6)), 8 => MenuLinkMock::create(array('id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => '', 'weight' => 7))); foreach ($links as $instance) { $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition()); } }