Created
June 19, 2015 23:43
-
-
Save swichers/c0fd342b03070e8b070a to your computer and use it in GitHub Desktop.
Add mtime to files in Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_file_url_alter(). | |
*/ | |
function HOOK_file_url_alter(&$uri) { | |
// Client is complaining about images not refreshing immediately when the user | |
// changes one image for another (and the filename stays the same). Using the | |
// image path flush function doesn't trigger a browser cache refresh for the | |
// image, so we adjust all image URLs to include the last modified time of | |
// the image. | |
$processed_uris = &drupal_static(__FUNCTION__, array()); | |
// Only check URLs with the following extensions. | |
$process_exts = array('gif', 'jpg', 'jpeg', 'png'); | |
$ext = pathinfo($uri, PATHINFO_EXTENSION); | |
$has_ext = in_array(strtolower($ext), $process_exts); | |
// If the extension is one of the preset image types and not already | |
// processed. | |
if ($has_ext && !in_array($uri, $processed_uris)) { | |
// Add this URI to our processed list. | |
$processed_uris[] = $uri; | |
// And finally create the new URL with a timestamp. | |
$uri = file_create_url($uri) . '?mt=' . filemtime(drupal_realpath($uri)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment