community_tags_authcache.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/community_tags/community_tags_authcache/community_tags_authcache.module

Functions & methods

NameDescription
community_tags_authcache_authcache_ajaxImplements hook_authcache_ajax().
community_tags_authcache_community_tags_tags_addedThe CT form is cached along with the rest of the node page with mytags removed from the form. Users tags are loaded by authcache and cached by the browser. If any tags are added or removed from node, the cached node page is invalidated which in turn…
community_tags_authcache_community_tags_tags_removed
community_tags_authcache_form_community_tags_form_alterRemove the personalised data in the form - i.e. my tags - these will be loaded via authcache ajax.
_community_tags_authcache_invalidate_nodepage_authcacheRemove selected node pages that have been cached by authcache.
_community_tags_authcache_update_role_keysMaintain the set of role combinations. Store in page cache so that it gets reset when the page cache is cleared.

File

View source
  1. <?php
  2. /**
  3. * Authcache cycle is thus:
  4. * - Node page containing user-specific community tags form is marked for caching by authcache ($is_page_authcache).
  5. * - User's tags are removed from the form and added via ajax to cached page.
  6. * - Method in JS will be called by authcache by matching value from hook_authcache_ajax().
  7. */
  8. /**
  9. * Remove the personalised data in the form - i.e. my tags - these will be
  10. * loaded via authcache ajax.
  11. */
  12. function community_tags_authcache_form_community_tags_form_alter(&$form, &$form_state) {
  13. global $user, $is_page_authcache;
  14. if($is_page_authcache) {
  15. if(_community_tags_tag_access()) {
  16. // unset user specific values in the CT form
  17. unset($form['tags']['#default_value']);
  18. unset($form['tags_refs']['#value']);
  19. // add ct-processed class to prevent CT adding behaviour until user's
  20. // tags are loaded via ajax.
  21. $form['tags']['#attributes']['class'] .= ' ct-processed';
  22. drupal_add_js(drupal_get_path('module', 'community_tags_authcache') .'/community_tags_authcache.js', 'module', 'header');
  23. }
  24. // maintain user role cache keys for clearing selected node pages from authcache page cache
  25. _community_tags_authcache_update_role_keys();
  26. }
  27. }
  28. /**
  29. * Maintain the set of role combinations. Store in page cache so that it gets
  30. * reset when the page cache is cleared.
  31. *
  32. * This should be called from the code that determines how the updated content
  33. * is rendered in the authcached node page. e.g. hook_preprocess for a block
  34. * where $is_page_authcache is set. Or in the more general case could be called
  35. * by an implementation of hook_authcache_info() or hook_authcache_ajax() but might
  36. * result in more redundant calls to cache_clear_all().
  37. */
  38. function _community_tags_authcache_update_role_keys() {
  39. global $user;
  40. // get the list of role keys used so far to cache pages where this function has been invoked.
  41. $cache_entry = cache_get('authcache_role_keys', 'cache_page');
  42. $keys = !empty($cache_entry->data) ? $cache_entry->data : array();
  43. // recreate the roles part of the key that is used to cache the current page.
  44. $key = _authcache_key($user);
  45. // update the list of roles keys if necessary
  46. if (!isset($keys[$key])) {
  47. $keys[$key] = $key;
  48. cache_set('authcache_role_keys', $keys, 'cache_page', CACHE_TEMPORARY);
  49. }
  50. }
  51. /**
  52. * Implements hook_authcache_ajax().
  53. *
  54. * Modifies footer JSON for Ajax call. Cached page, add any context needed to get user's tags as part of ajax call.
  55. * The value set here will be passed to hooks in authcache_custom.php.
  56. */
  57. function community_tags_authcache_authcache_ajax() {
  58. global $user, $theme_key;
  59. $authcache_ajax = array();
  60. if (_community_tags_tag_access()) {
  61. $authcache_ajax['community_tags'] = 1;
  62. }
  63. return $authcache_ajax;
  64. }
  65. /**
  66. * The CT form is cached along with the rest of the node page
  67. * with mytags removed from the form. Users tags are loaded by authcache and cached by the browser.
  68. * If any tags are added or removed from node, the cached node page is invalidated which
  69. * in turn causes a fresh ajax request for mytags.
  70. */
  71. function community_tags_authcache_community_tags_tags_added($node, $user, $terms) {
  72. // invalidate the authcache page cache for this node
  73. _community_tags_authcache_invalidate_nodepage_authcache($node);
  74. }
  75. function community_tags_authcache_community_tags_tags_removed($node, $user, $terms) {
  76. // invalidate the authcache page cache for this node
  77. _community_tags_authcache_invalidate_nodepage_authcache($node);
  78. }
  79. /**
  80. * Remove selected node pages that have been cached by authcache.
  81. */
  82. function _community_tags_authcache_invalidate_nodepage_authcache($node) {
  83. global $base_root;
  84. // Invalidate cached node page for all roles
  85. $cache_entry = cache_get('authcache_role_keys', 'cache_page');
  86. if(!empty($cache_entry->data)) {
  87. $url = url('node/'.$node->nid);
  88. foreach($cache_entry->data as $role_key) {
  89. $cache_key = $role_key . $base_root . $url;
  90. cache_clear_all($cache_key, 'cache_page');
  91. }
  92. }
  93. }