diff --git a/core/includes/menu.inc b/core/includes/menu.inc index c8fddcd..27196d1 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -1994,14 +1994,15 @@ function menu_local_tasks($level = 0) { // If we failed to fetch a router item or the current user doesn't have // access to it, don't bother computing the tabs. - if (!$router_item || !$router_item['access']) { + if ((!$route_name && !$router_item) || ($router_item && !$router_item['access'])) { return $empty; } + // @todo remove all code using {menu_router} and anything using MENU_* // constants when all local actions and local tasks are converted to // plugins. The remaining code should just invoke those managers plus do the // invocations of hook_menu_local_tasks() and hook_menu_local_tasks_alter(). - + if ($router_item) { // Get all tabs (also known as local tasks) and the root page. $result = db_select('menu_router', NULL, array('fetch' => PDO::FETCH_ASSOC)) ->fields('menu_router') @@ -2170,15 +2171,16 @@ function menu_local_tasks($level = 0) { // Remove the depth, we are interested only in their relative placement. $tabs = array_values($tabs); $data['tabs'] += $tabs; + } // Allow modules to dynamically add further tasks. $module_handler = \Drupal::moduleHandler(); foreach ($module_handler->getImplementations('menu_local_tasks') as $module) { $function = $module . '_menu_local_tasks'; - $function($data, $router_item, $root_path); + $function($data, $route_name); } // Allow modules to alter local tasks. - $module_handler->alter('menu_local_tasks', $data, $router_item, $root_path); + $module_handler->alter('menu_local_tasks', $data, $route_name); } if (isset($data['tabs'][$level])) { diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index eed50a4..8e81778 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -30,8 +30,8 @@ function custom_block_help($path, $arg) { /** * Implements hook_menu_local_tasks(). */ -function custom_block_menu_local_tasks(&$data, $router_item, $root_path) { - if ($router_item['route_name'] == 'custom_block.list') { +function custom_block_menu_local_tasks(&$data, $route_name) { + if ($route_name == 'custom_block.list') { // @todo Move to a LocalAction plugin when https://drupal.org/node/2045267 // allows local actions to work with query strings. $item = menu_get_item('block/add'); @@ -48,13 +48,13 @@ function custom_block_menu_local_tasks(&$data, $router_item, $root_path) { $routes = array_map(function ($theme) { return "block.admin_display_$theme"; }, array_keys(list_themes())); - if (in_array($router_item['route_name'], $routes)) { + if (in_array($route_name, $routes)) { // @todo Move to a LocalAction plugin when https://drupal.org/node/2045267 // allows local actions to work with query strings. $item = menu_get_item('block/add'); if ($item['access']) { // Add a destination parameter. - $item['localized_options']['query']['theme'] = end($router_item['map']); + $item['localized_options']['query']['theme'] = \Drupal::request()->attributes->get('theme'); $data['actions']['block/add'] = array( '#theme' => 'menu_local_action', '#link' => $item, diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 090d593..d43552a 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -148,11 +148,11 @@ function forum_menu() { /** * Implements hook_menu_local_tasks(). */ -function forum_menu_local_tasks(&$data, $router_item, $root_path) { +function forum_menu_local_tasks(&$data, $route_name) { global $user; // Add action link to 'node/add/forum' on 'forum' sub-pages. - if ($root_path == 'forum' || $root_path == 'forum/%') { + if (in_array($route_name, array('forum.index', 'forum.page'))) { $request = \Drupal::request(); $forum_term = $request->attributes->get('taxonomy_term'); $vid = \Drupal::config('forum.settings')->get('vocabulary'); diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 0dcda45..eaa388e 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1062,9 +1062,10 @@ function node_menu() { /** * Implements hook_menu_local_tasks(). */ -function node_menu_local_tasks(&$data, $router_item, $root_path) { +function node_menu_local_tasks(&$data, $route_name) { // Add action link to 'node/add' on 'admin/content' page. - if ($root_path == 'admin/content') { + // @todo Switch to inspecting route name in https://drupal.org/node/2021161. + if (current_path() == 'admin/content') { $item = menu_get_item('node/add'); if ($item['access']) { $data['actions'][] = array( diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index f2b0116..d5c1d38 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -858,12 +858,10 @@ function hook_menu_alter(&$items) { * associative array as described above. * - tabs: A list of (up to 2) tab levels that contain a list of of tabs keyed * by their href, each one being an associative array as described above. - * @param array $router_item - * The menu system router item of the page. - * @param string $root_path - * The path to the root item for this set of tabs. + * @param string $route_name + * The route name of the page. */ -function hook_menu_local_tasks(&$data, $router_item, $root_path) { +function hook_menu_local_tasks(&$data, $route_name) { // Add an action linking to node/add to all pages. $data['actions']['node/add'] = array( '#theme' => 'menu_local_action', @@ -890,8 +888,6 @@ function hook_menu_local_tasks(&$data, $router_item, $root_path) { ), ), ), - // Define whether this link is active. This can usually be omitted. - '#active' => ($router_item['path'] == $root_path), ); } @@ -904,14 +900,12 @@ function hook_menu_local_tasks(&$data, $router_item, $root_path) { * @param array $data * An associative array containing tabs and actions. See * hook_menu_local_tasks() for details. - * @param array $router_item - * The menu system router item of the page. - * @param string $root_path - * The path to the root item for this set of tabs. + * @param string $route_name + * The route name of the page. * * @see hook_menu_local_tasks() */ -function hook_menu_local_tasks_alter(&$data, $router_item, $root_path) { +function hook_menu_local_tasks_alter(&$data, $route_name) { } /** diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php index 6e10a3c..8834b4a 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php @@ -30,7 +30,10 @@ public static function getSubscribedEvents() { */ public function routes(RouteBuildEvent $event) { $collection = $event->getRouteCollection(); - foreach (entity_test_entity_types() as $entity_type) { + $types = entity_test_entity_types(); + $types[] = 'entity_test_render'; + + foreach ($types as $entity_type) { $route = new Route( "$entity_type/add", array('_content' => '\Drupal\entity_test\Controller\EntityTestController::testAdd', 'entity_type' => $entity_type), diff --git a/core/modules/system/tests/modules/menu_test/menu_test.local_tasks.yml b/core/modules/system/tests/modules/menu_test/menu_test.local_tasks.yml index 7d270f3..02ce8f2 100644 --- a/core/modules/system/tests/modules/menu_test/menu_test.local_tasks.yml +++ b/core/modules/system/tests/modules/menu_test/menu_test.local_tasks.yml @@ -52,3 +52,22 @@ menu_local_task_test_upcasting_sub2: title: 'upcasting sub2' tab_root_id: menu_local_task_test_upcasting_sub1 weight: 10 + +menu_test.tasks_default_tab: + route_name: menu_test.tasks_default + title: 'View' + tab_root_id: menu_test.tasks_default_tab + +menu_test.tasks_tasks_tab: + route_name: menu_test.tasks_tasks + title: 'View' + tab_root_id: menu_test.tasks_tasks_tab +menu_test.tasks_edit_tab: + route_name: menu_test.tasks_edit + title: 'Edit' + tab_root_id: menu_test.tasks_tasks_tab +menu_test.tasks_settings_tab: + route_name: menu_test.tasks_settings + title: 'Settings' + weight: 100 + tab_root_id: menu_test.tasks_tasks_tab diff --git a/core/modules/system/tests/modules/menu_test/menu_test.module b/core/modules/system/tests/modules/menu_test/menu_test.module index c75b1c7..566a620 100644 --- a/core/modules/system/tests/modules/menu_test/menu_test.module +++ b/core/modules/system/tests/modules/menu_test/menu_test.module @@ -226,29 +226,10 @@ function menu_test_menu() { 'title' => 'Default only', 'route_name' => 'menu_test.tasks_default', ); - $items['menu-test/tasks/default/view'] = array( - 'title' => 'View', - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); $items['menu-test/tasks/tasks'] = array( 'title' => 'With tasks', 'route_name' => 'menu_test.tasks_tasks', ); - $items['menu-test/tasks/tasks/view'] = array( - 'title' => 'View', - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items['menu-test/tasks/tasks/edit'] = array( - 'title' => 'Edit', - 'type' => MENU_LOCAL_TASK, - 'route_name' => 'menu_test.tasks_edit', - ); - $items['menu-test/tasks/tasks/settings'] = array( - 'title' => 'Settings', - 'type' => MENU_LOCAL_TASK, - 'weight' => 100, - 'route_name' => 'menu_test.tasks_settings', - ); // Menu trail tests. // @see MenuTrailTestCase @@ -364,11 +345,11 @@ function menu_test_local_action_dynamic_title($arg) { * If the menu_test.settings configuration 'tasks.add' has been set, adds * several local tasks to menu-test/tasks. */ -function menu_test_menu_local_tasks(&$data, $router_item, $root_path) { +function menu_test_menu_local_tasks(&$data, $route_name) { if (!\Drupal::config('menu_test.settings')->get('tasks.add')) { return; } - if (strpos($router_item['tab_root'], 'menu-test/tasks/') === 0) { + if (in_array($route_name, array('menu_test.tasks_default', 'menu_test.tasks_empty', 'menu_test.tasks_tasks'))) { $data['tabs'][0]['foo'] = array( '#theme' => 'menu_local_task', '#link' => array( @@ -394,17 +375,17 @@ function menu_test_menu_local_tasks(&$data, $router_item, $root_path) { * If the menu_test.settings configuration 'tasks.alter' has been set, adds * several local tasks to menu-test/tasks. */ -function menu_test_menu_local_tasks_alter(&$data, $router_item, $root_path) { +function menu_test_menu_local_tasks_alter(&$data, $route_name) { if (!\Drupal::config('menu_test.settings')->get('tasks.alter')) { return; } - if (strpos($router_item['tab_root'], 'menu-test/tasks/') === 0) { + if (in_array($route_name, array('menu_test.tasks_default', 'menu_test.tasks_empty', 'menu_test.tasks_tasks'))) { // Rename the default local task from 'View' to 'Show'. // $data['tabs'] is expected to be keyed by link hrefs. // The default local task always links to its parent path, which means that // if the tab root path appears as key in $data['tabs'], then that key is // the default local task. - $key = $router_item['tab_root']; + $key = $route_name . '_tab'; if (isset($data['tabs'][0][$key])) { $data['tabs'][0][$key]['#link']['title'] = 'Show it'; }