Drupal Most Recent Poll Block Not Caching
I was working on my new Drupal site, Shop the Internet, yesterday and I tried to insert a poll into a sidebar block that would ask the users which online shopping site did they prefer: Amazon, Bestbuy, Newegg, Target, or Walmart. To test the poll I click Amazon and viola, it posted my vote. Success! Or so I thought…I was logged in to my admin panel in Firefox so I tried it in Internet Explorer. When I refreshed the page, it said it was voted on and was showing the results. So obviously the poll must have been more advanced than I thought, using IP address over cookies. So again I tried it on Google Chrome and same thing. Okay everything is fine. So I go back to work with my site. However after half an hour when my cache data was set to reset I decided to retest it and so I opened Internet Explorer and went to Shop the Internet…but now the poll was available to be voted on. Of course I instantly realized this was because the caching of the poll was wrong, it was caching the poll and saving it as the cached version so that anyone who viewed it after I had voted on it would see the results only for another half an hour. So I started my research on Google of course.
I searched for a bit, but no results and I was wondering if it were my fault that the poll was being cached. But eventually I came upon a Drupal forum post http://drupal.org/node/523676 which gave a patch which is transcribed below:
diff -urp –strip-trailing-cr ../drupal-6.x-dev/modules/poll/poll.install ./modules/poll/poll.install
— ../drupal-6.x-dev/modules/poll/poll.install 2009-01-06 16:46:37.000000000 +0100
+++ ./modules/poll/poll.install 2009-07-18 22:29:37.000000000 +0200
@@ -130,3 +130,21 @@ function poll_schema() {
return $schema;
}+/**
+ * @defgroup updates-6.x-extra Extra poll updates for 6.x
+ * @{
+ */
+
+/**
+ * Fix cache mode for “Most recent poll” block.
+ */
+function poll_update_6000() {
+ $ret = array();
+ $ret[] = update_sql(“UPDATE {blocks} SET cache = 2 WHERE module = ‘poll’ AND delta = ’0′”);
+ return $ret;
+}
+
+/**
+ * @} End of “defgroup updates-6.x-extra”
+ * The next series of updates should start at 7000.
+ */
diff -urp –strip-trailing-cr ../drupal-6.x-dev/modules/poll/poll.module ./modules/poll/poll.module
— ../drupal-6.x-dev/modules/poll/poll.module 2008-12-18 16:46:20.000000000 +0100
+++ ./modules/poll/poll.module 2009-07-18 21:45:53.000000000 +0200
@@ -131,6 +131,7 @@ function poll_block($op = ‘list’, $delta
if (user_access(‘access content’)) {
if ($op == ‘list’) {
$blocks[0]['info'] = t(‘Most recent poll’);
+ $blocks[0]['cache'] = BLOCK_CACHE_PER_USER;
return $blocks;
}
else if ($op == ‘view’) {
@@ -776,6 +777,8 @@ function poll_cancel($form, &$form_state// Subtract from the votes.
db_query(“UPDATE {poll_choices} SET chvotes = chvotes – 1 WHERE nid = %d AND chorder = %d”, $node->nid, $node->vote);
+
+ cache_clear_all();
}/**
If this code looks strange to you you are not alone. I was confused too. I have patched files in linux before, but my webhost, Ixwebhosting, does not have access to ssh or the terminal in general so I was stuck. This was an obvious bug and I could not fix it. All I could find were links to how to convert a patch file to an exe on Windows, but first I have a linux server and also how would I have executed that anyway? So I decided to do it the manual way, open the files and manually edit it. Make sure you back up all original files before editing!
- First open ./modules/poll/poll.install and go to line 130
Findfunction poll_schema() { return $schema; }and add after it
function poll_update_6000() { $ret = array(); $ret[] = update_sql("UPDATE {blocks} SET cache = 2 WHERE module = 'poll' AND delta = '0'"); return $ret; } - Save and exit. Next open ./modules/poll/poll.module and go to line 131
Find:if (user_access('access content')) { if ($op == 'list') { $blocks[0]['info'] = t('Most recent poll'); return $blocks; } else if ($op == 'view') {and replace with:
if (user_access('access content')) { if ($op == 'list') { $blocks[0]['info'] = t('Most recent poll'); $blocks[0]['cache'] = BLOCK_CACHE_PER_USER; return $blocks; } else if ($op == 'view') { - In the same file go to line 777
Find:function poll_cancel($form, &$form_state) {$node = node_load($form['#nid']); global $user; if ($user->uid) {db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid); }else {db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address());} // Subtract from the votes.
db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);}
and replace with:
function poll_cancel($form, &$form_state // Subtract from the votes. db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote); cache_clear_all(); } - Save this file and upload. Now the Poll block should work well.







