Menu Links

// Do nothing unless URL parameter 'm' is supplied, e.g. www.iac.org/menu-links?m=Programs
if (!isset($_GET['m'])) return;

// Get the menu link ID associated with the menu named in the 'm' URL parameter
// Note: We could error-check here, but we don't
$res = db_query("SELECT mlid FROM menu_links WHERE menu_name = 'main-menu' AND link_title LIKE :title LIMIT 1", array(':title' => $_GET['m'] . '%'))->fetchObject();

// Get the paths to the menu items whose parent is the menu link ID we extracted in the previous query,
// sorted by weight (so the order of the bullet list items will match the menu)
$r2 = db_query("SELECT link_path, link_title FROM menu_links WHERE plid = :mlid ORDER BY weight", array(':mlid' => $res->mlid));

// Modify the page ititle
drupal_set_title("Menu: " . $_GET['m']);

// Start the bullet list
print "

    \n";

    // Loop through the menu items
    foreach ($r2 as $item) {

    // Link paths are in the form "node/nnnn", where nnnn is a node number (nid).
    // Split the path at the slash
    $pes = explode('/', $item->link_path);
    // watchdog('iac', print_r($pes, TRUE)); // !!!

    // Find the node title
    if ($pes[0] == "node") { // internal link, use node title
    if ($pes[1] == 'add') {
    $t = 'Add ' . $pes[2];
    } else {
    $t = db_query("SELECT title FROM node WHERE nid = :nid LIMIT 1", array(':nid' => $pes[1]))->fetchObject();
    $t = $t->title;
    }
    } else { // external link, use menu title
    $t = $item->link_title;
    }

    // Find the node alias
    $a = db_query("SELECT alias FROM url_alias WHERE source = :path LIMIT 1", array(':path' => $item->link_path))->fetchObject();

    // Generate a bullet item consisting of the node title, which is hyperlinked to the page
    // Note that some links (e.g. external pages) don't have an alias, hence the "ternary if" statement
    print "

  • " . $t . "
  • \n";

    }

    // Close the bullet list
    print "

\n";