I'm trying to print a drupal menu outside drupal. In order to achieve this I ran the bootstrap as follows:


And I render my menu as follows:

public static function getDrupalMenu($menuName) {
        $menu_output = &drupal_static(__FUNCTION__, array());

        if (!isset($menu_output[$menuName])) {
        $tree = menu_tree_all_data($menuName);
        $menu_output[$menuName] = menu_tree_output($tree);
        return render($menu_output[$menuName]);

Which is basically just a copy of the menu_tree function AND with menu_tree_all_data instead of menu_tree_page_data so I get all links no matter what page I'm on.

Everything works fine so far, my menu is printed, everybody is happy... but unfortunately my links are incorrect somehow. To solve this I've used a solution for absolute links as described here:

How to force absolute links on a menu tree in Drupal 7

So I use:

function mythemename_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  $output = l($element['#title'], $element['#href'], array_merge($element['#localized_options'], array('absolute' => TRUE)));
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";

This works fine on my drupal site, but where I call the getDrupalMenu() after initializing the bootstrap it does not work (empty result, not even the original one). I probably have to set the theme or something, but I can't find what or where to do so.

Just to be clear: when I change the code in core (menu.inc) is works fine on both sites... it just gives en empty result when I override it in the theme.

Any pointers or solutions ?

  • The theme should be initialized by doing a full bootstrap, if you check the global $theme variable in your custom code is it setting the theme you have your theme override in?
    – rooby
    Commented Oct 16, 2013 at 13:15
  • Thank you for you pointer. The theme is present in the global $theme variable, so that seems to be okay. I'm pretty sure the mythemename_menu_link is executed as well, because instead of showing menu items like it does when I remove the function, it returns a blank result. However, even when I put return '<li>TEST</li>' it doesn't return something. Also no errors in the logs.
    – JNDPNT
    Commented Oct 16, 2013 at 14:15
  • You can always add die('test'); in your theme override function. That'll at least tell you if it is running. You could also try var_dump() in there to see what variables are there and if they are empty or not.
    – rooby
    Commented Oct 16, 2013 at 14:19

1 Answer 1


While what you are doing could work, this is not what you want to do. If you want to render your menus outside Drupal you should do it a more robust way to implement this using Drupal best practice etc.

One way would be to

  • create a public URL that returns the menus. This could return just the data or markup.
  • Have you custom code consume the API, so you fetch the data via http instead of manually trying to bootstrap Drupal.

You also need to ask yourself why this custom code lives outside Drupal. If you can bootstrap Drupal etc, the code might as well be part of Drupal via a module.

Boostrapping Drupal yourself is possible but a lot of crazy bugs can show up. Drupal depend on a lot of various stuff like $_GET / $_POST and other stuff that you might forget to handle. Also if you update Drupal this code can easily break as you are using internal functions which can change in various ways.

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.