You are here

menu_block_split.module in Menu Block Split 6

Allow to have an splitted menu within two blocks Developed by Robert Garrigos <robert@garrigos.cat> Modified for Drupal 6.x by Frank Meyerer <meyerer@digi-info.de> http://www.digi-info.de

File

menu_block_split.module
View source
<?php

/**
 * @file
 * Allow to have an splitted menu within two blocks
 * Developed by Robert Garrigos <robert@garrigos.cat>
 * Modified for Drupal 6.x by Frank Meyerer <meyerer@digi-info.de>
 * http://www.digi-info.de
 */

/**
 * Since Drupal 6, the breadcrumbs only work for menu items in the 'Navigation' menu
 * The following two function 'fix' this issue to have correct breadcrumbs for ALL menu's
 * See http://drupal.org/node/184955 for more info on this fix.
 *
 * Roel De Meester - http://krimson.be
 */

/**
 * Implementation of hook_init
 */
function menu_block_split_init() {
  $menu_name = menu_block_split_menu_name();
  if ($menu_name) {
    menu_set_active_menu_name($menu_name);
  }
}

/*
 * Returns the menu_name to which the current menu-item belongs. 
 */
function menu_block_split_menu_name() {
  $item = menu_get_item();
  $result = db_query("SELECT menu_name FROM {menu_links} ml where ml.link_path = '%s'", $item['href']);
  while ($item = db_fetch_array($result)) {
    if (!in_array($item['menu_name'], array(
      'navigation',
      'admin_menu',
    ))) {
      return $item['menu_name'];
    }
  }
}

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

/**
 * Implementation of hook_menu().
 */
function menu_block_split_menu() {
  $items = array();
  $items['admin/settings/menu_block_split'] = array(
    'title' => t('Menu block split settings'),
    'description' => t('Settings for Menu Block Split'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'menu_block_split_settings',
    ),
    'access arguments' => array(
      'administer menu block split',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function menu_block_split_theme() {
  return array(
    'menu_block_split_menu' => array(
      'arguments' => array(
        'name',
        'tree',
        'level',
      ),
    ),
  );
}

/**
 * Settings form
 */
function menu_block_split_settings() {
  $form['menu_block_split_howmany'] = array(
    '#type' => 'select',
    '#title' => t('How many blocks with first level menu do you need?'),
    '#default_value' => variable_get('menu_block_split_howmany', 1),
    '#options' => range(0, 10),
    '#description' => t('Set how many first menu level blocks do you need and click on the Save Configuration button to have the form available.'),
  );

  // We don't want zero as an option
  unset($form['menu_block_split_howmany']['#options'][0]);
  $menu_select_options = array();
  foreach (menu_get_menus() as $name => $title) {
    if (count(menu_navigation_links($name)) > 0) {
      $menu_select_options[$name] = $title;
    }
  }
  for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
    $form['menu_block_split_fieldset_' . $i] = array(
      '#type' => 'fieldset',
      '#title' => t('Menu Block Split !i', array(
        '!i' => $i,
      )),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['menu_block_split_fieldset_' . $i]['menu_block_split_' . $i] = array(
      '#type' => 'select',
      '#title' => t('Block' . $i),
      '#options' => $menu_select_options,
      '#default_value' => variable_get('menu_block_split_' . $i, ''),
      '#description' => t('Choose a block as first level menu to split.'),
    );
    $form['menu_block_split_fieldset_' . $i]['menu_block_splittitle_' . $i] = array(
      '#type' => 'textfield',
      '#title' => t('Title') . ' ' . $i,
      '#default_value' => variable_get('menu_block_splittitle_' . $i, ''),
      '#required' => FALSE,
      '#description' => t('Set the title of the resulting block.'),
    );
  }
  return system_settings_form($form);
}

/**
 * Implementation of hook_block().
 */
function menu_block_split_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
        $mid = variable_get('menu_block_split_' . $i, '');
        $item = menu_load($mid);
        $blocks[$i]['info'] = 'Menu block split 1st level (' . $item['title'] . ')';
      }
      $blocks[0]['info'] = 'Menu block split 2nd level';
      return $blocks;
    case 'view':

      // Delta 0 is the 2nd level block
      if ($delta > 0) {
        $tree = menu_tree_page_data(variable_get('menu_block_split_' . $delta, ''));
        $block['subject'] = variable_get('menu_block_splittitle_' . $delta, '');
        $block['content'] = theme('menu_block_split_menu', variable_get('menu_block_split_' . $delta, ''), $tree, $delta);
      }
      else {
        $current_router_item = menu_get_item();
        for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
          $name = variable_get('menu_block_split_' . $i, '');
          $active = menu_get_active_menu_name();
          if ($active != $name) {
            menu_set_active_menu_name($name);
          }
          $trail = menu_get_active_trail();
          $tree = menu_tree_page_data($name);
          $info = menu_block_split_get_first_level($trail);
          $block['subject'] = $info['title'];
          $block['content'] = theme('menu_block_split_menu', $name, $tree, $delta);
        }
      }
      return $block;
      break;
    case 'cache':
      return BLOCK_NO_CACHE;
  }
}

