public
Created — forked from mattfarina/custom.module

Protocol Relative URLs in Drupal 7

  • Download Gist
custom.module
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?php
 
/**
* Implements hook_file_url_alter().
*
* Make all URLs be protocol relative.
* Note: protocol relatice URLs will cause IE7/8 to download stylesheets twice.
* @see http://www.stevesouders.com/blog/2010/02/10/5a-missing-schema-double-download/
*/
function custom_file_url_alter(&$url) {
global $base_url;
 
static $relative_base_url = NULL, $relative_base_length = NULL;
 
$scheme = file_uri_scheme($url);
 
// For some things (e.g., images) hook_file_url_alter can be called multiple
// times. So, we have to be sure not to alter it multiple times. If we already
// are relative protocol we can just return.
// Only setup the and parse this stuff once.
if (!$relative_base_url || !$relative_base_length) {
$relative_base_url = '//' . file_uri_target($base_url);
$relative_base_length = strlen($relative_base_url);
}
if (!$scheme && substr($url, 0, $relative_base_length) == $relative_base_url) {
return;
}
 
// Handle the case where we have public files with the scheme public:// or
// the case the relative path doesn't start with a /. Internal relative urls
// have the base url prepended to them.
if (!$scheme || $scheme == 'public') {
 
// Internal Drupal paths.
if (!$scheme) {
$path = $url;
}
else {
$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($url);
}
 
// Clean up Windows paths.
$path = str_replace('\\', '/', $path);
 
$url = $base_url . '/' . $path;
}
 
// Convert full URLs to relative protocol.
$protocols = array('http', 'https');
$scheme = file_uri_scheme($url);
if ($scheme && in_array($scheme, $protocols)) {
$url = '//' . file_uri_target($url);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.