diff --git a/core/includes/menu.inc b/core/includes/menu.inc index d205ac0..4e3beff 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -964,37 +964,13 @@ function _menu_link_translate(&$item, $translate = FALSE) { * TRUE if the user has access or FALSE if the user should be presented * with access denied. * + * @deprecated Use \Drupal::service('menu_link.access')->access() instead. + * * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException * If the system path in $href does not match the $route. */ function menu_item_route_access(Route $route, $href, &$map) { - $request = Request::create('/' . $href); - $request->attributes->set('_system_path', $href); - // Attempt to match this path to provide a fully built request to the - // access checker. - try { - $request->attributes->add(Drupal::service('router.dynamic')->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']); - } - } - } - - return Drupal::service('access_manager')->check($route, $request); + return Drupal::service('menu_link.access')->access($route, $href, $map); } /** diff --git a/core/modules/book/book.services.yml b/core/modules/book/book.services.yml index 9d8c140..a5e9746 100644 --- a/core/modules/book/book.services.yml +++ b/core/modules/book/book.services.yml @@ -1,4 +1,8 @@ services: + book.breadcrumb: + class: Drupal\book\BookBreadcrumbBuilder + 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..ddb0857 --- /dev/null +++ b/core/modules/book/lib/Drupal/book/BookBreadcrumbBuilder.php @@ -0,0 +1,29 @@ +book)) { + // @todo Write an actual implementation. + return menu_get_active_breadcrumb(); + } + } + +} diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkAccess.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkAccess.php new file mode 100644 index 0000000..4935c27 --- /dev/null +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkAccess.php @@ -0,0 +1,93 @@ +accessManager = $access_manager; + $this->dynamicRouter = $dynamic_router; + } + + /** + * Checks access to a menu item by mocking a request for a path. + * + * @param \Symfony\Component\Routing\Route $route + * Router for the given menu item. + * @param string $href + * Menu path as returned by link_path of the menu item. + * @param array $map + * An array of path arguments; for example, array('node', '5'). + * + * @return bool + * TRUE if the user has access or FALSE if the user should be presented + * with access denied. + * + * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException + * If the system path in $href does not match the $route. + */ + public function access(Route $route, $href, &$map) { + $request = Request::create('/' . $href); + $request->attributes->set('_system_path', $href); + // Attempt to match this path to provide a fully built request to the + // access checker. + 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']); + } + } + } + + return $this->accessManager->check($route, $request); + } + +} diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php index acece08..89ea1c2 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkBreadcrumbBuilder.php @@ -8,6 +8,12 @@ namespace Drupal\menu_link; use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; +use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Routing\RouteProviderInterface; +use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\menu_link\MenuLinkAccess; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class to define the menu_link breadcrumb builder. @@ -15,12 +21,106 @@ class MenuLinkBreadcrumbBuilder implements BreadcrumbBuilderInterface { /** + * The current request. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * The route provider service. + * + * @var \Drupal\Core\Routing\RouteProviderInterface + */ + protected $routeProvider; + + /** + * The menu link storage controller. + * + * @var \Drupal\menu_link\MenuLinkStorageControllerInterface + */ + protected $menuLinkStorage; + + /** + * The menu link access service. + * + * @var \Drupal\menu_link\MenuLinkAccess + */ + protected $menuLinkAccess; + + /** + * The translation manager service. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface; + */ + protected $translation; + + /** + * Constructs the MenuLinkBreadcrumbBuilder. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request. + * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider + * The route provider service. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager service. + * @param \Drupal\menu_link\MenuLinkAccess $menu_link_access + * The menu link access service. + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation + * The translation manager service. + */ + public function __construct(Request $request, RouteProviderInterface $route_provider, EntityManager $entity_manager, MenuLinkAccess $menu_link_access, TranslationInterface $translation) { + $this->request = $request; + $this->routeProvider = $route_provider; + $this->menuLinkStorage = $entity_manager->getStorageController('menu_link'); + $this->menuLinkAccess = $menu_link_access; + $this->translation = $translation; + } + + /** * {@inheritdoc} */ public function build(array $attributes) { - // @todo Rewrite the implementation. - // Currently the result always array. - return menu_get_active_breadcrumb(); + $links = array(); + if ($system_path = $this->request->get('_system_path')) { + $path_elements = explode('/', $system_path); + while (count($path_elements) > 0) { + array_pop($path_elements); + $pattern = implode('/', $path_elements); + $routes = $this->routeProvider->getRoutesByPattern('/' . $pattern); + if (count($routes) > 0) { + try { + // Take the first one. + $routes = $routes->all(); + $route = reset($routes); + continue; + if ($this->menuLinkAccess->access($route, $pattern, $path_elements) && + ($menu_links = $this->menuLinkStorage->loadMultipleByProperties(array( + 'link_path' => $pattern + )))) { + $menu_link = reset($menu_links); + // @todo Replace with link generator service when + // https://drupal.org/node/2047619 lands. + $links[] = l($menu_link->label(), $pattern, $menu_link->options); + } + } + catch (NotFoundHttpException $e) { + continue; + } + } + else { + // Legacy hook_menu page callback. + // @todo Remove this once all routes are converted. + if ($item = menu_get_item($pattern)) { + $links[] = l($item['title'], $pattern, $item['options']); + } + } + } + } + // @todo Replace with link generator service when + // https://drupal.org/node/2047619 lands. + $links[] = l($this->translation->translate('Home'), ''); + return array_reverse($links); } } diff --git a/core/modules/menu_link/menu_link.services.yml b/core/modules/menu_link/menu_link.services.yml index a428476..d2d2a9f 100644 --- a/core/modules/menu_link/menu_link.services.yml +++ b/core/modules/menu_link/menu_link.services.yml @@ -1,5 +1,10 @@ services: + menu_link.access: + class: Drupal\menu_link\MenuLinkAccess + arguments: ['@access_manager', '@router.dynamic'] + menu_link.breadcrumb: class: Drupal\menu_link\MenuLinkBreadcrumbBuilder + arguments: ['@request', '@router.route_provider', '@plugin.manager.entity', '@menu_link.access', '@string_translation'] tags: - { name: breadcrumb_builder, priority: 0 } 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',