Commit f9f3e7f2 authored by webchick's avatar webchick

Issue #2237963 by Wim Leers: Remove dead menu system code: menu_get_active_trail() and friends.

parent 80f9e581
......@@ -657,113 +657,6 @@ function menu_get_active_menu_names() {
return menu_set_active_menu_names();
}
/**
* Sets the active path, which determines which page is loaded.
*
* Note that this may not have the desired effect unless invoked very early
* in the page load or unless you do a subrequest to generate your page output.
*
* @param $path
* A Drupal path - not a path alias.
*/
function menu_set_active_item($path) {
// Since the active item has changed, the active menu trail may also be out
// of date.
drupal_static_reset('menu_set_active_trail');
// @todo Refactor to use the Symfony Request object.
_current_path($path);
}
/**
* Sets the active trail (path to the menu tree root) of the current page.
*
* Any trail set by this function will only be used for functionality that calls
* menu_get_active_trail(). Drupal core only uses trails set here for
* the page title and not for menu trees or page content.
*
* To affect the trail used by menu trees, use menu_tree_set_path(). To affect
* the page content, use menu_set_active_item() instead.
*
* @param $new_trail
* Menu trail to set; the value is saved in a static variable and can be
* retrieved by menu_get_active_trail(). The format of this array should be
* the same as the return value of menu_get_active_trail().
*
* @return
* The active trail. See menu_get_active_trail() for details.
*/
function menu_set_active_trail($new_trail = NULL) {
$trail = &drupal_static(__FUNCTION__);
/** @var \Drupal\menu_link\MenuTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu_link.tree');
if (isset($new_trail)) {
$trail = $new_trail;
}
elseif (!isset($trail)) {
$trail = array();
$trail[] = array(
'title' => t('Home'),
'href' => '<front>',
'link_path' => '',
'localized_options' => array(),
'type' => 0,
);
// Try to retrieve a menu link corresponding to the current path. If more
// than one exists, the link from the most preferred menu is returned.
$preferred_link = menu_link_get_preferred();
// There is a link for the current path.
if ($preferred_link) {
_menu_link_translate($preferred_link);
// Pass TRUE for $only_active_trail to make menu_tree_page_data() build
// a stripped down menu tree containing the active trail only, in case
// the given menu has not been built in this request yet.
$tree = $menu_tree->buildPageData($preferred_link['menu_name'], NULL, TRUE);
list($key, $curr) = each($tree);
}
// There is no link for the current path.
else {
$curr = FALSE;
}
while ($curr) {
$link = $curr['link'];
if ($link['in_active_trail']) {
// Add the link to the trail, unless it links to its parent.
if (!($link['type'] & MENU_LINKS_TO_PARENT)) {
// The menu tree for the active trail may contain additional links
// that have not been translated yet, since they contain dynamic
// argument placeholders (%). Such links are not contained in regular
// menu trees, and have only been loaded for the additional
// translation that happens here, so as to be able to display them in
// the breadcumb for the current page.
// @see _menu_tree_check_access()
// @see _menu_link_translate()
if (strpos($link['href'], '%') !== FALSE) {
_menu_link_translate($link);
}
if ($link['access']) {
$trail[] = $link;
}
}
$tree = $curr['below'] ? $curr['below'] : array();
}
list($key, $curr) = each($tree);
}
// Make sure the current page is in the trail to build the page title, by
// appending either the preferred link or the menu router item for the
// current page. Exclude it if we are on the front page.
$last = end($trail);
if ($preferred_link && $last['href'] != $preferred_link['href'] && !drupal_is_front_page()) {
$trail[] = $preferred_link;
}
}
return $trail;
}
/**
* Looks up the preferred menu link for a given system path.
*
......@@ -846,28 +739,6 @@ function menu_link_get_preferred($path = NULL, $selected_menu = NULL) {
return isset($preferred_links[$path][$selected_menu]) ? $preferred_links[$path][$selected_menu] : FALSE;
}
/**
* Gets the active trail (path to root menu root) of the current page.
*
* If a trail is supplied to menu_set_active_trail(), that value is returned. If
* a trail is not supplied to menu_set_active_trail(), the path to the current
* page is calculated and returned. The calculated trail is also saved as a
* static value for use by subsequent calls to menu_get_active_trail().
*
* @return
* Path to menu root of the current page, as an array of menu link items,
* starting with the site's home page. Each link item is an associative array
* with the following components:
* - title: Title of the item.
* - href: Drupal path of the item.
* - localized_options: Options for passing into the l() function.
* - type: A menu type constant, such as MENU_DEFAULT_LOCAL_TASK, or 0 to
* indicate it's not really in the menu (used for the home page item).
*/
function menu_get_active_trail() {
return menu_set_active_trail();
}
/**
* Clears all cached menu data.
*
......@@ -885,10 +756,6 @@ function menu_cache_clear_all() {
function menu_reset_static_cache() {
\Drupal::entityManager()
->getStorage('menu_link')->resetCache();
drupal_static_reset('_menu_build_tree');
drupal_static_reset('menu_tree');
drupal_static_reset('menu_tree_all_data');
drupal_static_reset('menu_tree_page_data');
drupal_static_reset('menu_link_get_preferred');
}
......
......@@ -148,22 +148,6 @@ public function on403Html(FlattenException $exception, Request $request) {
$subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'GET', array('destination' => $system_path, '_exception_statuscode' => 403), $request->cookies->all(), array(), $request->server->all());
}
// The active trail is being statically cached from the parent request to
// the subrequest, like any other static. Unfortunately that means the
// data in it is incorrect and does not get regenerated correctly for
// the subrequest. In this instance, that even causes a fatal error in
// some circumstances because menu_get_active_trail() ends up having
// a missing localized_options value. To work around that, reset the
// menu static variables and let them be regenerated as needed.
// @todo It is likely that there are other such statics that need to be
// reset that are not triggering test failures right now. If found,
// add them here.
// @todo Refactor the breadcrumb system so that it does not rely on static
// variables in the first place, which will eliminate the need for this
// hack.
drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache();
$response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode(403, 'Access denied');
}
......@@ -224,22 +208,6 @@ public function on404Html(FlattenException $exception, Request $request) {
$subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'GET', array('destination' => $system_path, '_exception_statuscode' => 404), $request->cookies->all(), array(), $request->server->all());
}
// The active trail is being statically cached from the parent request to
// the subrequest, like any other static. Unfortunately that means the
// data in it is incorrect and does not get regenerated correctly for
// the subrequest. In this instance, that even causes a fatal error in
// some circumstances because menu_get_active_trail() ends up having
// a missing localized_options value. To work around that, reset the
// menu static variables and let them be regenerated as needed.
// @todo It is likely that there are other such statics that need to be
// reset that are not triggering test failures right now. If found,
// add them here.
// @todo Refactor the breadcrumb system so that it does not rely on static
// variables in the first place, which will eliminate the need for this
// hack.
drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache();
$response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode(404, 'Not Found');
}
......
......@@ -226,7 +226,6 @@ public function buildPageData($menu_name, $max_depth = NULL, $only_active_trail
// that needs to be checked for access on all levels, we simply check
// whether we have the menu already in cache, or otherwise, build a
// minimum tree containing the active trail only.
// @see menu_set_active_trail()
if (!isset($this->menuPageTrees[$cid]) && $only_active_trail) {
$cid .= ':trail';
}
......
......@@ -37,9 +37,6 @@ public function renderTree($tree);
* specified in calls to static::setPath(), the preferred link will be
* overridden by the corresponding path returned by static::getPath().
*
* Setting this path does not affect the main content; for that use
* menu_set_active_item() instead.
*
* @param string $menu_name
* The name of the affected menu tree.
* @param string $path
......
......@@ -36,20 +36,6 @@ public function titleCallback(array $_title_arguments = array(), $_title = '') {
return t($_title_arguments['title']) . ' - Case ' . $_title_arguments['case_number'];
}
/**
* @todo Remove menu_test_custom_403_404_callback().
*/
public function custom403404() {
return menu_test_custom_403_404_callback();
}
/**
* @todo Remove menu_test_menu_trail_callback().
*/
public function menuTrail() {
return menu_test_menu_trail_callback();
}
/**
* @todo Remove menu_test_theme_page_callback().
*/
......
<?php
/**
* @file
* Contains \Drupal\menu_test\EventSubscriber\ActiveTrailSubscriber.
*/
namespace Drupal\menu_test\EventSubscriber;
use Drupal\Core\KeyValueStore\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Tracks the active trail.
*/
class ActiveTrailSubscriber implements EventSubscriberInterface {
/**
* The active trail before redirect.
*
* @var array
*/
protected $trail = array();
/**
* The state service.
*
* @var \Drupal\Core\KeyValueStore\StateInterface
*/
protected $state;
/**
* Constructs a new ActiveTrailSubscriber.
*
* @param \Drupal\Core\KeyValueStore\StateInterface $state
* The state service.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* Tracks the active trail.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The event to process.
*/
public function onKernelRequest(GetResponseEvent $event) {
// When requested by one of the MenuTrailTestCase tests, record the initial
// active trail during Drupal's bootstrap (before the user is redirected to
// a custom 403 or 404 page).
if (!$this->trail && $this->state->get('menu_test.record_active_trail') ?: FALSE) {
$this->trail = menu_get_active_trail();
$this->state->set('menu_test.active_trail_initial', $this->trail);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onKernelRequest');
return $events;
}
}
<?php
/**
* @file
* Contains \Drupal\menu_test\EventSubscriber\ActiveTrailTestSubscriber.
*/
namespace Drupal\menu_test\EventSubscriber;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Tracks the active trail.
*/
class ActiveTrailTestSubscriber implements EventSubscriberInterface {
/**
* The state keyvalue store.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $state;
/**
* Constructs the ActiveTrailTestSubscriber.
*
* @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state
* The state keyvalue store.
*/
public function __construct(KeyValueStoreInterface $state) {
$this->state = $state;
}
/**
* Records the active trail.
*/
public function onKernelException(KernelEvent $event) {
// When requested by one of the MenuTrailTestCase tests, record the initial
// active trail during Drupal's bootstrap (before the user is redirected to a
// custom 403 or 404 page). See menu_test_custom_403_404_callback().
if ($this->state->get('menu_test.record_active_trail') ?: FALSE) {
$this->state->set('menu_test.active_trail_initial', menu_get_active_trail());
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// The actual exception subscriber has a weight of -128 so this comes first.
$events[KernelEvents::EXCEPTION] = 'onKernelException';
return $events;
}
}
......@@ -57,25 +57,6 @@ menu_test.hidden.block:
title: Blocks
route_name: menu_test.hidden_block
parent: menu_test.hidden
# Menu trail tests.
# @see MenuTrailTestCase
menu_test.menu-trail:
title: 'Menu trail - Case 1'
route_name: menu_test.menu_trail
parent: menu_test
menu_test.admin.config.development.menu-trail:
title: 'Menu trail - Case 2'
description: 'Tests menu_tree_set_path()'
route_name: menu_test.menu_trail_admin
parent: system.admin_config_development
menu_test.custom-403-page:
title: 'Custom 403 page'
route_name: menu_test.custom_403
parent: menu_test
menu_test.custom-404-page:
title: 'Custom 404 page'
route_name: menu_test.custom_404
parent: menu_test
menu_test.menu-title-test.case1:
title: 'Example title - Case 1'
route_name: menu_test.title_test_case1
......
......@@ -112,27 +112,6 @@ function menu_test_menu_trail_callback() {
return 'This is menu_test_menu_trail_callback().';
}
/**
* Page callback: Sets the active menu trail for our custom 403 and 404 pages.
*
* @return string
* A text string that can be used for comparison.
*
* @see menu_test_menu().
*
* @deprecated Use \Drupal\menu_test\Controller\MenuTestController::custom403404()
*/
function menu_test_custom_403_404_callback() {
// When requested by one of the TrailTest tests, record the final
// active trail now that the user has been redirected to the custom 403 or
// 404 page.
if (\Drupal::state()->get('menu_test.record_active_trail') ?: FALSE) {
\Drupal::state()->set('menu_test.active_trail_final', menu_get_active_trail());
}
return 'This is menu_test_custom_403_404_callback().';
}
/**
* Page callback: Tests the theme negotiation functionality.
*
......
......@@ -286,38 +286,6 @@ menu_test.hidden_block_delete:
requirements:
_access: 'TRUE'
menu_test.custom_403:
path: '/menu-test/custom-403-page'
defaults:
_title: 'Custom 403 page'
_content: '\Drupal\menu_test\Controller\MenuTestController::custom403404'
requirements:
_access: 'TRUE'
menu_test.custom_404:
path: '/menu-test/custom-404-page'
defaults:
_title: 'Custom 404 page'
_content: '\Drupal\menu_test\Controller\MenuTestController::custom403404'
requirements:
_access: 'TRUE'
menu_test.menu_trail:
path: '/menu-test/menu-trail'
defaults:
_title: 'Menu trail - Case 1'
_content: '\Drupal\menu_test\Controller\MenuTestController::menuTrail'
requirements:
_access: 'TRUE'
menu_test.menu_trail_admin:
path: '/admin/config/development/menu-trail'
defaults:
_title: 'Menu trail - Case 2'
_content: '\Drupal\menu_test\Controller\MenuTestController::menuTrail'
requirements:
_permission: 'access administration pages'
menu_test.theme_callback:
path: '/menu-test/theme-callback/{inherited}'
defaults:
......
......@@ -4,12 +4,6 @@ services:
tags:
- { name: event_subscriber }
menu_test.active_trail_subscriber:
class: Drupal\menu_test\EventSubscriber\ActiveTrailSubscriber
arguments: ['@state']
tags:
- { name: event_subscriber }
theme.negotiator.test_theme:
class: Drupal\menu_test\Theme\TestThemeNegotiator
tags:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment