You are here

function theme_superfish in Superfish 7

Same name and namespace in other branches
  1. 6 superfish.module \theme_superfish()

Theme function to allow any menu tree to be themed as a Superfish menu.

1 theme call to theme_superfish()
superfish_block_view in ./superfish.module
Implements hook_block_view().

File

./superfish.module, line 2074
Enables the use of jQuery Superfish plugin for Drupal menus.

Code

function theme_superfish($variables) {
  global $language;
  $id = $variables['id'];
  $menu_name = $variables['menu_name'];
  $mlid = $variables['mlid'];
  $sfsettings = $variables['sfsettings'];
  $menu = menu_tree_all_data($menu_name);
  drupal_alter('superfish_menu_data', $menu);
  if (function_exists('i18n_menu_localize_tree')) {
    $menu = i18n_menu_localize_tree($menu);
  }

  // For custom $menus and menus built all the way from the top-level we
  // don't need to "create" the specific sub-menu and we need to get the title
  // from the $menu_name since there is no "parent item" array.
  // Create the specific menu if we have a mlid.
  if (!empty($mlid)) {

    // Load the parent menu item.
    $item = menu_link_load($mlid);
    $title = check_plain($item['title']);
    $parent_depth = $item['depth'];

    // Narrow down the full menu to the specific sub-tree we need.
    for ($p = 1; $p < 10; $p++) {
      if ($sub_mlid = $item["p{$p}"]) {
        $subitem = menu_link_load($sub_mlid);
        $key = 50000 + $subitem['weight'] . ' ' . $subitem['title'] . ' ' . $subitem['mlid'];
        $menu = isset($menu[$key]['below']) ? $menu[$key]['below'] : $menu;
      }
    }
  }
  else {
    $result = '';
    if (module_exists('i18n_string')) {
      $string = module_invoke('i18n_string', 'build', "menu:menu:{$menu_name}:title");
      if ($string) {
        $result = $string
          ->get_translation($GLOBALS['language']->language);
      }
    }
    if (!$result) {
      $result = db_query("SELECT `title` FROM {menu_custom} WHERE `menu_name` = :a", array(
        ':a' => $menu_name,
      ))
        ->fetchField();
    }
    $title = check_plain($result);
  }
  $output['content'] = '';
  $output['subject'] = $title;
  if ($menu) {

    // Set the total menu depth counting from this parent if we need it.
    $depth = $sfsettings['depth'];
    $depth = $sfsettings['depth'] > 0 && isset($parent_depth) ? $parent_depth + $depth : $depth;
    $var = array(
      'id' => $id,
      'menu' => $menu,
      'depth' => $depth,
      'trail' => superfish_build_page_trail(menu_tree_page_data($menu_name)),
      'clone_parent' => FALSE,
      'sfsettings' => $sfsettings,
    );
    if ($menu_tree = theme('superfish_build', $var)) {
      if ($menu_tree['content']) {

        // Add custom HTML codes around the main menu.
        if ($sfsettings['wrapmul'] && strpos($sfsettings['wrapmul'], ',') !== FALSE) {
          $wmul = explode(',', $sfsettings['wrapmul']);

          // In case you just wanted to add something after the element.
          if (drupal_substr($sfsettings['wrapmul'], 0) == ',') {
            array_unshift($wmul, '');
          }
        }
        else {
          $wmul = array();
        }
        $classes = 'menu sf-menu sf-' . $menu_name . ' sf-' . $sfsettings['type'] . ' sf-style-' . $sfsettings['style'];
        $classes .= $sfsettings['itemcounter'] ? ' sf-total-items-' . $menu_tree['total_children'] : '';
        $classes .= $sfsettings['itemcounter'] ? ' sf-parent-items-' . $menu_tree['parent_children'] : '';
        $classes .= $sfsettings['itemcounter'] ? ' sf-single-items-' . $menu_tree['single_children'] : '';
        $classes .= $sfsettings['ulclass'] ? ' ' . trim($sfsettings['ulclass']) : '';
        $classes .= $language->direction == 1 ? ' rtl' : '';
        $output['content'] = isset($wmul[0]) ? $wmul[0] : '';
        $output['content'] .= '<ul ' . drupal_attributes(array(
          'id' => 'superfish-' . $id,
          'class' => $classes,
        )) . '>';
        $output['content'] .= $menu_tree['content'];
        $output['content'] .= '</ul>';
        $output['content'] .= isset($wmul[1]) ? $wmul[1] : '';
      }
    }
  }
  return $output;
}