diff --git a/core/includes/menu.inc b/core/includes/menu.inc index d205ac0..ccc40f8 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -895,7 +895,6 @@ function _menu_link_translate(&$item, $translate = FALSE) { // current path, and we can take over the argument map for a link like // 'foo/%/bar'. This inheritance is only valid for breadcrumb links. // @see _menu_tree_check_access() - // @see menu_get_active_breadcrumb() elseif ($translate && ($current_router_item = menu_get_item())) { // If $translate is TRUE, then this link is in the active trail. // Only translate paths within the current path. @@ -2610,57 +2609,6 @@ function menu_get_active_trail() { } /** - * Gets the breadcrumb for the current page, as determined by the active trail. - * - * @see menu_set_active_trail() - */ -function menu_get_active_breadcrumb() { - $breadcrumb = array(); - - // No breadcrumb for the front page. - if (drupal_is_front_page()) { - return $breadcrumb; - } - - $item = menu_get_item(); - if (!empty($item['access'])) { - $active_trail = menu_get_active_trail(); - - // Allow modules to alter the breadcrumb, if possible, as that is much - // faster than rebuilding an entirely new active trail. - drupal_alter('menu_breadcrumb', $active_trail, $item); - - // Don't show a link to the current page in the breadcrumb trail. - $end = end($active_trail); - if (Drupal::request()->attributes->get('_system_path') == $end['href']) { - array_pop($active_trail); - } - - // Remove the tab root (parent) if the current path links to its parent. - // Normally, the tab root link is included in the breadcrumb, as soon as we - // are on a local task or any other child link. However, if we are on a - // default local task (e.g., node/%/view), then we do not want the tab root - // link (e.g., node/%) to appear, as it would be identical to the current - // page. Since this behavior also needs to work recursively (i.e., on - // default local tasks of default local tasks), and since the last non-task - // link in the trail is used as page title (see menu_get_active_title()), - // this condition cannot be cleanly integrated into menu_get_active_trail(). - // menu_get_active_trail() already skips all links that link to their parent - // (commonly MENU_DEFAULT_LOCAL_TASK). In order to also hide the parent link - // itself, we always remove the last link in the trail, if the current - // router item links to its parent. - if (($item['type'] & MENU_LINKS_TO_PARENT) == MENU_LINKS_TO_PARENT) { - array_pop($active_trail); - } - - foreach ($active_trail as $parent) { - $breadcrumb[] = l($parent['title'], $parent['href'], $parent['localized_options']); - } - } - return $breadcrumb; -} - -/** * Gets the title of the current page, as determined by the active trail. */ function menu_get_active_title() { diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index 9d8c140..283efd8 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -1,4 +1,9 @@ services: + book.breadcrumb: + class: Drupal\book\BookBreadcrumbBuilder + arguments: ['@plugin.manager.entity', '@access_manager', '@string_translation'] + tags: + - { name: breadcrumb_builder, priority: 701 } book.manager: class: Drupal\book\BookManager arguments: ['@database', '@plugin.manager.entity'] diff --git a/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php new file mode 100644 index 0000000..80838bc --- /dev/null +++ b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php @@ -0,0 +1,107 @@ +menuLinkStorage = $entity_manager->getStorageController('menu_link'); + $this->accessManager = $access_manager; + $this->translation = $translation; + } + + /** + * {@inheritdoc} + */ + public function build(array $attributes) { + // @todo - like \Drupal\forum\ForumBreadcrumbBuilder this depends on the + // legacy non-route node view. It must be updated once that's converted. + if (!empty($attributes['_drupal_menu_item']) && !empty($attributes['_drupal_menu_item']['map'][1]->book)) { + $mlids = array(); + // @todo Replace with link generator service when + // https://drupal.org/node/2047619 lands. + $links = array(l($this->translation->translate('Home'), '')); + $book = $attributes['_drupal_menu_item']['map'][1]->book; + $depth = 1; + // We skip the current node. + while (!empty($book['p' . ($depth + 1)])) { + $mlids[] = $book['p' . $depth]; + $depth++; + } + $menu_links = $this->menuLinkStorage->loadMultiple($mlids); + if (count($menu_links) > 0) { + $depth = 1; + while (!empty($book['p' . ($depth + 1)])) { + if (!empty($menu_links[$book['p' . $depth]]) && ($menu_link = $menu_links[$book['p' . $depth]])) { + // Legacy hook_menu page callback. + // @todo change this once thie node view route is converted. + if ($item = menu_get_item($menu_link->link_path)) { + if ($item['access']) { + $links[] = l($menu_link->label(), $menu_link->link_path, $menu_link->options); + } + } + } + $depth++; + } + } + return $links; + } + } + +} diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php deleted file mode 100644 index acece08..0000000 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php +++ /dev/null @@ -1,26 +0,0 @@ -isNew()) { - // Get the human-readable menu title from the given menu name. - $titles = menu_get_menus(); - $current_title = $titles[$menu_link->menu_name]; - - // Get the current breadcrumb and add a link to that menu's overview page. - $breadcrumb = menu_get_active_breadcrumb(); - $breadcrumb[] = l($current_title, 'admin/structure/menu/manage/' . $menu_link->menu_name); - drupal_set_breadcrumb($breadcrumb); - } - $form['link_title'] = array( '#type' => 'textfield', '#title' => t('Menu link title'), diff --git a/core/modules/menu_link/menu_link.services.yml b/core/modules/menu_link/menu_link.services.yml deleted file mode 100644 index a428476..0000000 --- a/core/modules/menu_link/menu_link.services.yml +++ /dev/null @@ -1,5 +0,0 @@ -services: - menu_link.breadcrumb: - class: Drupal\menu_link\MenuLinkBreadcrumbBuilder - tags: - - { name: breadcrumb_builder, priority: 0 } diff --git a/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php new file mode 100644 index 0000000..0edf725 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/PathBasedBreadcrumbBuilder.php @@ -0,0 +1,241 @@ +request = $request; + $this->routeProvider = $route_provider; + $this->menuLinkStorage = $entity_manager->getStorageController('menu_link'); + $this->accessManager = $access_manager; + $this->translation = $translation; + $this->menuStorage = $entity_manager->getStorageController('menu'); + $this->router = $router; + } + /** + * {@inheritdoc} + */ + public function build(array $attributes) { + $links = array(); + + // Custom breadcrumb behaviour for editing menu links, we append a link to + // the menu in which the link is found. + if (!empty($attributes['_route']) && $attributes['_route'] == 'menu_link_edit' && !empty($attributes['menu_link'])) { + $menu_link = $attributes['menu_link']; + if (!$menu_link->isNew()) { + // Add a link to the menu admin screen. + $menu = $this->menuStorage->load($menu_link->menu_name); + $links[] = l($menu->label(), 'admin/structure/menu/manage/' . $menu_link->menu_name); + } + } + + // General path-based breadcrumbs. + $path_elements = explode('/', trim($this->request->getPathInfo(), '/')); + while (count($path_elements) > 1) { + array_pop($path_elements); + // Copy the path elements for up-casting. + $pattern = implode('/', $path_elements); + $route_request = $this->getRequestForPath($pattern); + if ($route_request) { + $access = FALSE; + $route_name = $route_request->attributes->get('_route'); + if ($route_request->attributes->get('_legacy')) { + // @todo handle access check + $access = TRUE; + } + else { + debug(array_keys($route_request->attributes->all())); + $parameters = array(); //$route_request->attributes->get('_raw_variables'); + $access = $this->accessManager->checkNamedRoute($route_name, $parameters, $route_request); + } + if ($access) { + $links[] = l($pattern, $pattern); + } + } + + } + // @todo Replace with link generator service when + // https://drupal.org/node/2047619 lands. + $links[] = l($this->translation->translate('Home'), ''); + return array_reverse($links); + } + + /** + * Matches a path in the router. + * + * @param string $path + * The request path. + + * @return \Symfony\Component\HttpFoundation\Request + * A populated request object or NULL if the patch couldn't be matched. + */ + protected function getRequestForPath($path) { + $request = Request::create($path); + // Attempt to match this path to provide a fully built request. + try { + $request->attributes->add($this->router->matchRequest($request)); + return $request; + } + catch (NotFoundHttpException $e) { + return NULL; + } + catch (ResourceNotFoundException $e) { + return NULL; + } + } + + /** + * Upcasts the path elements from their raw form. + * + * Path map is of form array('node', 1) when passed in. After upcasting raw + * values are replaced with their actual objects for example array('node', 1) + * would become array('node', \Drupal\node\Entity\Node); + * + * @param \Symfony\Component\Routing\Route $route + * The route object. + * @param string $href + * The request path. + * @param array $map + * The raw path map which is to be upcast. + */ + protected function upcastParameters(Route $route, $href, array &$map) { + $request = Request::create('/' . $href); + $request->attributes->set('_system_path', $href); + // Attempt to match this path to provide a fully built request. + try { + $request->attributes->add($this->dynamicRouter->matchRequest($request)); + } + catch (NotFoundHttpException $e) { + return FALSE; + } + + // Populate the map with any matching values from the request. + $path_bits = explode('/', trim($route->getPath(), '/')); + foreach ($map as $index => $map_item) { + $matches = array(); + // Search for placeholders wrapped by curly braces. For example, a path + // 'foo/{bar}/baz' would return 'bar'. + if (isset($path_bits[$index]) && preg_match('/{(?.*)}/', $path_bits[$index], $matches)) { + // If that placeholder is present on the request attributes, replace the + // placeholder in the map with the value. + if ($request->attributes->has($matches['placeholder'])) { + $map[$index] = $request->attributes->get($matches['placeholder']); + } + } + } + } + + /** + * Returns a named map of placeholder values. + * + * Path map is of form array('node', 1) when passed in. After mapping values + * that correspond with named placeholders in the path are returned, keyed by + * the placeholder name. For example array('node', 1) for path node/{node} + * would be returned as array('node' => 1). + * + * @param \Symfony\Component\Routing\Route $route + * The route object. + * @param array $map + * The path map, eg array('node', 1) for /node/1. + */ + protected function getNamedParameters(Route $route, array $map) { + // Populate the map with any matching values from the request. + $path_bits = explode('/', trim($route->getPath(), '/')); + $named = array(); + foreach ($map as $index => $map_item) { + $matches = array(); + // Search for placeholders wrapped by curly braces. For example, a path + // 'foo/{bar}/baz' would return 'bar'. + if (isset($path_bits[$index]) && preg_match('/{(?.*)}/', $path_bits[$index], $matches)) { + $named[$matches['placeholder']] = $map_item; + } + } + return $named; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/TrailTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/TrailTest.php index fbd3e34..4da001e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/TrailTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/TrailTest.php @@ -83,23 +83,9 @@ function testMenuTreeSetPath() { 'admin/config/development/menu-trail' => t('Menu trail - Case 2'), ); - $override_breadcrumb = $config + array( - 'admin/config/system' => t('System'), - 'admin/config/system/site-information' => t('Site information'), - ); - $override_tree = $config_tree + array( - 'admin/config/system' => t('System'), - 'admin/config/system/site-information' => t('Site information'), - ); - // Test the tree generation for the Administration menu. \Drupal::state()->delete('menu_test.menu_tree_set_path'); $this->assertBreadcrumb('admin/config/development/menu-trail', $breadcrumb, t('Menu trail - Case 2'), $tree); - - // Override the active trail for the Administration tree; it should affect - // the breadcrumbs and Administration tree. - \Drupal::state()->set('menu_test.menu_tree_set_path', $test_menu_path); - $this->assertBreadcrumb('admin/config/development/menu-trail', $override_breadcrumb, t('Menu trail - Case 2'), $override_tree); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php index 263e2fc..df10877 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php @@ -33,6 +33,13 @@ class TreeAccessTest extends DrupalUnitTestBase { */ protected $routeCollection; + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('menu_link'); + public static function getInfo() { return array( 'name' => 'Menu tree access', diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 6339d1c..4d7aae0 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -940,53 +940,6 @@ function hook_local_task_alter(&$local_tasks) { } /** - * Alter links in the active trail before it is rendered as the breadcrumb. - * - * This hook is invoked by menu_get_active_breadcrumb() and allows alteration - * of the breadcrumb links for the current page, which may be preferred instead - * of setting a custom breadcrumb via drupal_set_breadcrumb(). - * - * Implementations should take into account that menu_get_active_breadcrumb() - * subsequently performs the following adjustments to the active trail *after* - * this hook has been invoked: - * - The last link in $active_trail is removed, if its 'href' is identical to - * the 'href' of $item. This happens, because the breadcrumb normally does - * not contain a link to the current page. - * - The (second to) last link in $active_trail is removed, if the current $item - * is a MENU_DEFAULT_LOCAL_TASK. This happens in order to do not show a link - * to the current page, when being on the path for the default local task; - * e.g. when being on the path node/%/view, the breadcrumb should not contain - * a link to node/%. - * - * Each link in the active trail must contain: - * - title: The localized title of the link. - * - href: The system path to link to. - * - localized_options: An array of options to pass to url(). - * - * @param $active_trail - * An array containing breadcrumb links for the current page. - * @param $item - * The menu router item of the current page. - * - * @see drupal_set_breadcrumb() - * @see menu_get_active_breadcrumb() - * @see menu_get_active_trail() - * @see menu_set_active_trail() - */ -function hook_menu_breadcrumb_alter(&$active_trail, $item) { - // Always display a link to the current page by duplicating the last link in - // the active trail. This means that menu_get_active_breadcrumb() will remove - // the last link (for the current page), but since it is added once more here, - // it will appear. - if (!drupal_is_front_page()) { - $end = end($active_trail); - if ($item['href'] == $end['href']) { - $active_trail[] = $end; - } - } -} - -/** * Alter contextual links before they are rendered. * * This hook is invoked by menu_contextual_links(). The system-determined diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index 08d67ad..08e8ed8 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -10,6 +10,11 @@ services: class: Drupal\system\LegacyBreadcrumbBuilder tags: - {name: breadcrumb_builder, priority: 500} + system.breadcrumb.default: + class: Drupal\system\PathBasedBreadcrumbBuilder + arguments: ['@request', '@router.route_provider', '@plugin.manager.entity', '@access_manager', '@string_translation', '@router'] + tags: + - { name: breadcrumb_builder, priority: 0 } path_processor.files: class: Drupal\system\PathProcessor\PathProcessorFiles tags: