/** * Overrides EntityForm::form(). */ public function form(array $form, array &$form_state) { $menu_link = $this->entity; // Since menu_link_load() no longer returns a translated and access checked // item, do it here instead. _menu_link_translate($menu_link); $form['link_title'] = array('#type' => 'textfield', '#title' => t('Menu link title'), '#default_value' => $menu_link->link_title, '#description' => t('The text to be used for this link in the menu.'), '#required' => TRUE); foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) { $form[$key] = array('#type' => 'value', '#value' => $menu_link->{$key}); } // Any item created or edited via this interface is considered "customized". $form['customized'] = array('#type' => 'value', '#value' => 1); // We are not using url() when constructing this path because it would add // $base_path. $path = $menu_link->link_path; if (isset($menu_link->options['query'])) { $path .= '?' . $this->urlGenerator->httpBuildQuery($menu_link->options['query']); } if (isset($menu_link->options['fragment'])) { $path .= '#' . $menu_link->options['fragment']; } if ($menu_link->module == 'menu_ui') { $form['link_path'] = array('#type' => 'textfield', '#title' => t('Path'), '#maxlength' => 255, '#default_value' => $path, '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')), '#required' => TRUE); } else { $form['_path'] = array('#type' => 'item', '#title' => t('Path'), '#description' => l($menu_link->link_title, $menu_link->href, $menu_link->options)); } $form['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => isset($menu_link->options['attributes']['title']) ? $menu_link->options['attributes']['title'] : '', '#rows' => 1, '#description' => t('Shown when hovering over the menu link.')); $form['enabled'] = array('#type' => 'checkbox', '#title' => t('Enabled'), '#default_value' => !$menu_link->hidden, '#description' => t('Menu links that are not enabled will not be listed in any menu.')); $form['expanded'] = array('#type' => 'checkbox', '#title' => t('Show as expanded'), '#default_value' => $menu_link->expanded, '#description' => t('If selected and this menu link has children, the menu will always appear expanded.')); // Generate a list of possible parents (not including this link or descendants). $options = menu_ui_parent_options(menu_ui_get_menus(), $menu_link); $default = $menu_link->menu_name . ':' . $menu_link->plid; if (!isset($options[$default])) { $default = 'tools:0'; } $form['parent'] = array('#type' => 'select', '#title' => t('Parent link'), '#default_value' => $default, '#options' => $options, '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), '#attributes' => array('class' => array('menu-title-select'))); // Get number of items in menu so the weight selector is sized appropriately. $delta = $this->menuLinkStorage->countMenuLinks($menu_link->menu_name); $form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#delta' => max($delta, 50), '#default_value' => $menu_link->weight, '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.')); // Language module allows to configure the menu link language independently // of the menu language. It also allows to optionally show the language // selector on the menu link form so that the language of each menu link can // be configured individually. if ($this->moduleHandler->moduleExists('language')) { $language_configuration = language_get_default_configuration('menu_link', $menu_link->bundle()); $default_langcode = $menu_link->isNew() ? $language_configuration['langcode'] : $menu_link->langcode; $language_show = $language_configuration['language_show']; } else { $default_langcode = $menu_link->isNew() ? entity_load('menu', $menu_link->menu_name)->language()->getId() : $menu_link->langcode; $language_show = FALSE; } $form['langcode'] = array('#type' => 'language_select', '#title' => t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $default_langcode, '#access' => $language_show); return parent::form($form, $form_state, $menu_link); }
/** * Wraps _menu_link_translate(). */ protected function menuLinkTranslate(&$item) { _menu_link_translate($item); }
/** * Provide a single block on the administration overview page. * * @param \Drupal\menu_link\MenuLinkInterface|array $item * The menu item to be displayed. * * @return array * An array of menu items, as expected by theme_admin_block_content(). */ public function getAdminBlock($item) { if (!isset($item['mlid'])) { $menu_links = $this->menuLinkStorage->loadByProperties(array('link_path' => $item['path'], 'module' => 'system')); if ($menu_links) { $menu_link = reset($menu_links); $item['mlid'] = $menu_link->id(); $item['menu_name'] = $menu_link->menu_name; } else { return array(); } } if (isset($this->menuItems[$item['mlid']])) { return $this->menuItems[$item['mlid']]; } $content = array(); $menu_links = $this->menuLinkStorage->loadByProperties(array('plid' => $item['mlid'], 'menu_name' => $item['menu_name'], 'hidden' => 0)); foreach ($menu_links as $link) { _menu_link_translate($link); if ($link['access']) { // The link description, either derived from 'description' in // hook_menu() or customized via Menu UI module is used as title attribute. if (!empty($link['localized_options']['attributes']['title'])) { $link['description'] = $link['localized_options']['attributes']['title']; unset($link['localized_options']['attributes']['title']); } // Prepare for sorting as in function _menu_tree_check_access(). // The weight is offset so it is always positive, with a uniform 5-digits. $key = 50000 + $link['weight'] . ' ' . Unicode::strtolower($link['title']) . ' ' . $link['mlid']; $content[$key] = $link; } } ksort($content); $this->menuItems[$item['mlid']] = $content; return $content; }
/** * Returns all top level menu links. * * @return \Drupal\menu_link\MenuLinkInterface[] */ protected function getTopLevelMenuLinks() { $route_provider = \Drupal::service('router.route_provider'); $routes = array(); foreach ($route_provider->getAllRoutes() as $key => $value) { $path = $value->getPath(); if (strpos($path, '/admin/') === 0 && count(explode('/', $path)) == 3) { $routes[$key] = $key; } } $menu_link_ids = \Drupal::entityQuery('menu_link')->condition('route_name', $routes)->execute(); $menu_items = \Drupal::entityManager()->getStorage('menu_link')->loadMultiple($menu_link_ids); foreach ($menu_items as &$menu_item) { _menu_link_translate($menu_item); } return $menu_items; }
/** * Provide the administration overview page. * * @param string $path * The administrative path for which to display child links. * * @return array * A renderable array of the administration overview page. */ public function overview($path) { // 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' => url('admin/reports/status'))), 'error'); } $blocks = array(); // Load all links on $path and menu links below it. $query = $this->queryFactory->get('menu_link')->condition('link_path', $path)->condition('module', 'system'); $result = $query->execute(); $menu_link_storage = $this->entityManager()->getStorage('menu_link'); if ($system_link = $menu_link_storage->loadMultiple($result)) { $system_link = reset($system_link); $query = $this->queryFactory->get('menu_link')->condition('link_path', 'admin/help', '<>')->condition('menu_name', $system_link->menu_name)->condition('plid', $system_link->id())->condition('hidden', 0); $result = $query->execute(); if (!empty($result)) { $menu_links = $menu_link_storage->loadMultiple($result); foreach ($menu_links as $item) { _menu_link_translate($item); if (!$item['access']) { continue; } // The link description, either derived from 'description' in hook_menu() // or customized via Menu UI module is used as title attribute. if (!empty($item['localized_options']['attributes']['title'])) { $item['description'] = $item['localized_options']['attributes']['title']; unset($item['localized_options']['attributes']['title']); } $block = $item; $block['content'] = array('#theme' => 'admin_block_content', '#content' => $this->systemManager->getAdminBlock($item)); if (!empty($block['content']['#content'])) { // Prepare for sorting as in function _menu_tree_check_access(). // The weight is offset so it is always positive, with a uniform 5-digits. $blocks[50000 + $item['weight'] . ' ' . $item['title'] . ' ' . $item['mlid']] = $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.')); } }