Problem/Motivation

There are two problems when you for example want to display a poll on a node using an entity reference field:

a) Forced redirects after vote/cancel to poll/ID
b) The output is render cached, resulting in incorrect output for other users.

Proposed resolution

a) Improve the redirect handling, remove completely from vote submit callback, add a destination to the cancel confirmation form, so we get back to the current page.

b) Discussed a bit with WimLeers, the only option we see is to always render the form in a post_render_cache. That allows it to be safely embeded everywhere, with the downside that the form needs to be built on every page and can't be cached (except page cache for anonymous users without a session).

The advantage is that voting doesn't require a cache tag deletion on the poll ID anymore, as this could possibly result in cache clearing everything (if the poll is displayed on every page in a block, for example). It was only a bogus hack anyway, it made the test work but only because that test did the first request after the cache clear.

Remaining tasks

We could consider to improve the performance for anonymous users by caching 3 variants:
- Vote form
- Results with cancel
- Results without cancel

They should always be the same, so we just need to figure out which case we

Another alternative would be to move more logic out of the view form (like the title, and fields (poll with an image..)) into the view builder, possibly actually using the normal view builder and inside that display the form, which would be limited to the actual form elements. Probably even two separate forms for vote and results to make it less complex.

I think both options can be (separate) follow-ups, but I wanted my thoughts down.

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Status: Active » Needs review
FileSize
7.91 KB

First implementation, seems to work quite fine, used the comment form as an example.

Patch depends on #2331515: Allow to show the poll question in the vote/result templates, which depends on #2331461: Clean up entity handlers/forms, just posting in case someone wants to review this (Hi Wim :)).

Also needs tests.

Status: Needs review » Needs work

The last submitted patch, 1: poll-embed-2335425-1.patch, failed testing.

Wim Leers’s picture

Looking good :)

Berdir’s picture

Status: Needs work » Needs review
FileSize
10.47 KB
4.98 KB

Did some testing with page and render caching. Had to make a few more changes, one thing was the missing validation in case a form submit from a cached page happens for an IP that already voted, another thing is re-adding that the results page is not cached.

I have a project specific behat test, will try to tansform that into a simpletest when the other two issues are done.

Status: Needs review » Needs work

The last submitted patch, 4: poll-embed-2335425-4.patch, failed testing.

Berdir queued 4: poll-embed-2335425-4.patch for re-testing.

The last submitted patch, 4: poll-embed-2335425-4.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
10.27 KB

Re-roll, conflicted on the renamed getCacheTags() method that we remove here.

This will fail on some tests but those tests are bogus I think and we need more tests as mentioned above.

Status: Needs review » Needs work

The last submitted patch, 8: poll-embed-2335425-8.patch, failed testing.

kopeboy’s picture

Will the poll cache per roles that can't vote?
(I'm not a dev..)

Berdir’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
12.11 KB
1.83 KB

Rerolled and updated the tests to reflect how this is now actually working, which I think is the correct way and only possible way, the tests were cheating.

@kopeboy: That would theoretically be possibly, by checking that in the post render cache callback, and returning a cached version for those users. Also note that the page cache is supported, so anonymous users will get a cached page, and it will be validated on submit if their IP is still allowed to vote, as demonstrated by the test.

adammalone’s picture

Status: Needs review » Needs work
+++ b/src/Tests/PollVoteCheckHostnameTest.php
@@ -115,7 +115,7 @@ class PollVoteCheckHostnameTest extends PollTestBase {
+    $this->assertEqual($this->drupalGetHeader('x-drupal-cache'), 'HIT', 'Cached page reeturn.');

spelling mistake on return.

+++ b/src/PollPostRenderCache.php
@@ -0,0 +1,77 @@
+   * #post_render_cache callback; replaces placeholder with poll view form.

Is this comment meant to have the semi-colon and #? phpcs calls this up on not starting with a capital letter.

Other than that, I don't see any problem with this, indeed allowing polls to be embedded correctly is desirable.

Berdir’s picture

Thanks. Will fix the reeeturn, the description there is copied from CommentPostRenderCache in core I think. I can rewrite it to Callback for #... or so.

Berdir’s picture

Status: Needs work » Needs review
FileSize
12.12 KB
1.42 KB

Fixed that, will commit once I confirmed that there was no core change that broke something.

  • Berdir committed 14db281 on 8.x-1.x
    Issue #2335425 by Berdir: Allow to embed polls safely in other content
    
Berdir’s picture

Status: Needs review » Fixed

Still green, committed. Great, that was the last poll customization we had.

Next up: #2409887: Views integration.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.