Skip to content
Snippets Groups Projects
Commit a5e048a5 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Renamed some menu items for clarity and ... added basic phpDoc comments.
parent 2fe79310
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -2,7 +2,7 @@
// $Id$
/**
* Register a menu item to the menu system.
* Register a menu item with the menu system.
*/
function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidden = 0) {
global $_list;
......@@ -12,99 +12,121 @@ function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidde
}
/**
* Returns the path of the active menu item.
* Returns an array with the menu items that lead to the specied path.
*/
function menu_get_active() {
function menu_get_trail($path) {
global $_list;
static $path;
if (empty($path)) {
$path = $_GET["q"];
$trail = array();
while ($path && !$_list[$path]) {
$path = substr($path, 0, strrpos($path, "/"));
while ($path) {
if ($_list[$path]) {
array_unshift($trail, $path);
}
$path = substr($path, 0, strrpos($path, "/"));
}
return $path;
return $trail;
}
function menu_get_path($path) {
/**
* Returns the path of the active menu item.
*/
function menu_get_active_item() {
global $_list;
static $trail; // cache
if (empty($trail)) {
$trail = array();
static $path;
while ($path) {
if ($_list[$path]) {
array_unshift($trail, $path);
}
if (empty($path)) {
$path = $_GET["q"];
while ($path && !$_list[$path]) {
$path = substr($path, 0, strrpos($path, "/"));
}
}
return $trail;
return $path;
}
/**
* Returns the title of the active menu item.
*/
function menu_get_active_title() {
global $_list;
if ($path = menu_get_active()) {
if ($path = menu_get_active_item()) {
return ucfirst($_list[$path]["title"]);
}
}
/**
* Returns the help associated with the active menu item.
*/
function menu_get_active_help() {
global $_list;
if ($path = menu_get_active()) {
if ($path = menu_get_active_item()) {
return $_list[$path]["help"];
}
}
function menu_is_active($path) {
}
/**
* Returns an array of rendered menu items in the active breadcrumb trail.
*/
function menu_get_active_breadcrumb() {
function menu_render_item($path) {
global $_list;
$links[] = l(t("Home"), "");
if ($path == $_GET["q"]) {
$css = " class=\"active\"";
$trail = menu_get_trail($_GET["q"]);
foreach ($trail as $item) {
$links[] = _render_item($item);
}
return "<a href=\"". url($path) ."\"$css>". t($_list[$path]["title"]) ."</a>";
return $links;
}
function menu_active_breadcrumb() {
$links[] = l(t("Home"), "");
/**
* Execute the handler associated with the active menu item.
*/
function menu_execute_active_handler() {
global $_list;
$trail = menu_get_path($_GET["q"]);
foreach ($trail as $item) {
$links[] = menu_render_item($item);
}
$path = menu_get_active_item();
return $links;
if ($_list[$path]["callback"]) {
$arg = substr($_GET["q"], strlen($path) + 1);
if (empty($arg)) {
return call_user_func($_list[$path]["callback"]);
}
else {
return call_user_func_array($_list[$path]["callback"], explode("/", $arg));
}
}
}
function _menu_sort($a, $b) {
global $_list;
/**
* Returns true when the path is in the active trail.
*/
function menu_in_active_trail($path) {
static $trail;
$a = &$_list[$a];
$b = &$_list[$b];
if (empty($trail)) {
$trail = menu_get_trail($_GET["q"]);
}
return $a["weight"] < $b["weight"] ? -1 : ($a["weight"] > $b["weight"] ? 1 : ($a["title"] < $b["title"] ? -1 : 1));
return in_array($path, $trail);
}
/**
* Returns a rendered menu tree.
*/
function menu_tree($parent = "") {
global $_list;
static $trail;
if (empty($tail)) {
$trail = menu_get_path($_GET["q"]);
$trail = menu_get_trail($_GET["q"]);
}
if ($_list[$parent]["children"]) {
......@@ -112,10 +134,10 @@ function menu_tree($parent = "") {
usort($_list[$parent]["children"], "_menu_sort");
foreach ($_list[$parent]["children"] as $item) {
if ($_list[$item]["hidden"] == 0) {
$style = ($_list[$item]["children"] ? (in_array($item, $trail) ? "expanded" : "collapsed") : "leaf");
$style = ($_list[$item]["children"] ? (menu_in_active_trail($item) ? "expanded" : "collapsed") : "leaf");
$output .= "<li class=\"$style\">";
$output .= menu_render_item($item);
if (in_array($item, $trail)) {
$output .= _render_item($item);
if (menu_in_active_trail($item, $trail)) {
$output .= menu_tree($item);
}
$output .= "</li>\n";
......@@ -127,23 +149,9 @@ function menu_tree($parent = "") {
return $output;
}
function menu_execute_action() {
global $_list;
$path = menu_get_active();
if ($_list[$path]["callback"]) {
$arg = substr($_GET["q"], strlen($path) + 1);
if (empty($arg)) {
return call_user_func($_list[$path]["callback"]);
}
else {
return call_user_func_array($_list[$path]["callback"], explode("/", $arg));
}
}
}
/**
* Query to module to build the menu.
*/
function menu_build($type) {
/*
** Build a sequential list of all menus items.
......@@ -175,4 +183,24 @@ function menu_build($type) {
}
}
function _menu_sort($a, $b) {
global $_list;
$a = &$_list[$a];
$b = &$_list[$b];
return $a["weight"] < $b["weight"] ? -1 : ($a["weight"] > $b["weight"] ? 1 : ($a["title"] < $b["title"] ? -1 : 1));
}
function _render_item($path) {
global $_list;
if ($path == $_GET["q"]) {
$css = " class=\"active\"";
}
return "<a href=\"". url($path) ."\"$css>". t($_list[$path]["title"]) ."</a>";
}
?>
......@@ -47,14 +47,14 @@ function admin_page() {
$contents = "<small>$help</small><hr />";
}
if (arg(1)) {
$contents .= menu_execute_action();
$contents .= menu_execute_active_handler();
}
else {
$contents.= watchdog_overview("actions");
$title = t("System messages");
}
$breadcrumb = menu_active_breadcrumb();
$breadcrumb = menu_get_active_breadcrumb();
array_pop($breadcrumb);
$title = menu_get_active_title();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment