/** * {@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); }
/** * @return array * Array containing Devel Menu links */ protected function develMenuLinks() { $parameters = new MenuTreeParameters(); $parameters->setMaxDepth(1)->onlyEnabledLinks(); $tree = $this->menuLinkTree->load('devel', $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuLinkTree->transform($tree, $manipulators); $links = array(); foreach ($tree as $item) { if ($item->access->isAllowed()) { $links[] = array('title' => $item->link->getTitle(), 'description' => $item->link->getDescription(), 'url' => $item->link->getUrlObject()->toString()); } } return $links; }
/** * Provide a single block on the administration overview page. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu item to be displayed. * * @return array * An array of menu items, as expected by admin-block-content.html.twig. */ public function getAdminBlock(MenuLinkInterface $instance) { $content = array(); // Only find the children of this link. $link_id = $instance->getPluginId(); $parameters = new MenuTreeParameters(); $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks(); $tree = $this->menuTree->load(NULL, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); foreach ($tree as $key => $element) { // Only render accessible links. if (!$element->access->isAllowed()) { // @todo Bubble cacheability metadata of both accessible and // inaccessible links. Currently made impossible by the way admin // blocks are rendered. continue; } /** @var $link \Drupal\Core\Menu\MenuLinkInterface */ $link = $element->link; $content[$key]['title'] = $link->getTitle(); $content[$key]['options'] = $link->getOptions(); $content[$key]['description'] = $link->getDescription(); $content[$key]['url'] = $link->getUrlObject(); } ksort($content); return $content; }
/** * Provide the administration overview page. * * @param string $link_id * The ID of the administrative path link for which to display child links. * * @return array * A renderable array of the administration overview page. */ public function overview($link_id) { // Check for status report errors. if ($this->systemManager->checkRequirements() && $this->currentUser()->hasPermission('administer site configuration')) { drupal_set_message($this->t('One or more problems were detected with your Drupal installation. Check the <a href="@status">status report</a> for more information.', array('@status' => $this->url('system.status'))), 'error'); } // Load all menu links below it. $parameters = new MenuTreeParameters(); $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->onlyEnabledLinks(); $tree = $this->menuLinkTree->load(NULL, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuLinkTree->transform($tree, $manipulators); $blocks = array(); foreach ($tree as $key => $element) { $link = $element->link; $block['title'] = $link->getTitle(); $block['description'] = $link->getDescription(); $block['content'] = array('#theme' => 'admin_block_content', '#content' => $this->systemManager->getAdminBlock($link)); if (!empty($block['content']['#content'])) { $blocks[$key] = $block; } } if ($blocks) { ksort($blocks); return array('#theme' => 'admin_page', '#blocks' => $blocks); } else { return array('#markup' => $this->t('You do not have any administrative items.')); } }
/** * Form constructor to edit an entire menu tree at once. * * Shows for one menu the menu links accessible to the current user and * relevant operations. * * This form constructor can be integrated as a section into another form. It * relies on the following keys in $form_state: * - menu: A menu entity. * - menu_overview_form_parents: An array containing the parent keys to this * form. * Forms integrating this section should call menu_overview_form_submit() from * their form submit handler. */ protected function buildOverviewForm(array &$form, FormStateInterface $form_state) { // Ensure that menu_overview_form_submit() knows the parents of this form // section. $form['#tree'] = TRUE; $form['#theme'] = 'menu_overview_form'; if (!$form_state->has('menu_overview_form_parents')) { $form_state->set('menu_overview_form_parents', []); } $form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms'; $tree = $this->menuTree->load($this->entity->id(), new MenuTreeParameters()); // We indicate that a menu administrator is running the menu access check. $this->getRequest()->attributes->set('_menu_admin', TRUE); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); $this->getRequest()->attributes->set('_menu_admin', FALSE); // Determine the delta; the number of weights to be made available. $count = function (array $tree) { $sum = function ($carry, MenuLinkTreeElement $item) { return $carry + $item->count(); }; return array_reduce($tree, $sum); }; $delta = max($count($tree), 50); $form = array_merge($form, $this->buildOverviewTreeForm($tree, $delta)); $destination = $this->getUrlGenerator()->getPathFromRoute('entity.menu.edit_form', array('menu' => $this->entity->id())); $url = $destination = $this->url('entity.menu.add_link_form', array('menu' => $this->entity->id()), array('query' => array('destination' => $destination))); $form['#empty_text'] = $this->t('There are no menu links yet. <a href="@url">Add link</a>.', array('@url' => $url)); return $form; }
/** * Returns the maximum depth of the possible parents of the menu link. * * @param string $id * The menu link plugin ID or an empty value for a new link. * * @return int * The depth related to the depth of the given menu link. */ protected function getParentDepthLimit($id) { if ($id) { $limit = $this->menuLinkTree->maxDepth() - $this->menuLinkTree->getSubtreeHeight($id); } else { $limit = $this->menuLinkTree->maxDepth() - 1; } return $limit; }
/** * Responds to GET requests. * * Returns the menu structure for the given menu name. * * @param string $menu_name * The menu name. * * @return \Drupal\rest\ResourceResponse * The response containing the log entry. * * @TODO: make $menu parameter a type-hinted MenuInterface parameter * @TODO: cacheability and access checks for menu links? * @TODO: optional whitelist to only allow menu links from defined providers. */ public function get($menu_name) { $menu = Menu::load($menu_name); if ($menu) { $tree = $this->menuTree->load($menu->id(), new MenuTreeParameters()); $result = array_values($tree); return new ResourceResponse($result); } throw new HttpException(t('No valid menu ID was provided.')); }
/** * Form constructor to edit an entire menu tree at once. * * Shows for one menu the menu links accessible to the current user and * relevant operations. * * This form constructor can be integrated as a section into another form. It * relies on the following keys in $form_state: * - menu: A menu entity. * - menu_overview_form_parents: An array containing the parent keys to this * form. * Forms integrating this section should call menu_overview_form_submit() from * their form submit handler. */ protected function buildOverviewForm(array &$form, FormStateInterface $form_state) { // Ensure that menu_overview_form_submit() knows the parents of this form // section. if (!$form_state->has('menu_overview_form_parents')) { $form_state->set('menu_overview_form_parents', []); } $form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms'; $tree = $this->menuTree->load($this->entity->id(), new MenuTreeParameters()); // We indicate that a menu administrator is running the menu access check. $this->getRequest()->attributes->set('_menu_admin', TRUE); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); $this->getRequest()->attributes->set('_menu_admin', FALSE); // Determine the delta; the number of weights to be made available. $count = function (array $tree) { $sum = function ($carry, MenuLinkTreeElement $item) { return $carry + $item->count(); }; return array_reduce($tree, $sum); }; $delta = max($count($tree), 50); $form['links'] = array('#type' => 'table', '#theme' => 'table__menu_overview', '#header' => array($this->t('Menu link'), array('data' => $this->t('Enabled'), 'class' => array('checkbox')), $this->t('Weight'), array('data' => $this->t('Operations'), 'colspan' => 3)), '#attributes' => array('id' => 'menu-overview'), '#tabledrag' => array(array('action' => 'match', 'relationship' => 'parent', 'group' => 'menu-parent', 'subgroup' => 'menu-parent', 'source' => 'menu-id', 'hidden' => TRUE, 'limit' => \Drupal::menuTree()->maxDepth() - 1), array('action' => 'order', 'relationship' => 'sibling', 'group' => 'menu-weight'))); $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], ['query' => ['destination' => $this->entity->url('edit-form')]])]); $links = $this->buildOverviewTreeForm($tree, $delta); foreach (Element::children($links) as $id) { if (isset($links[$id]['#item'])) { $element = $links[$id]; $form['links'][$id]['#item'] = $element['#item']; // TableDrag: Mark the table row as draggable. $form['links'][$id]['#attributes'] = $element['#attributes']; $form['links'][$id]['#attributes']['class'][] = 'draggable'; $form['links'][$id]['#item'] = $element['#item']; // TableDrag: Sort the table row according to its existing/configured weight. $form['links'][$id]['#weight'] = $element['#item']->link->getWeight(); // Add special classes to be used for tabledrag.js. $element['parent']['#attributes']['class'] = array('menu-parent'); $element['weight']['#attributes']['class'] = array('menu-weight'); $element['id']['#attributes']['class'] = array('menu-id'); $form['links'][$id]['title'] = array(array('#theme' => 'indentation', '#size' => $element['#item']->depth - 1), $element['title']); $form['links'][$id]['enabled'] = $element['enabled']; $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array('checkbox', 'menu-enabled'); $form['links'][$id]['weight'] = $element['weight']; // Operations (dropbutton) column. $form['links'][$id]['operations'] = $element['operations']; $form['links'][$id]['id'] = $element['id']; $form['links'][$id]['parent'] = $element['parent']; } } return $form; }
/** * {@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); }
/** * Provide a single block on the administration overview page. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The menu item to be displayed. * * @return array * An array of menu items, as expected by theme_admin_block_content(). */ public function getAdminBlock(MenuLinkInterface $instance) { $content = array(); // Only find the children of this link. $link_id = $instance->getPluginId(); $parameters = new MenuTreeParameters(); $parameters->setRoot($link_id)->excludeRoot()->setTopLevelOnly()->excludeHiddenLinks(); $tree = $this->menuTree->load(NULL, $parameters); $manipulators = array(array('callable' => 'menu.default_tree_manipulators:checkAccess'), array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort')); $tree = $this->menuTree->transform($tree, $manipulators); foreach ($tree as $key => $element) { /** @var $link \Drupal\Core\Menu\MenuLinkInterface */ $link = $element->link; $content[$key]['title'] = $link->getTitle(); $content[$key]['options'] = $link->getOptions(); $content[$key]['description'] = $link->getDescription(); $content[$key]['url'] = $link->getUrlObject(); } ksort($content); return $content; }
/** * Generates menu links in a tree structure. */ protected function generateLinks($num_links, $menus, $title_length, $link_types, $max_depth, $max_width) { $links = array(); $menus = array_keys(array_filter($menus)); $link_types = array_keys(array_filter($link_types)); $nids = array(); for ($i = 1; $i <= $num_links; $i++) { // Pick a random menu. $menu_name = $menus[array_rand($menus)]; // Build up our link. $link_title = $this->getRandom()->word(mt_rand(2, max(2, $title_length))); $link = $this->menuLinkContentStorage->create(array('menu_name' => $menu_name, 'weight' => mt_rand(-50, 50), 'title' => $link_title, 'bundle' => 'menu_link_content', 'description' => $this->t('Description of @title.', array('@title' => $link_title)))); $link->link->options = array('devel' => TRUE); // For the first $max_width items, make first level links. if ($i <= $max_width) { $depth = 0; } else { // Otherwise, get a random parent menu depth. $depth = mt_rand(1, max(1, $max_depth - 1)); } // Get a random parent link from the proper depth. do { $parameters = new MenuTreeParameters(); $parameters->setMinDepth($depth); $parameters->setMaxDepth($depth); $tree = $this->menuLinkTree->load($menu_name, $parameters); if ($tree) { $link->parent = array_rand($tree); } $depth--; } while (!$link->parent && $depth > 0); $link_type = array_rand($link_types); switch ($link_types[$link_type]) { case 'node': // Grab a random node ID. $select = db_select('node_field_data', 'n')->fields('n', array('nid', 'title'))->condition('n.status', 1)->range(0, 1)->orderRandom(); // Don't put a node into the menu twice. if (!empty($nids[$menu_name])) { $select->condition('n.nid', $nids[$menu_name], 'NOT IN'); } $node = $select->execute()->fetchAssoc(); if (isset($node['nid'])) { $nids[$menu_name][] = $node['nid']; $link->link->uri = 'entity:node/' . $node['nid']; $link->title = $node['title']; break; } case 'external': $link->link->uri = 'http://www.example.com/'; break; case 'front': $link->link->uri = 'internal:/<front>'; break; default: $link->devel_link_type = $link_type; break; } $link->save(); $links[$link->id()] = $link->link_title; } return $links; }
/** * Load a list of menu tree elements. * * @param $menu_id * * @return MenuLinkTreeElement[] */ private function loadMenuTree($menu_id) { $parameters = (new MenuTreeParameters())->setMaxDepth(1); return $this->menuLinkTree->load($menu_id, $parameters); }
/** * Get a list of menu link elements for the specified menu. * * @param string $menu * * @return MenuLinkTreeElement[] */ protected function getMenuLinkElements($menu) { $parameters = (new MenuTreeParameters())->setMaxDepth(1); return $this->menuLinkTree->load($menu, $parameters); }