You are here

function superfish_block_view in Superfish 7

Implements hook_block_view().

File

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

Code

function superfish_block_view($delta = 0) {
  $block_css = $block_js = $sfsettings = $sfoptions = $sfplugins = array();
  $sfsettings['depth'] = variable_get('superfish_depth_' . $delta, '-1');
  $sfsettings['type'] = variable_get('superfish_type_' . $delta, 'horizontal');
  $sfsettings['style'] = variable_get('superfish_style_' . $delta, 'none');
  $sfsettings['expanded'] = variable_get('superfish_expanded_' . $delta, 0);
  $sfsettings['firstlast'] = variable_get('superfish_firstlast_' . $delta, 1);
  $sfsettings['zebra'] = variable_get('superfish_zebra_' . $delta, 1);
  $sfsettings['dfirstlast'] = variable_get('superfish_dfirstlast_' . $delta, 0);
  $sfsettings['dzebra'] = variable_get('superfish_dzebra_' . $delta, 0);
  $sfsettings['itemcount'] = variable_get('superfish_itemcount_' . $delta, 1);
  $sfsettings['itemcounter'] = variable_get('superfish_itemcounter_' . $delta, 1);
  $sfsettings['itemdepth'] = variable_get('superfish_itemdepth' . $delta, 1);
  $sfsettings['ulclass'] = superfish_sanitize(variable_get('superfish_ulclass_' . $delta, ''), 'strict');
  $sfsettings['liclass'] = superfish_sanitize(variable_get('superfish_liclass_' . $delta, ''), 'strict');
  $sfsettings['hlclass'] = superfish_sanitize(variable_get('superfish_hlclass_' . $delta, ''), 'strict');
  $sfsettings['wrapmul'] = superfish_sanitize(variable_get('superfish_wrapmul_' . $delta, ''));
  $sfsettings['wrapul'] = superfish_sanitize(variable_get('superfish_wrapul_' . $delta, ''));
  $sfsettings['wraphl'] = superfish_sanitize(variable_get('superfish_wraphl_' . $delta, ''));
  $sfsettings['wraphlt'] = superfish_sanitize(variable_get('superfish_wraphlt_' . $delta, ''));
  $sfsettings['use_link_theme'] = variable_get('superfish_use_link_theme_' . $delta, 1);
  $sfsettings['use_item_theme'] = variable_get('superfish_use_item_theme_' . $delta, 1);
  $sfsettings['clone_parent'] = variable_get('superfish_clone_parent_' . $delta, 0);
  $sfsettings['hidelinkdescription'] = variable_get('superfish_hhldescription_' . $delta, 0);
  $sfsettings['linkdescription'] = variable_get('superfish_hldescription_' . $delta, 0);
  $sfsettings['hldmenus'] = variable_get('superfish_hldmenus_' . $delta, '');
  $sfsettings['hldmenus'] = strpos($sfsettings['hldmenus'], ',') ? superfish_array_filter(explode(',', $sfsettings['hldmenus'])) : $sfsettings['hldmenus'];
  $sfsettings['hldexclude'] = variable_get('superfish_hldexclude_' . $delta, '');
  $sfsettings['hldexclude'] = strpos($sfsettings['hldexclude'], ',') ? superfish_array_filter(explode(',', $sfsettings['hldexclude'])) : $sfsettings['hldexclude'];
  $sfsettings['megamenu'] = variable_get('superfish_multicolumn_' . $delta, 0);
  $sfsettings['megamenu_depth'] = variable_get('superfish_mcdepth_' . $delta, 1);
  $sfsettings['megamenu_depth'] = $sfsettings['type'] == 'navbar' && $sfsettings['megamenu_depth'] == 1 ? 2 : $sfsettings['megamenu_depth'];
  $sfsettings['megamenu_levels'] = variable_get('superfish_mclevels_' . $delta, 1) + $sfsettings['megamenu_depth'];
  $sfsettings['megamenu_exclude'] = variable_get('superfish_mcexclude_' . $delta, '');
  $sfsettings['megamenu_exclude'] = strpos($sfsettings['megamenu_exclude'], ',') ? superfish_array_filter(explode(',', $sfsettings['megamenu_exclude'])) : $sfsettings['megamenu_exclude'];
  $sfoptions['pathClass'] = $sfsettings['type'] == 'navbar' ? variable_get('superfish_pathclass_' . $delta, 'active-trail') : '';
  $sfoptions['pathLevels'] = variable_get('superfish_pathlevels_' . $delta, 1) != 1 ? variable_get('superfish_pathlevels_' . $delta, 1) : '';
  $sfoptions['delay'] = variable_get('superfish_delay_' . $delta, 800) != 800 ? variable_get('superfish_delay_' . $delta, 800) : '';
  $sfoptions['animation']['opacity'] = 'show';
  $slide = variable_get('superfish_slide_' . $delta, 'vertical');
  if (strpos($slide, '_') && superfish_library('javascript', 'jquery.easing.js', 'check')) {
    $slide = explode('_', $slide);
    switch ($slide[1]) {
      case 'vertical':
        $sfoptions['animation']['height'] = array(
          'show',
          $slide[0],
        );
        break;
      case 'horizontal':
        $sfoptions['animation']['width'] = array(
          'show',
          $slide[0],
        );
        break;
      case 'diagonal':
        $sfoptions['animation']['height'] = array(
          'show',
          $slide[0],
        );
        $sfoptions['animation']['width'] = array(
          'show',
          $slide[0],
        );
        break;
    }
    $block_js = array_merge($block_js, superfish_library('javascript', 'easing', 'add'));
  }
  else {
    switch ($slide) {
      case 'vertical':
        $sfoptions['animation']['height'] = 'show';
        break;
      case 'horizontal':
        $sfoptions['animation']['width'] = 'show';
        break;
      case 'diagonal':
        $sfoptions['animation']['height'] = 'show';
        $sfoptions['animation']['width'] = 'show';
        break;
    }
  }
  $speed = variable_get('superfish_speed_' . $delta, 'fast');
  if ($speed != 'normal') {
    $sfoptions['speed'] = is_numeric($speed) ? (int) $speed : ($speed == ('slow' || 'normal' || 'fast') ? $speed : '');
  }
  $sfoptions['autoArrows'] = variable_get('superfish_arrow_' . $delta, 1) == 0 ? FALSE : '';
  $sfoptions['dropShadows'] = variable_get('superfish_shadow_' . $delta, 1) == 0 ? FALSE : '';
  if (variable_get('superfish_hid_' . $delta, 1) == 1 && superfish_library('javascript', 'jquery.hoverIntent.minified.js', 'check')) {
    $block_js = array_merge($block_js, superfish_library('javascript', 'hoverIntent', 'add'));
  }
  else {
    $sfoptions['disableHI'] = TRUE;
  }
  $sfoptions = superfish_array_filter($sfoptions);
  if (variable_get('superfish_amw_' . $delta, 0) == 1 && superfish_library('javascript', 'sfautomaticwidth.js', 'check')) {
    $sfplugins['automaticwidth'] = TRUE;
    $block_js = array_merge($block_js, superfish_library('javascript', 'sfautomaticwidth', 'add'));
  }
  $touchscreen = variable_get('superfish_touch_' . $delta, 0);
  if ($touchscreen > 0 && superfish_library('javascript', 'sftouchscreen.js', 'check')) {
    $block_js = array_merge($block_js, superfish_library('javascript', 'sftouchscreen', 'add'));
    $behaviour = variable_get('superfish_touchbh_' . $delta, 2);
    $sfplugins['touchscreen']['behaviour'] = $behaviour != 2 ? $behaviour : '';
    if (variable_get('superfish_touchdh_' . $delta, 0)) {
      $sfplugins['touchscreen']['disableHover'] = TRUE;
    }
    switch ($touchscreen) {
      case 1:
        $sfplugins['touchscreen']['mode'] = 'always_active';
        break;
      case 2:
        $sfplugins['touchscreen']['mode'] = 'window_width';
        $tsbp = variable_get('superfish_touchbp_' . $delta, 768);
        $sfplugins['touchscreen']['breakpoint'] = $tsbp != 768 ? (double) $tsbp : '';
        $tsbpu = variable_get('superfish_touchbpu_' . $delta, 'px');
        $sfplugins['touchscreen']['breakpointUnit'] = $tsbpu;
        break;
      case 3:

        // Which method to use for UA detection.
        $tsuam = variable_get('superfish_touchuam_' . $delta, 0);
        $tsua = variable_get('superfish_touchua_' . $delta, 0);
        switch ($tsuam) {

          // Client-side.
          case 0:
            switch ($tsua) {
              case 0:
                $sfplugins['touchscreen']['mode'] = 'useragent_predefined';
                break;
              case 1:
                $sfplugins['touchscreen']['mode'] = 'useragent_custom';
                $tsual = drupal_strtolower(variable_get('superfish_touchual_' . $delta, ''));
                if (strpos($tsual, '*')) {
                  $tsual = str_replace('*', '|', $tsual);
                }
                $sfplugins['touchscreen']['useragent'] = $tsual;
                break;
            }
            break;

          // Server-side.
          case 1:
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
              $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
              switch ($tsua) {

                // Use the pre-defined list of mobile UA strings.
                case 0:
                  if (preg_match('/(android|bb\\d+|meego)|avantgo|bada\\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
                    $sfplugins['touchscreen']['mode'] = 'always_active';
                    if ($behaviour == 2) {
                      $sfsettings['clone_parent'] = 1;
                    }
                  }
                  break;

                // Use the custom list of UA strings.
                case 1:
                  $tsual = drupal_strtolower(variable_get('superfish_touchual_' . $delta, ''));
                  $tsuac = array();
                  if (strpos($tsual, '*')) {
                    $tsual = explode('*', $tsual);
                    foreach ($tsual as $ua) {
                      $tsuac[] = strpos($hua, $ua) ? 1 : 0;
                    }
                  }
                  else {
                    $tsuac[] = strpos($hua, $tsual) ? 1 : 0;
                  }
                  if (in_array(1, $tsuac)) {
                    $sfplugins['touchscreen']['mode'] = 'always_active';
                    if ($behaviour == 2) {
                      $sfsettings['clone_parent'] = 1;
                    }
                  }
                  break;
              }
            }
            break;
        }
        break;
    }
  }

  // Unset hoverIntent if sfTouchscreen settings make it ineffective.
  if (isset($sfplugins['touchscreen']['disableHover']) && $sfplugins['touchscreen']['disableHover'] === TRUE && $sfplugins['touchscreen']['mode'] == 'always_active') {
    $hid = array_keys(superfish_library('javascript', 'hoverIntent', 'add'));
    if (isset($block_js[$hid[0]])) {
      unset($block_js[$hid[0]]);
    }
  }
  $smallscreen = variable_get('superfish_small_' . $delta, 2);
  if ($smallscreen > 0 && superfish_library('javascript', 'sfsmallscreen.js', 'check') == 1) {
    $block_js = array_merge($block_js, superfish_library('javascript', 'sfsmallscreen', 'add'));
    switch ($smallscreen) {
      case 1:
        $sfplugins['smallscreen']['mode'] = 'always_active';
        break;
      case 2:
        $sfplugins['smallscreen']['mode'] = 'window_width';
        $ssbp = variable_get('superfish_smallbp_' . $delta, 768);
        $sfplugins['smallscreen']['breakpoint'] = $ssbp != 768 ? (double) $ssbp : '';
        $ssbpu = variable_get('superfish_smallbpu_' . $delta, 'px');
        $sfplugins['smallscreen']['breakpointUnit'] = $ssbpu;
        break;
      case 3:

        // Which method to use for UA detection.
        $ssuam = variable_get('superfish_smalluam_' . $delta, 0);
        $ssua = variable_get('superfish_smallua_' . $delta, 0);
        switch ($ssuam) {

          // Client-side.
          case 0:
            switch ($ssua) {
              case 0:
                $sfplugins['smallscreen']['mode'] = 'useragent_predefined';
                break;
              case 1:
                $sfplugins['smallscreen']['mode'] = 'useragent_custom';
                $ssual = drupal_strtolower(variable_get('superfish_smallual_' . $delta, ''));
                if (strpos($ssual, '*')) {
                  $ssual = str_replace('*', '|', $ssual);
                }
                $sfplugins['smallscreen']['useragent'] = $ssual;
                break;
            }
            break;

          // Server-side.
          case 1:
            if (isset($_SERVER['HTTP_USER_AGENT'])) {
              $hua = drupal_strtolower($_SERVER['HTTP_USER_AGENT']);
              switch ($ssua) {

                // Use the pre-defined list of mobile UA strings.
                case 0:
                  if (preg_match('/(android|bb\\d+|meego).+mobile|avantgo|bada\\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge|maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $hua)) {
                    $sfplugins['smallscreen']['mode'] = 'always_active';
                  }
                  break;

                // Use the custom list of UA strings.
                case 1:
                  $ssual = drupal_strtolower(variable_get('superfish_smallual_' . $delta, ''));
                  $ssuac = array();
                  if (strpos($ssual, '*')) {
                    $ssual = explode('*', $ssual);
                    foreach ($ssual as $ua) {
                      $ssuac[] = strpos($hua, $ua) ? 1 : 0;
                    }
                  }
                  else {
                    $ssuac[] = strpos($hua, $ssual) ? 1 : 0;
                  }
                  if (in_array(1, $ssuac)) {
                    $sfplugins['smallscreen']['mode'] = 'always_active';
                  }
                  break;
              }
            }
            break;
        }
        break;
    }
    $type = variable_get('superfish_smallact_' . $delta, 1);
    switch ($type) {
      case 0:
        $asa = variable_get('superfish_smallasa_' . $delta, 0);
        $cmc = variable_get('superfish_smallcmc_' . $delta, 0);
        $chc = variable_get('superfish_smallchc_' . $delta, 0);
        $ecm = variable_get('superfish_smallecm_' . $delta, '');
        $ech = variable_get('superfish_smallech_' . $delta, '');
        $icm = variable_get('superfish_smallicm_' . $delta, '');
        $ich = variable_get('superfish_smallich_' . $delta, '');
        $sfplugins['smallscreen']['type'] = 'select';
        $sfplugins['smallscreen']['addSelected'] = $asa == 1 ? TRUE : '';
        $sfplugins['smallscreen']['menuClasses'] = $cmc == 1 ? TRUE : '';
        $sfplugins['smallscreen']['hyperlinkClasses'] = $chc == 1 ? TRUE : '';
        $sfplugins['smallscreen']['excludeClass_menu'] = $cmc == 1 && !empty($ecm) ? $ecm : '';
        $sfplugins['smallscreen']['excludeClass_hyperlink'] = $chc == 1 && !empty($ech) ? $ech : '';
        $sfplugins['smallscreen']['includeClass_menu'] = !empty($icm) ? $icm : '';
        $sfplugins['smallscreen']['includeClass_hyperlink'] = !empty($ich) ? $ich : '';
        break;
      case 1:
        if (superfish_library('CSS', 'smallscreen', 'check')) {
          $block_css = array_merge($block_css, superfish_library('CSS', 'smallscreen', 'add'));
        }
        $ab = variable_get('superfish_smallabt_' . $delta, 1);
        $sfplugins['smallscreen']['accordionButton'] = $ab != 1 ? $ab : '';
        $sfplugins['smallscreen']['expandText'] = t('Expand') != 'Expand' ? t('Expand') : '';
        $sfplugins['smallscreen']['collapseText'] = t('Collapse') != 'Collapse' ? t('Collapse') : '';
        break;
    }
  }
  if (variable_get('superfish_spp_' . $delta, 1) == 1 && superfish_library('javascript', 'supposition.js', 'check')) {
    $sfplugins['supposition'] = TRUE;
    $block_js = array_merge($block_js, superfish_library('javascript', 'supposition', 'add'));
  }
  if (variable_get('superfish_bgf_' . $delta, 0) == 1 && superfish_library('javascript', 'jquery.bgiframe.min.js', 'check')) {
    $sfplugins['bgiframe'] = TRUE;
    $block_js = array_merge($block_js, superfish_library('javascript', 'bgiframe', 'add'));
  }
  if (variable_get('superfish_supersubs_' . $delta, 1) == 1 && superfish_library('javascript', 'supersubs.js', 'check')) {
    $block_js = array_merge($block_js, superfish_library('javascript', 'supersubs', 'add'));
    $emm = variable_get('superfish_supersubs_emm_' . $delta, 0);
    $minwidth = variable_get('superfish_minwidth_' . $delta, 12);
    $maxwidth = variable_get('superfish_maxwidth_' . $delta, 27);
    $sfplugins['supersubs']['megamenu'] = $emm ? FALSE : '';
    $sfplugins['supersubs']['minWidth'] = $minwidth != 12 ? $minwidth : '';
    $sfplugins['supersubs']['maxWidth'] = $maxwidth != 27 ? $maxwidth : '';
    if (empty($sfplugins['supersubs']['minWidth']) && empty($sfplugins['supersubs']['maxWidth']) && $sfplugins['supersubs']['megamenu'] !== FALSE) {
      $sfplugins['supersubs'] = TRUE;
    }
  }
  $menu_name = variable_get('superfish_menu_' . $delta, 'main-menu:0');

  // Integration with the Domain Access module.
  $main = variable_get('menu_main_links_source');
  $secondary = variable_get('menu_secondary_links_source');
  if ($main || $secondary) {
    if (strpos($menu_name, 'domainaccessmenu1st') !== FALSE) {
      $menu_name = $main . ':0';
    }
    elseif (strpos($menu_name, 'domainaccessmenu2nd') !== FALSE) {
      $menu_name = $secondary . ':0';
    }
  }
  $menu_name = str_replace(array(
    'domainaccessmenu1st-',
    'domainaccessmenu2nd-',
  ), '', $menu_name);
  list($menu_name, $mlid) = explode(':', $menu_name);
  $vars = array(
    'id' => $delta,
    'menu_name' => $menu_name,
    'mlid' => $mlid,
    'sfsettings' => $sfsettings,
  );
  if ($block = theme('superfish', $vars)) {
    if (!empty($block['content'])) {
      $block['content'] = array(
        '#type' => 'markup',
        '#markup' => $block['content'],
        '#contextual_links' => array(
          'superfish' => array(
            'admin/structure/menu/manage',
            array(
              $menu_name,
            ),
          ),
        ),
      );

      // Creating a title for the small-screen menu.
      if ($smallscreen > 0 && superfish_library('javascript', 'sfsmallscreen.js', 'check')) {
        switch ($type) {
          case 0:
            $title = variable_get('superfish_smallset_' . $delta, '');
            break;
          case 1:
            $title = variable_get('superfish_smallamt_' . $delta, '');
            break;
        }
        $sfplugins['smallscreen']['title'] = empty($title) ? $block['subject'] : $title;
      }
      $sfplugins = superfish_array_filter($sfplugins);

      // Prepare the JavaScript settings.
      $index = 'menu-' . $delta;
      $javascript['superfish'][$index]['id'] = $delta;
      $javascript['superfish'][$index]['sf'] = isset($sfoptions) ? $sfoptions : array();
      if (!empty($sfplugins)) {
        $javascript['superfish'][$index]['plugins'] = $sfplugins;
      }

      // Adding required Javascript.
      if (superfish_library('javascript', 'superfish.js', 'check')) {
        $block_js = array_merge($block_js, superfish_library('javascript', 'superfish.js', 'add'));
        if (superfish_library('javascript', 'drupal_behavior', 'check')) {
          $block_js = array_merge($block_js, array(
            array(
              'data' => $javascript,
              'type' => 'setting',
            ),
          ));
          $block_js = array_merge($block_js, superfish_library('javascript', 'drupal_behavior', 'add'));
        }
      }

      // Adding required CSS files.
      if (superfish_library('CSS', 'superfish', 'check')) {
        $block_css = array_merge($block_css, superfish_library('CSS', 'superfish', 'add'));
      }
      if (superfish_library('CSS', $sfsettings['type'], 'check')) {
        $block_css = array_merge($block_css, superfish_library('CSS', $sfsettings['type'], 'add'));
      }
      if ($sfsettings['style'] != 'none' && superfish_styles('path', $sfsettings['style']) != '') {
        $block_css = array_merge($block_css, superfish_styles('path', $sfsettings['style']));
      }
      $extracss = variable_get('superfish_pathcss_' . $delta, '');
      if ($extracss) {
        if (strpos($extracss, ',')) {
          $extracss = explode(',', $extracss);
          foreach ($extracss as $c) {
            $block_css = array_merge($block_css, array(
              $c => array(
                'type' => 'file',
                'weight' => 510,
              ),
            ));
          }
        }
        else {
          $block_css = array_merge($block_css, array(
            $extracss => array(
              'type' => 'file',
              'weight' => 510,
            ),
          ));
        }
      }
      $block['content']['#attached'] = array(
        'css' => $block_css,
        'js' => $block_js,
      );
    }
  }
  return $block;
}