Add element inside main menu block, but outside navigation I need to add an element somewhere within, or directly before/after, the main menu block. For example, the generated HTML should be something like this:

&lt;div id="main-navigation"&gt;
  &lt;div class="block block-system" id="block-system-main-menu"&gt;
    &lt;div class="block-inner clearfix"&gt;
      &lt;div class="content clearfix"&gt;
        &lt;ul class="menu"&gt;
          &lt;li class="first expanded"&gt;
            &lt;a href=""&gt;About&lt;/a&gt;
            &lt;ul class="menu"&gt;
              &lt;li class="first leaf"&gt;
                &lt;a href=""&gt;General Info&lt;/a&gt;
              &lt;/li&gt;
              &lt;li class="leaf"&gt;&lt;a href=""&gt;Video&lt;/a&gt;&lt;/li&gt;
                &lt;!-- ... other menu items --&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;/ul&gt; &lt;!-- this ends the top level main ul.menu --&gt;
        &lt;!-- a div added here would be ok --&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- or a div added here would be ok --&gt;


I am currently trying hook_block_view_alter() and have tried multiple variations of something like this:

function theme_block_view_alter(&amp;$data, $block) {
  if ($block-&gt;delta == 'main-menu'
    &amp;&amp; $block-&gt;region == 'menu') {
    $data['content'][] = array(
      '#markup' =&gt; '&lt;div class="."&gt;&lt;a href="#top"&gt;Close Mobile Menu&lt;/a&gt;&lt;/div&gt;'
    );
  }
}


which is the closest I have come.  This adds the div within ul.menu (top level) after the last li element; that won't work.

I can't seem to find a theming function for the menu as a whole, just theme_menu_links().  I see the theme wrapper menu_tree__main_menu within $data in hook_block_view_alter(), but I don't see a file or function that resembles that.

I'm aware that there may be a much better solution that I'm just not thinking of, so I'll state the final goal:


Absolute positioned menu. (I can work with #main-navigation being absolute and the element coming after it or with ul.menu being positioned, and the element coming after it.)
A link that follows the menu in markup, but is outside of the absolute positioned menu, so that I can position relative to window, not the ul.


Thanks!
To accomplish all your goals, you need to implement these three functions:


hook_preprocess_block()
theme_menu_tree()
theme_menu_link()


Create one template file for the block with your menu.

template.php

/**
 * Implements hook__preprocess_block().
 */
function YOURTHEME_preprocess_block(&amp;$variables, $hook) {
  // allow to use block templates depend on delta
  // example: delta = main-menu ; template file: block--main-menu.tpl.php
 if ($variables['block']-&gt;delta == 'main-menu') {
    $variables['theme_hook_suggestions'][] = 'block__' . str_replace('-', '_', $variables['block']-&gt;delta);
 } 
}

/**
 * Main menu
 * Implements theme__menu_tree().
 */
function YOURTHEME_menu_tree__main_menu($variables) {
    global $level;
    $div = ($level == 1) ? '&lt;div&gt; ... some div next to the main ul ... &lt;/div&gt;' : '';
    $ul = '&lt;ul class="menu"&gt;' . $variables['tree'] . '&lt;/ul&gt;' . $div;
    return $ul;
}

/**
 * Main menu
 * Implements theme__menu_link().
 */
function YOURTHEME_menu_link__main_menu($variables) {

  $element = $variables['element'];
  $title = $element['#title'];
  $sub_menu = '';

  // global menu level variable. 
  // within theme_menu_tree() at first level has always value = 1
  // but not here, that's why $depth will have to be defined separately
  global $level;
  $level = $element['#original_link']['depth'];

  // if you will need $depth, it must be defined separately
  // $depth = $element['#original_link']['depth']; 

  // submenu
  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }

  // link output
  $output = l($title, $element['#href'], $element['#localized_options']);

  // if link class is active, make li class as active too
  if(strpos($output,"active")&gt;0){
    $element['#attributes']['class'][] = "active";
  }

  $attr = drupal_attributes($element['#attributes']);
  return  '&lt;li' . $attr . '&gt;' . $output .$sub_menu . "&lt;/li&gt;\n";
}


block--main-menu.tpl.php

&lt;?php
/**
 * @file
 * theme's implementation to display a block with the main menu.
 */     
?&gt;
&lt;div id="main-navigation"&gt;
  &lt;div class="block block-system" id="block-system-main-menu"&gt;
    &lt;div class="block-inner clearfix"&gt;
      &lt;div class="content clearfix"&gt;
        &lt;?php print $content; ?&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;!-- or a div added here would be ok --&gt;
&lt;div&gt; ... some div next to the main navigation div ... &lt;/div&gt;


Results