You are here

function theme_superfish_build in Superfish 6

Same name and namespace in other branches
  1. 7 superfish.module \theme_superfish_build()

Helper function that builds the nested lists of a Superfish menu.

1 theme call to theme_superfish_build()
theme_superfish in ./superfish.module
Theme function to allow any menu tree to be themed as a Superfish menu.

File

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

Code

function theme_superfish_build($id, $menu, $depth = -1, $trail = NULL, $clone_parent, $sfsettings = NULL) {
  $output = array(
    'content' => '',
  );

  // Keep original $sfsettings untouched as we need to pass it to the child items.
  $settings = $sfsettings;
  $megamenu = $megamenu_below = $settings['megamenu'];
  $total_children = $parent_children = $single_children = 0;
  $i = 1;

  // Reckon the total number of available menu items.
  foreach ($menu as $menu_item) {
    if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
      $total_children++;
    }
  }

  // sfTouchscreen.
  // Adding cloned parent to the sub-menu.
  if ($clone_parent !== FALSE) {
    array_unshift($menu, $clone_parent);
  }
  foreach ($menu as $menu_item) {
    $show_children = $megamenu_wrapper = $megamenu_column = $megamenu_content = FALSE;
    $item_class = $link_class = array();
    $mlid = $menu_item['link']['mlid'];
    if (!isset($menu_item['link']['hidden']) || $menu_item['link']['hidden'] == 0) {
      $item_class[] = $trail && in_array($mlid, $trail) ? 'active-trail' : '';

      // Add helper classes to the menu items and hyperlinks.
      $settings['firstlast'] = $settings['dfirstlast'] == 1 && $total_children == 1 ? 0 : $settings['firstlast'];
      $item_class[] = $settings['firstlast'] == 1 ? $i == 1 && $i == $total_children ? 'firstandlast' : ($i == 1 ? 'first' : ($i == $total_children ? 'last' : 'middle')) : '';
      $settings['zebra'] = $settings['dzebra'] == 1 && $total_children == 1 ? 0 : $settings['zebra'];
      $item_class[] = $settings['zebra'] == 1 ? $i % 2 ? 'odd' : 'even' : '';
      $item_class[] = $settings['itemcount'] == 1 ? 'sf-item-' . $i : '';
      $item_class[] = $settings['itemdepth'] == 1 ? 'sf-depth-' . $menu_item['link']['depth'] : '';
      $link_class[] = $settings['itemdepth'] == 1 ? 'sf-depth-' . $menu_item['link']['depth'] : '';
      $item_class[] = $settings['liclass'] ? $settings['liclass'] : '';
      $link_class[] = $settings['hlclass'] ? $settings['hlclass'] : '';
      $item_class[] = $clone_parent !== FALSE ? 'sf-clone-parent' : '';
      $i++;

      // Hide hyperlink descriptions ("title" attribute).
      if ($settings['hidelinkdescription'] == 1) {
        unset($menu_item['link']['localized_options']['attributes']['title']);
      }

      // Insert hyperlink description ("title" attribute) into the text.
      $show_linkdescription = $settings['linkdescription'] == 1 && !empty($menu_item['link']['localized_options']['attributes']['title']) ? TRUE : FALSE;
      if ($show_linkdescription) {
        if (!empty($settings['hldmenus'])) {
          $show_linkdescription = is_array($settings['hldmenus']) ? in_array($mlid, $settings['hldmenus']) ? TRUE : FALSE : ($mlid == $settings['hldmenus'] ? TRUE : FALSE);
        }
        if (!empty($settings['hldexclude'])) {
          $show_linkdescription = is_array($settings['hldexclude']) ? in_array($mlid, $settings['hldexclude']) ? FALSE : $show_linkdescription : ($settings['hldexclude'] == $mlid ? FALSE : $show_linkdescription);
        }
        if ($show_linkdescription) {
          $menu_item['link']['title'] .= ' <span class="sf-description">';
          $menu_item['link']['title'] .= $menu_item['link']['localized_options']['attributes']['title'];
          $menu_item['link']['title'] .= '</span>';
          $menu_item['link']['localized_options']['html'] = TRUE;
        }
      }

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

        // In case you just wanted to add something after the element.
        if (drupal_substr($sfsettings['wrapul'], 0) == ',') {
          array_unshift($wul, '');
        }
      }
      else {
        $wul = array();
      }

      // Add custom HTML codes around the hyperlinks.
      if ($settings['wraphl'] && strpos($settings['wraphl'], ',') !== FALSE) {
        $whl = explode(',', $settings['wraphl']);

        // The same as above
        if (drupal_substr($settings['wraphl'], 0) == ',') {
          array_unshift($whl, '');
        }
      }
      else {
        $whl = array();
      }

      // Add custom HTML codes around the hyperlinks text.
      if ($settings['wraphlt'] && strpos($settings['wraphlt'], ',') !== FALSE) {
        $whlt = explode(',', $settings['wraphlt']);

        // The same as above
        if (drupal_substr($settings['wraphlt'], 0) == ',') {
          array_unshift($whlt, '');
        }
        $menu_item['link']['title'] = $whlt[0] . $menu_item['link']['title'] . $whlt[1];
        $menu_item['link']['localized_options']['html'] = TRUE;
      }
      $expanded = $sfsettings['expanded'] == 1 ? $menu_item['link']['expanded'] == 1 ? TRUE : FALSE : TRUE;
      if (!empty($menu_item['link']['has_children']) && !empty($menu_item['below']) && $depth != 0 && $expanded === TRUE) {

        // Megamenu is still beta, there is a good chance much of this will be changed.
        if (!empty($settings['megamenu_exclude'])) {
          if (is_array($settings['megamenu_exclude'])) {
            $megamenu_below = in_array($mlid, $settings['megamenu_exclude']) ? 0 : $megamenu;
          }
          else {
            $megamenu_below = $settings['megamenu_exclude'] == $mlid ? 0 : $megamenu;
          }

          // Send the result to the sub-menu.
          $sfsettings['megamenu'] = $megamenu_below;
        }
        if ($megamenu_below == 1) {
          $megamenu_wrapper = $menu_item['link']['depth'] == $settings['megamenu_depth'] ? TRUE : FALSE;
          $megamenu_column = $menu_item['link']['depth'] == $settings['megamenu_depth'] + 1 ? TRUE : FALSE;
          $megamenu_content = $menu_item['link']['depth'] >= $settings['megamenu_depth'] && $menu_item['link']['depth'] <= $settings['megamenu_levels'] ? TRUE : FALSE;
        }

        // sfTouchscreen.
        // Preparing the cloned parent links to be added to the sub-menus.
        $below_visible = array();
        foreach ($menu_item['below'] as $below) {
          if (!isset($below['link']['hidden']) || $below['link']['hidden'] == 0) {
            $below_visible[] = 1;
          }
        }
        if ($settings['clone_parent'] == 1 && in_array(1, $below_visible)) {
          $clone_parent = $menu_item;
          unset($clone_parent['below']);
          unset($clone_parent['has_children']);
        }
        else {
          $clone_parent = FALSE;
        }

        // Render the sub-menu.
        $children = theme('superfish_build', $id, $menu_item['below'], $depth, $trail, $clone_parent, $sfsettings);

        // Check to see whether it should be displayed.
        $show_children = ($menu_item['link']['depth'] <= $depth || $depth == -1) && $children['content'] ? TRUE : FALSE;
        if ($show_children) {

          // Add item counter classes.
          if ($settings['itemcounter']) {
            $item_class[] = 'sf-total-children-' . $children['total_children'];
            $item_class[] = 'sf-parent-children-' . $children['parent_children'];
            $item_class[] = 'sf-single-children-' . $children['single_children'];
          }

          // More helper classes.
          $item_class[] = $megamenu_column ? 'sf-megamenu-column' : '';
          $item_class[] = $link_class[] = 'menuparent';
        }
        $parent_children++;
      }
      else {
        $item_class[] = 'sf-no-children';
        $single_children++;
      }
      $item_class = implode(' ', superfish_array_filter($item_class));
      if (isset($menu_item['link']['localized_options']['attributes']['class'])) {
        $link_class_current = explode(' ', $menu_item['link']['localized_options']['attributes']['class']);
        $link_class = array_merge($link_class_current, superfish_array_filter($link_class));
      }
      $menu_item['link']['localized_options']['attributes']['class'] = implode(' ', superfish_array_filter($link_class));

      // Render the menu item.
      // Should a theme be used for menu items?
      if ($settings['use_item_theme']) {
        $item_variables = array(
          'element' => array(
            'attributes' => array(
              'id' => 'menu-' . $mlid . '-' . $id,
              'class' => trim($item_class),
            ),
            'below' => $show_children ? $children['content'] : NULL,
            'item' => $menu_item,
          ),
          'properties' => array(
            'megamenu' => array(
              'megamenu_column' => $megamenu_column,
              'megamenu_wrapper' => $megamenu_wrapper,
              'megamenu_content' => $megamenu_content,
            ),
            'wrapper' => array(
              'wul' => $wul,
              'whl' => $whl,
            ),
          ),
        );
        $output['content'] .= theme('superfish_menu_item', $item_variables);
      }
      else {
        $output['content'] .= '<li id="menu-' . $mlid . '-' . $id . '"';
        $output['content'] .= $item_class ? ' class="' . trim($item_class) . '">' : '>';
        $output['content'] .= $megamenu_column ? '<div class="sf-megamenu-column">' : '';
        $output['content'] .= isset($whl[0]) ? $whl[0] : '';
        $output['content'] .= theme('menu_item_link', $menu_item['link']);
        $output['content'] .= isset($whl[1]) ? $whl[1] : '';
        $output['content'] .= $megamenu_wrapper ? '<ul class="sf-megamenu"><li class="sf-megamenu-wrapper ' . $item_class . '">' : '';
        $output['content'] .= $show_children ? isset($wul[0]) ? $wul[0] : '' : '';
        $output['content'] .= $show_children ? $megamenu_content ? '<ol>' : '<ul>' : '';
        $output['content'] .= $show_children ? $children['content'] : '';
        $output['content'] .= $show_children ? $megamenu_content ? '</ol>' : '</ul>' : '';
        $output['content'] .= $show_children ? isset($wul[1]) ? $wul[1] : '' : '';
        $output['content'] .= $megamenu_wrapper ? '</li></ul>' : '';
        $output['content'] .= $megamenu_column ? '</div>' : '';
        $output['content'] .= '</li>';
      }
    }
  }
  $output['total_children'] = $total_children;
  $output['parent_children'] = $parent_children;
  $output['single_children'] = $single_children;
  return $output;
}