You are here

jump_menu.module in Better Jump Menus 6

Same filename and directory in other branches
  1. 8 jump_menu.module
  2. 7 jump_menu.module

Make use of the CTools jump menu and grabs from an existing menu. See: modules/ctools/includes/jump-menu.inc NOTE: Menu items must be checked as "expanded" for traversing to work.

File

jump_menu.module
View source
<?php

/**
 * @file
 * Make use of the CTools jump menu and grabs from an existing menu.
 * See: modules/ctools/includes/jump-menu.inc
 * NOTE: Menu items must be checked as "expanded" for traversing to work.
 */

/**
 * Main constructor function.
 */
function jump_menu($menu, $parent, $btn = FALSE, $maxDepth = 0, $choose = 'Select a destination') {
  ctools_include('jump-menu');

  // Load up the menu.
  $menu = menu_tree_all_data($menu);

  // Trim to the needed portion, start at parent menuID.
  foreach ($menu as $m) {
    if ($m['link']['mlid'] == $parent) {

      // mlid is i18n tranlsation friendly.
      $menu = $m['below'];
      break;
    }
  }

  // Initialize for building.
  $depths = array(
    'current' => 1,
    'max' => $maxDepth,
  );
  $targets = array();

  // Build the jump options from the menu
  _jump_menu_create_options($targets, $menu, $depths);

  // Output...
  if (count($targets) == 0) {
    return 'Jump menu contains no items!';
  }
  else {
    $options = array();

    // Handle button option.
    if ($btn) {
      $options['hide'] = false;
      $options['button'] = $btn;
    }
    else {
      $options['hide'] = true;
    }

    // Place initial select option value.
    $options['choose'] = t($choose);

    // Other available options...

    //'title' => The text to display for the #title attribute.

    //'description': The text to display for the #description attribute.

    //'default_value': The text to display for the #default_value attribute.

    //'image': If set, an image button will be used instead, and the image set to this.

    //'inline': If set to TRUE (default) the display will be forced inline.
    return drupal_get_form('ctools_jump_menu', $targets, $options);
  }
}

/**
 * Recursive menu to select option building.
 */
function _jump_menu_create_options(&$t, &$m, &$d) {
  foreach ($m as $item) {

    // Set the option.
    if ($item['link']['hidden'] == 0) {

      // Kill non-viewable menu items.
      // Add depth indicators to titles.
      if ($d['current'] > 1) {
        $title = ' ' . str_repeat('-', $d['current']) . ' ' . $item['link']['title'];
      }
      else {
        $title = $item['link']['title'];
      }

      // Add targets.
      // Allow for special menu item dummy items for grouping.
      if (module_exists('special_menu_items') && $item['link']['page_callback'] == 'special_menu_items_dummy') {

        // Create a dummy option using optgroups.
        $t[t($title)] = array();
      }
      else {

        // Create a normal option.
        $t[url($item['link']['href'])] = t($title);
      }
    }

    // Loop deeper if there is no max or we haven't reached it.
    if ($item['below'] && ($d['max'] == 0 || $d['current'] < $d['max'])) {
      $d['current']++;

      // Drop current depth.
      _jump_menu_create_options($t, $item['below'], $d);
    }
  }
  $d['current']--;

  // Raise current depth back up.
}

/**
 * Create jump blocks for all menus.
 */
function jump_menu_block($op = 'list', $delta = 0) {
  $menus = menu_get_menus();
  switch ($op) {
    case 'list':
      $blocks = array();

      // Create block for each menu.
      foreach ($menus as $name => $title) {
        $blocks['jump_menu_' . $name]['info'] = 'Jump Menu: ' . check_plain($title);

        // Menu blocks can't be cached because each menu item can have
        // a custom access callback. menu.inc manages its own caching.
        $blocks['jump_menu_' . $name]['cache'] = BLOCK_NO_CACHE;
      }
      return $blocks;
      break;
    case 'view':
      $menuName = str_replace('jump_menu_', '', $delta);

      // Strip off jump_menu.
      $data['subject'] = check_plain($menus[$menuName]);
      $data['content'] = jump_menu($menuName, 0, FALSE, 0, '-- Select destination --');
      return $data;
      break;
  }
}

Functions

Namesort descending Description
jump_menu Main constructor function.
jump_menu_block Create jump blocks for all menus.
_jump_menu_create_options Recursive menu to select option building.