Skip to content

Instantly share code, notes, and snippets.

@swichers
Created June 19, 2015 23:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swichers/c0fd342b03070e8b070a to your computer and use it in GitHub Desktop.
Save swichers/c0fd342b03070e8b070a to your computer and use it in GitHub Desktop.
Add mtime to files in Drupal
<?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