1

I need to access the raw menu data in a menu block, but $content only stores the rendered markup. How can I gain access to this object from within the block--sidebar_second.tpl.php file?

I've looked into creating a View but I couldn't figure out how to select menu nodes. Either solution will work for me...

Here is the output of the $block object:

stdClass Object
(
    [bid] => 196
    [module] => menu_block
    [delta] => 7
    [theme] => theme-name
    [status] => 1
    [weight] => 0
    [region] => sidebar_second
    [custom] => 0
    [visibility] => 0
    [pages] => 
    [title] => Services Menu
    [cache] => -1
    [subject_array] => Array
        (
            [#markup] => Services
        )

    [subject] => Services
    [last_in_region] => 1
)
1

If you need the menu array structure in your block theme template, you need to implement a template_preprocess_block() in your theme's template.php:

//@TODO: replace MYTHEME with your theme name
function MYTHEME_preprocess_block(&$variables) {
  //Only add on sidebar_second region
  if($variables['block']->region == 'sidebar_second') {
    //Replace "my-menu" with the machine name of your menu
    $variables['menu'] = menu_tree_all_data('my-menu');
  }
}
//after flushing caches $menu should be available in your block template.
2

It sounds like you are using Menu block module, and you would have already successfully placed the menu-tree in your region. If your desire is to access raw menu data from a menu block, just use TEMPLATE_preprocess_block(&$variables).

$variables['content'] is rendered.
$variables['elements']['#content'] is raw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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