Problem/Motivation

Vote now uses ajax, the other buttons do not yet. Ideally, all of them would work with ajax.

There is one problem and that is that cancel has a confirmation form.

Proposed resolution

Add #ajax everywhere, implement it correctly.

Remove the confirmation form. If someone clicks the button accidently, they can just vote again.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

edurenye’s picture

Status: Active » Needs review

Removed the confirmation, "added ajjax call" but I don't know why tha ajax function is never called. I upload the patch for @Berdir to take a look at it.

edurenye’s picture

Forgot the patch, sorry.

Status: Needs review » Needs work

The last submitted patch, 3: all_poll_actions_should-2599124-3.patch, failed testing.

edurenye’s picture

Created a followup #2610150: Readd tests for Ajax Poll Vote to readd the tests PollVoteTest::testAjaxPollVote as we have to use a class in the Ajax insertCommand, and we can not test it until this simpletest issue #2469713: Step 2: Create a JavaScriptTestBase using PhantomJs Driver/Binary is solved.

Now the Ajax works but we can not test it, just manual test until the simpletest issue is fixed.

Berdir’s picture

Status: Needs review » Needs work

Yeah, this is a lot better :)

  1. +++ b/src/Form/PollViewForm.php
    @@ -101,7 +102,7 @@ class PollViewForm extends FormBase {
       /**
    -   * Handles vote submit.
    +   * Handles replace form.
        *
        * @param array $form
        *   The previous form.
    

    More something like "Ajax callback to replace the poll form."

    Also, drop the @param and @return, those are usually left out for callbacks like this. And they're wrong now anyway (we no longer return an array)

  2. +++ b/src/Form/PollViewForm.php
    @@ -111,12 +112,16 @@ class PollViewForm extends FormBase {
    +    $form = ['messages' => ['#type' => 'status_messages']] + $form;
    +    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    +    $renderer = \Drupal::service('renderer');
    +    $output = $renderer->renderRoot($form);
    

    We should add some inline docs to describe what we are doing here (embed status messages into the form and then render and return it, replacing the existing form completely)

  3. +++ b/src/Form/PollViewForm.php
    @@ -111,12 +112,16 @@ class PollViewForm extends FormBase {
    +    return $response->addCommand(new ReplaceCommand('.poll-view-form-' . $this->poll->id() . '', $output));
    

    useless ". ''"

  4. +++ b/src/Form/PollViewForm.php
    @@ -256,21 +268,23 @@ class PollViewForm extends FormBase {
    +    \Drupal::logger('poll')->notice('%user\'s vote in Poll #%poll deleted.', array(
    +      '%user' => $this->currentUser()->id(),
    +      '%poll' => $this->poll->id(),
    +    ));
    

    Follow-up: Improve log message, this is pretty useless for an anonymous user currently.

  5. +++ b/src/Form/PollViewForm.php
    @@ -256,21 +268,23 @@ class PollViewForm extends FormBase {
    +    if ($this->getRequest()->query->get('ajax_form')) {
    +      $form_state->setRebuild(TRUE);
    +    }
    

    Add a comment here and below, explaining this:

    // In case of an ajax submission, trigger a form rebuild so that we can return an updated form through the ajax callback.

  6. +++ b/src/Form/PollViewForm.php
    @@ -325,6 +339,9 @@ class PollViewForm extends FormBase {
         // be the poll form or another page that is displaying this poll, for
         // example as a block.
    +    if ($this->getRequest()->query->get('ajax_form')) {
    

    Same here, additionally, the comment above doesn't really make sense here. Maybe move that below the new code.

  7. +++ b/src/Tests/PollVoteTest.php
    @@ -123,7 +120,7 @@ class PollVoteTest extends PollTestBase {
        */
    -  public function testAjaxPollVote() {
    +  /*public function testAjaxPollVote() {
    

    Sorry, I wasn't aware that this *only* tests ajax and needs to be disabled. Yes, in that case, add a @todo.

  8. +++ b/templates/poll-results.html.twig
    @@ -18,7 +18,7 @@
     #}
    -<div class="poll">
    +<div id="poll-{{ poll.id.value }}" class="poll">
    

    I think we no longer need this. Same in the vote form. can also be dropped from preprocess then.

edurenye’s picture

I did all those points and created the follow up: #2611642: Improve log message for anonymous users

Berdir’s picture

Status: Needs review » Fixed

Very nice. Committed!

  • Berdir committed 67ae2d7 on 8.x-1.x authored by edurenye
    Issue #2599124 by edurenye: All poll actions should use ajax, drop...

Status: Fixed » Closed (fixed)

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