From 1d72b6ec9f5b577d976b51498a7212d112c5cc33 Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Sun, 11 Oct 2009 06:05:53 +0000 Subject: #599706 by sun: Allow altering local tasks/actions. --- includes/common.inc | 4 +-- includes/menu.inc | 66 ++++++++++++++++++++++++++++++--------------- modules/menu/menu.api.php | 63 +++++++++++++++++++++++++++++++++++++++++++ modules/system/page.tpl.php | 12 ++++----- themes/garland/page.tpl.php | 6 ++--- themes/seven/page.tpl.php | 6 ++--- 6 files changed, 122 insertions(+), 35 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index c264147e9..8532e46c7 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4939,10 +4939,10 @@ function drupal_common_theme() { 'arguments' => array('tree' => NULL), ), 'menu_local_task' => array( - 'arguments' => array('link' => NULL, 'active' => FALSE), + 'arguments' => array('element' => NULL), ), 'menu_local_action' => array( - 'arguments' => array('link' => NULL), + 'arguments' => array('element' => NULL), ), 'menu_local_tasks' => array( 'arguments' => array(), diff --git a/includes/menu.inc b/includes/menu.inc index eba2a9036..076bcc6f6 100644 --- a/includes/menu.inc +++ b/includes/menu.inc @@ -1349,29 +1349,29 @@ function theme_menu_link(array $variables) { * * @param $variables * An associative array containing: - * - link: A menu link array with 'title', 'href', and 'localized_options' + * - #link: A menu link array with 'title', 'href', and 'localized_options' * keys. - * - active: A boolean indicating whether the local task is active. + * - #active: A boolean indicating whether the local task is active. * * @ingroup themeable */ function theme_menu_local_task($variables) { - $link = $variables['link']; - return '
  • ' . l($link['title'], $link['href'], $link['localized_options']) . "
  • \n"; + $link = $variables['element']['#link']; + return '' . l($link['title'], $link['href'], $link['localized_options']) . "\n"; } /** * Generate the HTML output for a single local action link. * - * @param $variables + * @param $element * An associative array containing: - * - link: A menu link array with 'title', 'href', and 'localized_options' + * - #link: A menu link array with 'title', 'href', and 'localized_options' * keys. * * @ingroup themeable */ function theme_menu_local_action($variables) { - $link = $variables['link']; + $link = $variables['element']['#link']; return '
  • ' . l($link['title'], $link['href'], $link['localized_options']) . "
  • \n"; } @@ -1564,8 +1564,8 @@ function menu_local_tasks($level = 0) { $data = &drupal_static(__FUNCTION__); $root_path = &drupal_static(__FUNCTION__ . ':root_path', ''); $empty = array( - 'tabs' => array('count' => 0, 'output' => ''), - 'actions' => array('count' => 0, 'output' => ''), + 'tabs' => array('count' => 0, 'output' => array()), + 'actions' => array('count' => 0, 'output' => array()), 'root_path' => &$root_path, ); @@ -1606,8 +1606,8 @@ function menu_local_tasks($level = 0) { // equal the depth. Thus we use the $depth counter (offset by 1000 for ksort). $depth = 1001; while (isset($children[$path])) { - $tabs_current = ''; - $actions_current = ''; + $tabs_current = array(); + $actions_current = array(); $next_path = ''; $tab_count = 0; $action_count = 0; @@ -1620,17 +1620,27 @@ function menu_local_tasks($level = 0) { for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']); // Use the path of the parent instead. $link['href'] = $tasks[$p]['href']; - $tabs_current .= theme('menu_local_task', array('link' => $link, 'active' => TRUE)); + $tabs_current[] = array( + '#theme' => 'menu_local_task', + '#link' => $link, + '#active' => TRUE, + ); $next_path = $item['path']; $tab_count++; } else { if ($item['type'] == MENU_LOCAL_TASK) { - $tabs_current .= theme('menu_local_task', array('link' => $link)); + $tabs_current[] = array( + '#theme' => 'menu_local_task', + '#link' => $link, + ); $tab_count++; } else { - $actions_current .= theme('menu_local_action', array('link' => $link)); + $actions_current[] = array( + '#theme' => 'menu_local_action', + '#link' => $link, + ); $action_count++; } } @@ -1650,7 +1660,7 @@ function menu_local_tasks($level = 0) { $current = $router_item; $depth = 1000; while (isset($children[$parent])) { - $tabs_current = ''; + $tabs_current = array(); $next_path = ''; $next_parent = ''; $count = 0; @@ -1672,14 +1682,21 @@ function menu_local_tasks($level = 0) { } // We check for the active tab. if ($item['path'] == $path) { - $tabs_current .= theme('menu_local_task', array('link' => $link, 'active' => TRUE)); + $tabs_current[] = array( + '#theme' => 'menu_local_task', + '#link' => $link, + '#active' => TRUE, + ); $next_path = $item['tab_parent']; if (isset($tasks[$next_path])) { $next_parent = $tasks[$next_path]['tab_parent']; } } else { - $tabs_current .= theme('menu_local_task', array('link' => $link)); + $tabs_current[] = array( + '#theme' => 'menu_local_task', + '#link' => $link, + ); } } } @@ -1694,6 +1711,9 @@ 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 alter local tasks or dynamically append further tasks. + drupal_alter('menu_local_tasks', $data, $router_item, $root_path); } if (isset($data['tabs'][$level])) { @@ -1741,18 +1761,22 @@ function menu_tab_root_path() { } /** - * Returns the rendered local tasks. The default implementation renders them as tabs. + * Returns renderable local tasks. * * @ingroup themeable */ function theme_menu_local_tasks() { - $output = ''; + $output = array(); if ($primary = menu_primary_local_tasks()) { - $output .= "\n"; + $primary['#prefix'] = ''; + $output[] = $primary; } if ($secondary = menu_secondary_local_tasks()) { - $output .= "\n"; + $secondary['#prefix'] = ''; + $output[] = $secondary; } return $output; diff --git a/modules/menu/menu.api.php b/modules/menu/menu.api.php index 599a4a124..6a3405d9e 100644 --- a/modules/menu/menu.api.php +++ b/modules/menu/menu.api.php @@ -429,6 +429,69 @@ function hook_menu_delete($menu) { variable_set('my_module_menus', $my_menus); } +/** + * Alter tabs and actions displayed on the page before they are rendered. + * + * This hook is invoked by menu_local_tasks(). The system-determined tabs and + * actions are passed in by reference. Additional tabs or actions may be added, + * or existing items altered. + * + * Each tab or action is an associative array containing: + * - #theme: The theme function to use to render. + * - #link: An associative array containing: + * - title: The localized title of the link. + * - href: The system path to link to. + * - localized_options: An array of options to pass to url(). + * - #active: Whether the link should be marked as 'active'. + * + * @param $data + * An associative array containing: + * - actions: An associative array containing: + * - count: The amount of actions determined by the menu system, which can + * be ignored. + * - output: A list of of actions, each one being an associative array + * as described above. + * - tabs: An indexed array (list) of tab levels (up to 2 levels), each + * containing an associative array: + * - count: The amount of tabs determined by the menu system. This value + * does not need to be altered if there is more than one tab. + * - output: A list of of tabs, each one being an associative array as + * described above. + */ +function hook_menu_local_tasks_alter(&$data, $router_item, $root_path) { + // Add an action linking to node/add to all pages. + $data['actions']['output'][] = array( + '#theme' => 'menu_local_task', + '#link' => array( + 'title' => t('Add new content'), + 'href' => 'node/add', + 'localized_options' => array( + 'attributes' => array( + 'title' => t('Add new content'), + ), + ), + ), + ); + + // Add a tab linking to node/add to all pages. + $data['tabs'][0]['output'][] = array( + '#theme' => 'menu_local_task', + '#link' => array( + 'title' => t('Example tab'), + 'href' => 'node/add', + 'localized_options' => array( + 'attributes' => array( + 'title' => t('Add new content'), + ), + ), + ), + // Define whether this link is active. This can be omitted for + // implementations that add links to pages outside of the current page + // context. + '#active' => ($router_item['path'] == $root_path), + ); +} + /** * @} End of "addtogroup hooks". */ diff --git a/modules/system/page.tpl.php b/modules/system/page.tpl.php index 2c4c8887e..bb2a6d3a3 100644 --- a/modules/system/page.tpl.php +++ b/modules/system/page.tpl.php @@ -31,14 +31,14 @@ * - $secondary_menu (array): An array containing the Secondary menu links for * the site, if they have been configured. * - $breadcrumb: The breadcrumb trail for the current page. - * - $action_links: Actions local to the page, such as 'Add menu' on the menu - * administration interface. * * Page content (in order of occurrence in the default page.tpl.php): * - $title: The page title, for use in the actual HTML content. * - $messages: HTML for status and error messages. Should be displayed prominently. - * - $tabs: Tabs linking to any sub-pages beneath the current page (e.g., the view - * and edit tabs when displaying a node). + * - $tabs (array): Tabs linking to any sub-pages beneath the current page + * (e.g., the view and edit tabs when displaying a node). + * - $action_links (array): Actions local to the page, such as 'Add menu' on the + * menu administration interface. * - $feed_icons: A string of all feed icons for the current page. * * Regions: @@ -107,9 +107,9 @@

    -
    +
    - +
    diff --git a/themes/garland/page.tpl.php b/themes/garland/page.tpl.php index ca3b563b8..a3a1cf30e 100644 --- a/themes/garland/page.tpl.php +++ b/themes/garland/page.tpl.php @@ -33,11 +33,11 @@
    > -
    - + + - +
    diff --git a/themes/seven/page.tpl.php b/themes/seven/page.tpl.php index d5d6b7b49..eee8cc667 100644 --- a/themes/seven/page.tpl.php +++ b/themes/seven/page.tpl.php @@ -4,11 +4,11 @@

    -
    +
    -
    +
    @@ -19,7 +19,7 @@
    - +
    -- cgit v1.2.3