Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created December 29, 2011 03:53
Show Gist options
  • Save msonnabaum/1531731 to your computer and use it in GitHub Desktop.
Save msonnabaum/1531731 to your computer and use it in GitHub Desktop.
<?php
// media_gallery.module
function media_gallery_menu() {
$items['media-gallery/detail/%node/%file'] = array(
'page callback' => 'media_gallery_detail_page',
'page arguments' => array(2, 3),
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'file' => 'media_gallery.pages.inc',
'expire callback' => 'media_gallery_detail_page_expire',
'expire arguments' => array('file'),
);
return $items;
}
function media_gallery_detail_page_expire($path, $type, $file) {
switch ($type) {
case 'file':
if (isset($file->media_title)) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node', '=')
->entityCondition('bundle', 'media_gallery', '=')
->fieldCondition('media_gallery_media', 'fid', $file->fid, '=');
if ($res = $query->execute()) {
$expire_paths = array();
foreach ($res['node'] as $node) {
$expire_paths[] = str_replace(array('%node', '%file'), array($node->nid, $file->fid), $path);
}
}
}
break;
}
return $expire_paths;
}
// expire.module
function expire_file_update($file) {
expire_expire_path('file', $file->fid, $file);
}
function expire_node_update($node) {
expire_expire_path('node', $node->nid, $node);
}
function expire_expire_path($type, $id, $obj) {
$tag_paths = expire_tag_paths();
$expire_paths = array();
if (isset($tag_paths[$type])) {
foreach ($tag_paths[$type] as $item) {
if (isset($item['callback'])) {
$expire_paths[] = $item['callback']($item['path'], $type, $obj);
}
else {
$expire_paths[] = str_replace('%' . $type, $id, $item['path']);
}
}
}
expire_cache_derivative($expire_paths);
}
function expire_tag_paths() {
if (!$cache= cache_get('expire_tag_paths')) {
$menus = module_invoke_all('menu');
drupal_alter('menu', $menus);
$tag_paths = array();
foreach ($menus as $path => $info) {
if (!empty($info['expire arguments'])) {
foreach ($info['expire arguments'] as $tag) {
if (!isset($tag_paths[$tag])) {
$tag_paths[$tag] = array();
}
$path_item = array('path' => $path);
if (isset($info['expire callback'])) {
$path_item['callback'] = $info['expire callback'];
}
$tag_paths[$tag][] = $path_item;
}
}
}
cache_set('expire_tag_paths', $tag_paths);
}
else {
$tag_paths = $cache->data;
}
return $tag_paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment