2

Is there a way to print out an array of the entirety of a menu in Drupal 7?

For example, I have access controls in place so that a user gets a role after they take a quiz, which then allows them to see the next quiz. So until they pass quiz #1, their menu will not show them any evidence of quiz #2's existence.

What I want to do is print out the titles of ALL menu items, regardless of whether the user has access to the associated nodes or not. I plan to rewrite the menu to make the permitted items links, while the others appear greyed out until the user gains access to them.

I have not been able to find any modules, and simply doing this:

print_r(menu_navigation_links('menu-program-menu'));

only shows the items that the user has permission to view.

Is there a module already that does this, that I just haven't been able to find? Does a query have to be made directly to the database or is there a way to do this with more finesse?

2 Answers 2

1

Besides writing custom database queries you might be able to use functions listed on the Menu API page. For example menu_load_links() loads all the links of the specified menu. menu_link_load() can be used to get additional (including access) information besides the general link data (thanks, beth, for pointing this out!). You could also use the code of the listed functions to create your own similar functions without the access controls.

2
  • 1
    Thank you, this is what I have been digging for! Only one thing, in looking at the output of that function, I don't see information about user permissions for each link. Am I missing something? Or should I use menu_link_load() for each mlid that is returned by menu_load_links()?
    – beth
    Commented May 9, 2012 at 13:18
  • You're right, menu_load_links() does not retrieve any access information. Like you already figured out using the mlid's and menu_link_load() should cover your needs though.
    – Madis
    Commented May 9, 2012 at 20:57
1

What you can do is to select corresponding titles from the database directly:

$sql = "SELECT l.link_title FROM {menu_links} l WHERE l.menu_name = :menu_name ";
$result = db_query($sql, array(':menu_name' => 'menu-program-menu'));
foreach($result as $object) {
    print $object->link_title;
}
2
  • That does indeed work! Though I think there must be an API or something somewhere that give you access to this. Is this the only way to do it? Anyway thank you! Did you consult documentation for this, and if so, might I have a link?
    – beth
    Commented May 8, 2012 at 21:16
  • No I didn't consult any documentation for this. I just opened phpmyadmin and had a quick look to recall how menu links are stored in the db and then wrote this piece of code. But you might find useful drupal.org/developing/api/database it explains how to query the database :) Commented May 8, 2012 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.