Ticket #239: relative_path.patch

File relative_path.patch, 2.0 KB (added by rk, 9 years ago)
  • sites/all/themes/dmtheme/template.php

    diff --git a/sites/all/themes/dmtheme/template.php b/sites/all/themes/dmtheme/template.php
    index fd4ab0a..aabab7b 100644
    a b function dmtheme_preprocess_block(&$vars) { 
    3434    // 
    3535} 
    3636 
     37/** 
     38 * Implements hook_file_url_alter(). 
     39 * 
     40 * Make all URLs be protocol relative. 
     41 * Note: protocol relatice URLs will cause IE7/8 to download stylesheets twice. 
     42 */ 
     43function dmtheme_file_url_alter(&$url) { 
     44 
     45  global $base_url; 
     46 
     47  static $relative_base_url = NULL, $relative_base_length = NULL; 
     48 
     49  $scheme = file_uri_scheme($url); 
     50 
     51  // For some things (e.g., images) hook_file_url_alter can be called multiple 
     52  // times. So, we have to be sure not to alter it multiple times. If we already 
     53  // are relative protocol we can just return. 
     54  // Only setup the and parse this stuff once. 
     55  if (!$relative_base_url || !$relative_base_length) { 
     56    $relative_base_url = '//' . file_uri_target($base_url); 
     57    $relative_base_length = strlen($relative_base_url); 
     58  } 
     59  if (!$scheme && substr($url, 0, $relative_base_length) == $relative_base_url) { 
     60    return; 
     61  } 
     62 
     63  // Handle the case where we have public files with the scheme public:// or 
     64  // the case the relative path doesn't start with a /. Internal relative urls 
     65  // have the base url prepended to them. 
     66  if (!$scheme || $scheme == 'public') { 
     67 
     68    // Internal Drupal paths. 
     69    if (!$scheme) { 
     70      $path = $url; 
     71    } 
     72    else { 
     73      $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme); 
     74      $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($url); 
     75    } 
     76 
     77    // Clean up Windows paths. 
     78    $path = str_replace('\\', '/', $path); 
     79 
     80    $url = $base_url . '/' . $path; 
     81  } 
     82 
     83  // Convert full URLs to relative protocol. 
     84  $protocols = array('http', 'https'); 
     85  $scheme = file_uri_scheme($url); 
     86  if ($scheme && in_array($scheme, $protocols)) { 
     87    $url = '//' . file_uri_target($url); 
     88  } 
     89}