You are here

function menu_tree_build in Menu Block 6.2

Same name and namespace in other branches
  1. 7.3 menu_block.module \menu_tree_build()
  2. 7.2 menu_block.module \menu_tree_build()

Build a menu tree based on the provided configuration.

Parameters

$config: array An array of configuration options that specifies how to build the menu tree and its title.

  • delta: (string) The menu_block's block delta.
  • menu_name: (string) The machine name of the requested menu. Can also be set to MENU_TREE__CURRENT_PAGE_MENU to use the menu selected by the page.
  • parent_mlid: (int) The mlid of the item that should root the tree. Use 0 to use the menu's root.
  • title_link: (boolean) Specifies if the title should be rendered as a link or a simple string.
  • admin_title: (string) An optional title to uniquely identify the block on the administer blocks page.
  • level: (int) The starting level of the tree.
  • follow: (string) Specifies if the starting level should follow the active menu item. Should be set to 0, 'active' or 'child'.
  • depth: (int) The maximum depth the tree should contain, relative to the starting level.
  • expanded: (boolean) Specifies if the entire tree be expanded or not.
  • sort: (boolean) Specifies if the tree should be sorted with the active trail at the top of the tree.

Return value

array An array containing the rendered tree in the 'content' key and the rendered title in the 'subject' key.

3 calls to menu_tree_build()
menu_block_menu_tree_content_type_admin_info in plugins/content_types/menu_tree/menu_tree.inc
Callback to provide administrative info (the preview in panels when building a panel).
menu_block_menu_tree_content_type_render in plugins/content_types/menu_tree/menu_tree.inc
Renders a menu_tree content type based on the delta supplied in the configuration.
_menu_block_block_view in ./menu_block.module
Returns the 'view' $op info for hook_block().

File

./menu_block.module, line 238
Provides configurable blocks of menu items.

Code

function menu_tree_build($config) {

  // Retrieve the active menu item from the database.
  if ($config['menu_name'] == MENU_TREE__CURRENT_PAGE_MENU) {

    // Retrieve the list of available menus.
    $menu_order = variable_get('menu_block_menu_order', array(
      'primary-links' => '',
      'secondary-links' => '',
    ));

    // Check for regular expressions as menu keys.
    $patterns = array();
    foreach (array_keys($menu_order) as $pattern) {
      if ($pattern[0] == '/') {
        $patterns[$pattern] = NULL;
      }
    }

    // Retrieve all the menus containing a link to the current page.
    $result = db_query("SELECT menu_name FROM {menu_links} WHERE link_path = '%s'", $_GET['q'] ? $_GET['q'] : '<front>');
    while ($item = db_fetch_array($result)) {

      // Check if the menu is in the list of available menus.
      if (isset($menu_order[$item['menu_name']])) {

        // Mark the menu.
        $menu_order[$item['menu_name']] = MENU_TREE__CURRENT_PAGE_MENU;
      }
      else {

        // Check if the menu matches one of the available patterns.
        foreach (array_keys($patterns) as $pattern) {
          if (preg_match($pattern, $item['menu_name'])) {

            // Mark the menu.
            $menu_order[$pattern] = MENU_TREE__CURRENT_PAGE_MENU;

            // Store the actual menu name.
            $patterns[$pattern] = $item['menu_name'];
          }
        }
      }
    }

    // Find the first marked menu.
    $config['menu_name'] = array_search(MENU_TREE__CURRENT_PAGE_MENU, $menu_order);

    // If a pattern was matched, use the actual menu name instead of the pattern.
    if (!empty($patterns[$config['menu_name']])) {
      $config['menu_name'] = $patterns[$config['menu_name']];
    }
    $config['parent_mlid'] = 0;

    // If no menu link was found, don't display the block.
    if (empty($config['menu_name'])) {
      return array();
    }
  }

  // Get the default block name.
  $menu_names = menu_block_get_all_menus();
  menu_block_set_title(t($menu_names[$config['menu_name']]));
  if ($config['expanded'] || $config['parent_mlid']) {

    // Get the full, un-pruned tree.
    $tree = menu_tree_all_data($config['menu_name']);

    // And add the active trail data back to the full tree.
    menu_tree_add_active_path($tree);
  }
  else {

    // Get the tree pruned for just the active trail.
    $tree = menu_tree_page_data($config['menu_name']);
  }

  // Allow other modules to alter the tree before we begin operations on it.
  $alter_data =& $tree;

  // Also allow modules to alter the config.
  $alter_data['__drupal_alter_by_ref'] = array(
    &$config,
  );
  drupal_alter('menu_block_tree', $alter_data);

  // Localize the tree.
  if (module_exists('i18nmenu')) {
    i18nmenu_localize_tree($tree);
  }

  // Prune the tree along the active trail to the specified level.
  if ($config['level'] > 1 || $config['parent_mlid']) {
    if ($config['parent_mlid']) {
      $parent_item = menu_link_load($config['parent_mlid']);
      menu_tree_prune_tree($tree, $config['level'], $parent_item);
    }
    else {
      menu_tree_prune_tree($tree, $config['level']);
    }
  }

  // Prune the tree to the active menu item.
  if ($config['follow']) {
    menu_tree_prune_active_tree($tree, $config['follow']);
  }

  // If the menu-item-based tree is not "expanded", trim the tree to the active path.
  if ($config['parent_mlid'] && !$config['expanded']) {
    menu_tree_trim_active_path($tree);
  }

  // Trim the branches that extend beyond the specified depth.
  if ($config['depth'] > 0) {
    menu_tree_depth_trim($tree, $config['depth']);
  }

  // Sort the active path to the top of the tree.
  if ($config['sort']) {
    menu_tree_sort_active_path($tree);
  }

  // Render the tree.
  $data = array();
  $data['subject'] = menu_block_get_title($config['title_link'], $config);
  $data['content'] = menu_block_tree_output($tree, $config);
  if ($data['content']) {
    $hooks = array();
    $hooks[] = 'menu_block_wrapper__' . str_replace('-', '_', $config['delta']);
    $hooks[] = 'menu_block_wrapper__' . str_replace('-', '_', $config['menu_name']);
    $hooks[] = 'menu_block_wrapper';
    $data['content'] = theme($hooks, $data['content'], $config, $config['delta']);
  }
  return $data;
}