/**
 * Theme menu
 *
 * @param string $name
 * @param array $tree
 *
 * @return menu tree
 */
function theme_menu_block_split_menu($name = 'navigation', $tree, $level) {
  if ($menu = menu_block_split_render_tree($tree, $level)) {
    return $menu;
  }
}

/**
 * Render menu
 *
 * @return menu tree
 */
function menu_block_split_render_tree($tree, $level) {
  $output = '';
  $num_items = count($tree);
  foreach ($tree as $i => $mid) {
    if ($mid['link']['hidden'] == 0) {
      $link = theme('menu_item_link', $mid['link']);

      // our first block
      if ($level > 0) {

        //var_dump($mid);
        $extra_class = 'menu-' . $mid['link']['mlid'];
        $output .= theme('menu_item', $link, '', '', FALSE, $extra_class);
      }
      else {
        if ($mid['below']) {
          $output .= menu_block_split_menu_tree_output($mid['below']);
        }
      }
    }
  }
  if ($output) {
    return "\n<ul class=\"menu\">\n" . $output . "\n</ul>\n";
  }
}

/**
 * Get info about first level.
 *
 * @param array $trail current trail
 *
 * @return title
 */
function menu_block_split_get_first_level($trail) {
  $info = array(
    'mlid' => isset($trail[1]['mlid']) ? $trail[1]['mlid'] : NULL,
    'title' => $trail[1]['title'],
  );
  return $info;
}

/**
 * Implementation of hook_help().
 */
function menu_block_split_help($page, $arg) {
  switch ($page) {
    case 'admin/help#menu_block_split':
      $output = '';
      $output .= '<p>' . t('Menu Block Split allows to split any menu block into two blocks: one with the first level menu entries and a second one for all the sublevels.') . '</p>';
      $output .= '<p>' . t('Once you set how many first level menu blocks you need you can go to the <a href="@blocks">blocks admin page</a> to admin your new blocks. You should find a new Menu Block Split second level block and as many Menu Block Split first level blocks as you set.', array(
        '@blocks' => url('admin/build/block'),
      )) . '</p>';
      return $output;
  }
}

/**
 * outputs a menu tree.
 *
 * @param $
 *   description of parameter.
 * @param $
 *   description of parameter.
 * @param $
 *   description of parameter.
 * @return
 *   description of return value.
 */
function menu_block_split_menu_tree_output($tree) {
  $output = '';
  $items = array();

  // Pull out just the menu items we are going to render so that we
  // get an accurate count for the first/last classes.
  foreach ($tree as $data) {
    if (!$data['link']['hidden']) {
      $items[] = $data;
    }
  }
  $num_items = count($items);
  foreach ($items as $i => $data) {
    $extra_class = NULL;
    if ($i == 0) {
      $extra_class = 'first menu-' . $data['link']['mlid'];
    }
    if ($i == $num_items - 1) {
      $extra_class = 'last menu-' . $data['link']['mlid'];
    }
    $link = theme('menu_item_link', $data['link']);
    if ($data['below']) {
      $output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
    }
    else {
      $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
    }
  }
  return $output ? theme('menu_tree', $output) : '';
}

Functions

Namesort descending Description
menu_block_split_block Implementation of hook_block().
menu_block_split_get_first_level Get info about first level.
menu_block_split_help Implementation of hook_help().
menu_block_split_init Implementation of hook_init
menu_block_split_menu Implementation of hook_menu().
menu_block_split_menu_name
menu_block_split_menu_tree_output outputs a menu tree.
menu_block_split_perm Implementation of hook_perm().
menu_block_split_render_tree Render menu
menu_block_split_settings Settings form
menu_block_split_theme Implementation of hook_theme().
theme_menu_block_split_menu Theme menu