display_cache.module

  1. cis7 sites/all/modules/ulmus/display_cache/display_cache.module
  2. cle7 sites/all/modules/ulmus/display_cache/display_cache.module
  3. ecd7 sites/all/modules/ulmus/display_cache/display_cache.module
  4. elmsmedia7 sites/all/modules/ulmus/display_cache/display_cache.module
  5. harmony7 sites/all/modules/ulmus/display_cache/display_cache.module
  6. icor7 sites/all/modules/ulmus/display_cache/display_cache.module
  7. meedjum_blog7 sites/all/modules/ulmus/display_cache/display_cache.module
  8. mooc7 sites/all/modules/ulmus/display_cache/display_cache.module

Module file for Display Cache.

Functions

Constants

File

sites/all/modules/ulmus/display_cache/display_cache.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for Display Cache.
  5. */
  6. define('DISPLAY_CACHE_DISABLED', '0');
  7. define('DISPLAY_CACHE_ENABLED', '1');
  8. define('DISPLAY_CACHE_FIELDS', '2');
  9. /**
  10. * Implements hook_form_FORM_ID_alter().
  11. */
  12. function display_cache_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  13. if (user_access('administer display cache')) {
  14. form_load_include($form_state, 'inc', 'display_cache', 'display_cache.admin');
  15. display_cache_field_ui_form($form, $form_state);
  16. }
  17. }
  18. /**
  19. * Implements hook_flush_caches().
  20. */
  21. function display_cache_flush_caches() {
  22. return array('cache_display_cache');
  23. }
  24. /**
  25. * Implements hook_menu().
  26. */
  27. function display_cache_menu() {
  28. $menu_items = array();
  29. // Menu item for configurations.
  30. $menu_items['admin/config/development/display-cache'] = array(
  31. 'title' => 'Display Cache',
  32. 'description' => 'Form to submit cache flush for display cache.',
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('display_cache_flush_caches_form'),
  35. 'access arguments' => array('administer display cache'),
  36. 'file' => 'display_cache.admin.inc',
  37. 'type' => MENU_NORMAL_ITEM,
  38. );
  39. return $menu_items;
  40. }
  41. /**
  42. * Implements hook_admin_menu_cache_info().
  43. */
  44. function display_cache_admin_menu_cache_info() {
  45. $links['display_cache'] = array(
  46. 'title' => 'Display Caches',
  47. 'callback' => '_display_cache_flush_cache_callback',
  48. );
  49. return $links;
  50. }
  51. /**
  52. * Menu callback to clear the cache table.
  53. */
  54. function _display_cache_flush_cache_callback() {
  55. cache_clear_all('*', 'cache_display_cache', TRUE);
  56. }
  57. /**
  58. * Implements hook_permission().
  59. */
  60. function display_cache_permission() {
  61. return array(
  62. 'administer display cache' => array(
  63. 'title' => t('Administer Display Cache'),
  64. 'description' => t('Permission to administer display cache settings and clear display cache caches from the ui.'),
  65. ),
  66. );
  67. }
  68. /**
  69. * Flushes the display cache of a given element.
  70. *
  71. * If one argument is missing, all following arguments will be replaced with a
  72. * wildcard.
  73. *
  74. * @param string $entity_type
  75. * Type like 'node' or 'comment'.
  76. *
  77. * @param int|object $entity
  78. * (optional) Entity object or entity id.
  79. * Leave empty to flush all entities of given type.
  80. *
  81. * @param string $view_mode
  82. * (optional) View mode like 'full' or 'teaser'.
  83. * Leave empty to flush all view modes of given entity.
  84. *
  85. * @param string $element
  86. * (optional) Element like 'entity', 'field' or 'render_array'.
  87. */
  88. function display_cache_flush_cache($entity_type, $entity = NULL, $view_mode = NULL, $element = NULL) {
  89. if (is_object($entity)) {
  90. $entity = entity_id($entity_type, $entity);
  91. }
  92. $keys = display_cache_get_cache_keys($entity_type, $entity, $view_mode, $element);
  93. $cache_id = implode(':', $keys);
  94. cache_clear_all($cache_id, 'cache_display_cache', TRUE);
  95. }
  96. /**
  97. * Implements hook_entity_delete().
  98. */
  99. function display_cache_entity_delete($entity, $entity_type) {
  100. display_cache_flush_cache($entity_type, $entity);
  101. }
  102. /**
  103. * Implements hook_entity_update().
  104. */
  105. function display_cache_entity_update($entity, $entity_type) {
  106. display_cache_flush_cache($entity_type, $entity);
  107. }
  108. /**
  109. * Returns display cache settings for given entity type, bundle and view mode.
  110. *
  111. * @param string $entity_type
  112. * The entity type like 'node' or 'comment'.
  113. *
  114. * @param string $bundle
  115. * Bundle within the entity type like 'page' or 'article' for nodes.
  116. *
  117. * @param string $view_mode
  118. * View mode like 'full' or 'teaser'.
  119. *
  120. * @return array
  121. * Array with settings.
  122. */
  123. function display_cache_get_settings($entity_type, $bundle, $view_mode) {
  124. $settings = variable_get('display_cache_' . $entity_type . '_' . $bundle . '_' . $view_mode, FALSE);
  125. // Get default settings.
  126. if (!$settings && $view_mode !== 'default') {
  127. $settings = display_cache_get_settings($entity_type, $bundle, 'default');
  128. if (!$settings) {
  129. return array(
  130. 'default' => array(
  131. 'use' => DISPLAY_CACHE_DISABLED,
  132. 'page_granularity' => DISPLAY_CACHE_DISABLED,
  133. 'user_granularity' => DISPLAY_CACHE_DISABLED,
  134. 'granularity' => DISPLAY_CACHE_DISABLED,
  135. ),
  136. );
  137. }
  138. }
  139. return $settings;
  140. }
  141. /**
  142. * Implements hook_entity_view_alter().
  143. */
  144. function display_cache_entity_view_alter(&$build, $entity_type) {
  145. $view_mode = $build['#view_mode'];
  146. $bundle = $build['#bundle'];
  147. $settings = display_cache_get_settings($entity_type, $bundle, $view_mode);
  148. $display_settings = $settings['default'];
  149. // Cache if enabled.
  150. if ($display_settings['use'] === DISPLAY_CACHE_ENABLED) {
  151. if (!empty($build['#' . $entity_type])) {
  152. $entity_id = entity_id($entity_type, $build['#' . $entity_type]);
  153. }
  154. elseif (!empty($build['#entity'])) {
  155. $entity_id = entity_id($entity_type, $build['#entity']);
  156. }
  157. elseif ($entity_type === 'taxonomy_term') {
  158. $entity_id = entity_id($entity_type, $build['#term']);
  159. }
  160. else {
  161. // Create watchdog entry for unsupported entities.
  162. watchdog('display cache', 'The entity type %entity_type seems to be unsupported until now. Please report this to the issue cue of Display Cache so we can fix this.', array('%entity_type' => $entity_type), WATCHDOG_WARNING);
  163. return;
  164. }
  165. $keys = display_cache_get_cache_keys($entity_type, $entity_id, $view_mode, 'entity');
  166. $build['#cache'] = array(
  167. 'keys' => $keys,
  168. 'bin' => 'cache_display_cache',
  169. 'granularity' => $display_settings['granularity'],
  170. );
  171. }
  172. }
  173. /**
  174. * Implements hook_field_attach_view_alter().
  175. */
  176. function display_cache_field_attach_view_alter(&$output, $context) {
  177. $entity_type = $context['entity_type'];
  178. $entity_id = entity_id($entity_type, $context['entity']);
  179. $view_mode = $context['view_mode'];
  180. // Get bundle.
  181. if (isset($output['#bundle'])) {
  182. $bundle = $output['#bundle'];
  183. }
  184. else {
  185. $first_field = reset($output);
  186. $bundle = $first_field['#bundle'];
  187. }
  188. $settings = display_cache_get_settings($entity_type, $bundle, $view_mode);
  189. // Cache if enabled.
  190. if (!empty($settings['default']['use'])) {
  191. foreach (element_children($output) as $field_name) {
  192. if (array_key_exists($field_name, $settings) && $settings[$field_name]['override'] != DISPLAY_CACHE_DISABLED) {
  193. $element = & $output[$field_name];
  194. $keys = display_cache_get_cache_keys($entity_type, $entity_id, $view_mode, $field_name);
  195. $element['#cache'] = array(
  196. 'keys' => $keys,
  197. 'bin' => 'cache_display_cache',
  198. 'granularity' => $settings[$field_name]['granularity'],
  199. );
  200. }
  201. }
  202. }
  203. }
  204. /**
  205. * Implements hook_entity_info_alter().
  206. */
  207. function display_cache_entity_info_alter(&$entity_info) {
  208. foreach ($entity_info as &$entity) {
  209. // Replace view callback with own callback display_cache_view_entity().
  210. if (isset($entity['view callback'])) {
  211. $entity['entity view callback'] = $entity['view callback'];
  212. }
  213. $entity['view callback'] = 'display_cache_view_entity';
  214. }
  215. }
  216. /**
  217. * Alternative entity view callback.
  218. *
  219. * This callback replacing all view callbacks provided by the Entity module. The
  220. * original callback will be called within this callback. In this way it is
  221. * possible to return the cached html before a renderable array is build.
  222. *
  223. * @see entity_view()
  224. * @see display_cache_entity_info_alter()
  225. */
  226. function display_cache_view_entity($entities, $view_mode, $langcode, $entity_type) {
  227. $info = entity_get_info($entity_type);
  228. $return = array();
  229. foreach ($entities as $entity_id => $entity) {
  230. $entity_info = entity_get_info($entity_type);
  231. if (!empty($entity_info['bundle keys']['bundle'])) {
  232. $bundle_key = $entity_info['bundle keys']['bundle'];
  233. }
  234. else {
  235. $bundle_key = $entity_info['entity keys']['bundle'];
  236. }
  237. $bundle = $entity->{$bundle_key};
  238. $settings = display_cache_get_settings($entity_type, $bundle, $view_mode);
  239. $render_array = array();
  240. if ($settings['default']['use'] == DISPLAY_CACHE_ENABLED) {
  241. // Get rendered HTML from cache.
  242. $keys = display_cache_get_cache_keys($entity_type, $entity_id, $view_mode, 'entity');
  243. $cid_parts = array(
  244. '#cache' => array(
  245. 'keys' => $keys,
  246. 'granularity' => $settings['default']['granularity'],
  247. ),
  248. );
  249. $cid = drupal_render_cid_create($cid_parts);
  250. $data = cache_get($cid, 'cache_display_cache');
  251. if (!empty($data->data)) {
  252. $render_array = $data->data;
  253. }
  254. }
  255. if (empty($render_array)) {
  256. // Get renderable array.
  257. if (isset($info['entity view callback'])) {
  258. $entity = entity_key_array_by_property(array($entity), $info['entity keys']['id']);
  259. $render_array = $info['entity view callback']($entity, $view_mode, $langcode, $entity_type);
  260. $render_array = $render_array[$entity_type][$entity_id];
  261. }
  262. elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
  263. $render_array = entity_get_controller($entity_type)->view(array($entity), $view_mode, $langcode);
  264. $render_array = $render_array[$entity_type][$entity_id];
  265. }
  266. }
  267. $return[$entity_type][$entity_id] = $render_array;
  268. }
  269. return $return;
  270. }
  271. /**
  272. * Implements hook_module_implements_alter().
  273. *
  274. * Move the hook implementation of hook_entity_info_alter() to the bottom to be
  275. * able to alter the view callback added by Entity API during this hook.
  276. *
  277. * @see entity_entity_info_alter()
  278. * @see display_cache_install()
  279. */
  280. function display_cache_module_implements_alter(&$implementations, $hook) {
  281. if ($hook == 'entity_info_alter') {
  282. // Move our hook implementation to the bottom.
  283. $group = $implementations['display_cache'];
  284. unset($implementations['display_cache']);
  285. $implementations['display_cache'] = $group;
  286. }
  287. }
  288. /**
  289. * Returns array of given keys for caching.
  290. *
  291. * @param string $entity_type
  292. * Entity type like 'node'.
  293. *
  294. * @param int $entity_id
  295. * Entity ID.
  296. *
  297. * @param string $view_mode
  298. * View mode like 'full' or 'teaser'.
  299. *
  300. * @param null $element
  301. * Element which is cached like 'entity', 'field' or 'render_array'.
  302. *
  303. * @return array
  304. * Array with keys to build the cache id.
  305. */
  306. function display_cache_get_cache_keys($entity_type, $entity_id = NULL, $view_mode = NULL, $element = NULL) {
  307. $keys = array('entity_type' => $entity_type);
  308. if (!empty($entity_id)) {
  309. $keys['entity_id'] = $entity_id;
  310. if (!empty($view_mode)) {
  311. $keys['view_mode'] = $view_mode;
  312. if (!empty($element)) {
  313. $keys['element'] = $element;
  314. }
  315. }
  316. }
  317. drupal_alter('display_cache_cache_keys', $keys);
  318. return $keys;
  319. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.