You are here

menu_block.module in Menu Block 5

File

menu_block.module
View source
<?php

/**
 * Implementation of hook_perm().
 */
function menu_block_perm() {
  return array(
    'administer menu block',
  );
}

/**
 * Implementation of hook_menu().
 */
function menu_block_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/build/menu-block',
      'title' => t('Menu blocks'),
      'description' => t('Enable blocks to appear on the <a href="!url">administer blocks page</a>.', array(
        '!url' => url('admin/build/block'),
      )),
      'access' => user_access('administer menu block'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'menu_block_settings',
    );
  }
  else {
    drupal_add_css(drupal_get_path('module', 'menu_block') . '/menu_block.css');
  }
  return $items;
}

/**
 * Display the settings form for Menu block.
 *
 * @return
 *   The form array.
 */
function menu_block_settings() {

  // The settings form is seldom used, so we store it in a separate file.
  include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
  return _menu_block_settings();
}
function menu_block_settings_submit($form_id, $form_values) {
  include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
  return _menu_block_settings_submit($form_id, $form_values);
}

/**
 * Implementation of hook_block().
 */
function menu_block_block($op = 'list', $delta = NULL, $edit = NULL) {
  $function = '_menu_block_block_' . $op;
  if (function_exists($function)) {
    return $function($delta, $edit);
  }
  else {

    // "op"s besides "view" are seldom used, so we store them in a separate file.
    include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
    if (function_exists($function)) {
      return $function($delta, $edit);
    }
  }
}

/**
 * Returns the 'view' $op info for hook_block().
 *
 * @param $delta
 *   string The name of the block to render.
 */
function _menu_block_block_view($delta) {
  $data = array();

  // Determine which menu and what level of the menu has been requested.
  $mid = variable_get("menu_block_{$delta}_mid", 1);
  $level = variable_get("menu_block_{$delta}_level", 1);

  // Render the block if the active menu item is in this menu.
  if ($level == 1 || menu_in_active_trail($mid)) {
    if ($level != 1) {

      // Get the menu item of the n-level link in the active trail.
      $active_trail = _menu_get_active_trail();
      $mid = $active_trail[$level - 1];
    }
    $menu_item = menu_get_item($mid);
    $data['subject'] = check_plain($menu_item['title']);
    $depth = variable_get("menu_block_{$delta}_depth", 0);
    if ($depth == 1) {
      $data['content'] = theme('menu_block_list', $mid);
    }
    else {
      $expanded = variable_get("menu_block_{$delta}_expanded", 0);
      $data['content'] = theme('menu_block_tree', $mid, $depth, $expanded);
    }
  }
  return $data;
}

/**
 * Generate the HTML for a menu tree.
 *
 * @ingroup themeable
 *
 * @param $pid
 *   int The parent id of the menu.
 * @param $depth_limit
 *   int The maximum depth of the returned tree, 0 for unlimited.
 * @param $expanded
 *   boolean Whether to expand the entire menu tree.
 * @return
 *   string The rendered menu tree.
 */
function theme_menu_block_tree($pid = 1, $depth_limit = 0, $expanded = FALSE) {
  if ($tree = menu_block_tree($pid, $depth_limit, $expanded)) {
    return '<ul class="menu menu-tree menu-' . $pid . '">' . $tree . '</ul>';
  }
  else {
    return '';
  }
}

/**
 * Generate the HTML for a menu list.
 *
 * @ingroup themeable
 *
 * @param $pid
 *   int The parent id of the menu.
 * @return
 *   string The rendered menu list.
 */
function theme_menu_block_list($pid = 1) {
  if ($list = menu_block_tree($pid, 1, FALSE, TRUE)) {
    return '<ul class="menu-list menu-' . $pid . '">' . $list . '</ul>';
  }
  else {
    return '';
  }
}

/**
 * Returns a rendered menu tree or menu list.
 *
 * @param $pid
 *   int The parent id of the menu.
 * @param $depth_limit
 *   int The maximum depth of the returned tree, 0 for unlimited.
 * @param $expanded
 *   boolean Whether to expand the entire menu tree.
 * @return
 *   string The rendered items of a menu.
 */
function menu_block_tree($pid = 1, $depth_limit = 0, $expanded = FALSE) {
  $menu = menu_get_menu();
  $output = '';
  if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
    $active_id = menu_get_active_item();
    $count = 1;
    $total_children = count($menu['visible'][$pid]['children']);
    foreach ($menu['visible'][$pid]['children'] as $mid) {

      // Theme the menu link
      $in_active_trail = menu_in_active_trail_in_submenu($mid, $pid);
      $item = menu_get_item($mid);
      $item['attributes'] = array();
      if (!empty($item['description'])) {
        $item['attributes']['title'] = $item['description'];
      }
      if ($in_active_trail) {
        $item['attributes']['class'] = 'active-trail';
      }
      while ($item['type'] & MENU_LINKS_TO_PARENT) {

        // Weirdness in D5's menu system
        $link_item = menu_get_item($item['pid']);
        $item['path'] = $link_item['path'];
      }
      $link = theme('menu_block_item_link', $item);

      // Theme the menu tree containing the children
      $has_children = !empty($menu['visible'][$mid]['children']);
      $children = '';
      if ($has_children) {
        $type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : FALSE;
        if ($depth_limit != 1 && ($type & MENU_EXPANDED || $expanded || $in_active_trail)) {
          $children = theme('menu_block_tree', $mid, $depth_limit ? $depth_limit - 1 : 0, $expanded);
        }
      }

      // Theme the menu item
      $extra_class = "menu-{$mid}";
      $extra_class .= $count == 1 ? ' first' : '';
      $extra_class .= $count == $total_children ? ' last' : '';
      $extra_class .= $mid == $active_id || $item['path'] == '<front>' && drupal_is_front_page() ? ' active' : '';
      $output .= theme('menu_block_item', $link, $has_children, $children, $in_active_trail, $extra_class);
      $count++;
    }
  }
  return $output;
}

/**
 * Generate the HTML output for a single menu link.
 *
 * @ingroup themeable
 *
 * @param $link
 *   array The menu item to render.
 * @return
 *   string The rendered menu link.
 */
function theme_menu_block_item_link($link) {
  if (empty($link['attributes'])) {
    $link['attributes'] = array();
  }
  return l($link['title'], $link['path'], $link['attributes'], isset($link['query']) ? $link['query'] : NULL);
}

/**
 * Generate the HTML output for a menu item and submenu.
 *
 * @ingroup themeable
 */
function theme_menu_block_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  $class = $menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf');
  if (!empty($extra_class)) {
    $class .= ' ' . $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
  return '<li class="' . $class . '">' . $link . $menu . "</li>";
}

Functions

Namesort descending Description
menu_block_block Implementation of hook_block().
menu_block_menu Implementation of hook_menu().
menu_block_perm Implementation of hook_perm().
menu_block_settings Display the settings form for Menu block.
menu_block_settings_submit
menu_block_tree Returns a rendered menu tree or menu list.
theme_menu_block_item Generate the HTML output for a menu item and submenu.
theme_menu_block_item_link Generate the HTML output for a single menu link.
theme_menu_block_list Generate the HTML for a menu list.
theme_menu_block_tree Generate the HTML for a menu tree.
_menu_block_block_view Returns the 'view' $op info for hook_block().