You are here

让Drupal7同时支持IE7,8,9,新式浏览器、手机端

用户改版网站,将原来的主题改为响应式主题,同时专门为手机制作了主题。这个时候,我们采用了mobile theme这个方案,做了修改,将mobile detect的缓存代码改了一下,放到了mobile theme下面。

   用户想要支持IE7,8,9,老版本的IE。响应式的主题下面由于使用swiper.js,默认不支持ie9。这个时候,我们决定采用mobile theme方案,在它的基础上进行修改:

   增加对于ie的检查:

function mobile_theme_detect_ie_lower_version() {

  //$mobile_browser = 0;

  $is_ie_lower_version = false;

  $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ""; 

       preg_match('/MSIE (.*?);/', $user_agent, $matches);

       if(count($matches)<2){

         preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches);

       }

 

       if (count($matches)>1){

         //Then we're using IE

         $version = $matches[1];

 

         switch(true){

              case ($version<=8):

                //IE 8 or under!

                $is_ie_lower_version = true; 

                break;

 

              case ($version==9):

                $is_ie_lower_version = true; 

                break;

              case ($version==10):

                break;

              case ($version==11):

                //Version 11!

                break;

 

              default:

                //You get the idea

         }

       } 

 

  /*

  $is_ie7  = (bool)preg_match('/msie 7./i', $user_agent );

  $is_ie8  = (bool)preg_match('/msie 8./i', $user_agent );

  $is_ie9  = (bool)preg_match('/msie 9./i', $user_agent );

  //$is_ie10 = (bool)preg_match('/msie 10./i', $user_agent );

 

  if($is_ie7 || $is_ie8 || $is_ie9){

       $is_ie_lower_version = true; 

        

  }

  */

  return $is_ie_lower_version;

}

 

在hook_custom_theme()里面判断主题:

function mobile_theme_custom_theme() {

  // Retrieve the detection method.

  $method = variable_get('mobile_theme_detection', 'mobile_theme_detect_php');

 

  // If the detection method cannot be found, revert to default

  if (!function_exists($method)) {

    $method = "mobile_theme_detect_php";

  }

 

  // Check if this is a mobile

  $mobile_device = $method();

  if ($mobile_device) {

    //同时修改boost存放目录

    global $conf; 

    $conf['boost_normal_dir'] = 'mobile';

    $conf['boost_gzip_dir'] = 'mobile';     

      

    $theme = variable_get('mobile_theme_selection', 'default');

    if ($theme != 'default') {

      return $theme;

    }

  }

  $ie_lower_version = mobile_theme_detect_ie_lower_version();

  //watchdog('mobile_theme', 'ie_lower_version: '. $ie_lower_version .':'.$_SERVER['HTTP_USER_AGENT']);

  //$ie_lower_version =  true;

  if ($ie_lower_version) {

    //同时修改boost存放目录

    global $conf; 

    $conf['boost_normal_dir'] = 'ie';

    $conf['boost_gzip_dir'] = 'ie';     

      

    $theme = variable_get('mobile_theme_ie', 'default');

    if ($theme != 'default') {

      return $theme;

    }

  } 

}

增加页面缓存支持,使得对于同一个页面,Drupal页面缓存三份:PC,IE,Mobile。

….

  function getMultiple(&$cids) {

    if (is_array($cids)) {

      foreach ($cids as $key => $cid) {

        $cids[$key] = $this->mobile_detect_caching_alter_cid() . $cid;

      }

    }

    return parent::getMultiple($cids);

  }

protected function mobile_detect_caching_alter_cid() {

    $device = '';

       $is_mobile = $this->mobile_detect_by_php();

    if(!empty($is_mobile)){

         $device = 'mobile:';

       }else{

      $ie_lower_version = mobile_theme_detect_ie_lower_version();

      if ($ie_lower_version) {     

           $device = 'ielower:';

         }

       }

 

    return $device;

  }

 

经过测试,上述代码在IE8下面运行良好,同时支持缓存。


论坛:

Drupal版本: