My task was to put confirmation dialog when clicking vote button to confirm user voting. I am using eventBeforeRate to handle this. But when user decide to not confirm his vote, I cannot prevent from making vote request. There is no chance to modify data variable in eventBeforeRate to prevent voting being processed.

I had to hack Rate javascript code, which I am not really happy with this. In eventBeforeRate I added user response decision to data variable like this:
data.voting_confirmed = false
And then I had to hack the code of Drupal.rateVote function in rate.js to not process request when voting is not confirmed.

Can this be please implemented in rate.js by default? Initialize variable like data.voting_approved = true, but if eventBeforeRate change it to false, do not process request. Now I have to take care of every Rate module update :(

Or is there other simple way to do it? I can imagine of unbinding click events or something like that, which seems more frustrating to do.

Comments

hideaway’s picture

Version: 7.x-1.5 » 7.x-1.6
hideaway’s picture

Issue summary: View changes

typo

hideaway’s picture

Issue summary: View changes

typo

hideaway’s picture

To be more precise, I would like to have rate.js look something like this:

(function ($) {
  Drupal.behaviors.rate = {
    attach: function(context) {
      $('.rate-widget:not(.rate-processed)', context).addClass('rate-processed').each(function () {
        var widget = $(this);
        var ids = widget.attr('id').match(/^rate\-([a-z]+)\-([0-9]+)\-([0-9]+)\-([0-9])$/);
        var data = {
          content_type: ids[1],
          content_id: ids[2],
          widget_id: ids[3],
          widget_mode: ids[4],
          // !!! flag to determine if vote should be processed
          process: true,
        };

        $('a.rate-button', widget).click(function() {
          var token = this.getAttribute('href').match(/rate\=([a-zA-Z0-9\-_]{32,64})/)[1];
          return Drupal.rateVote(widget, data, token);
        });
      });
    }
  };

  Drupal.rateVote = function(widget, data, token) {
    // Invoke JavaScript hook.
    widget.trigger('eventBeforeRate', [data]);

    $(".rate-info", widget).text(Drupal.t('Saving vote...'));

    // Random number to prevent caching, see http://drupal.org/node/1042216#comment-4046618
    var random = Math.floor(Math.random() * 99999);

    var q = (Drupal.settings.rate.basePath.match(/\?/) ? '&' : '?') + 'widget_id=' + data.widget_id + '&content_type=' + data.content_type + '&content_id=' + data.content_id + '&widget_mode=' + data.widget_mode + '&token=' + token + '&destination=' + encodeURIComponent(Drupal.settings.rate.destination) + '&r=' + random;
    if (data.value) {
      q = q + '&value=' + data.value;
    }

    // !!! process vote only if eventBeforeRate did not override voting process
    if (data.process) {
      $.get(Drupal.settings.rate.basePath + q, function(response) {
        if (response.match(/^https?\:\/\/[^\/]+\/(.*)$/)) {
          // We got a redirect.
          document.location = response;
        }
        else {
          // get parent object
          var p = widget.parent();

          // Invoke JavaScript hook.
          widget.trigger('eventAfterRate', [data]);

          widget.before(response);

          // remove widget
          widget.remove();
          widget = undefined;

          Drupal.attachBehaviors(p.get(0));
        }
      });
    }

    return false;
  }
})(jQuery);

Part I have modified is market with "!!!" in the code above

hideaway’s picture

Issue summary: View changes

typo

Doronro’s picture

Hi, just wondering if you found a good solution for this?
I posted a related question and quoted this post there...
https://www.drupal.org/node/2319269

subhojit777’s picture

Issue summary: View changes

I also agree with @hideaway in #2. Seems like we have to handle further execution by altering the data, and then continuing the execution from the JavaScript that stopped further execution.

subhojit777’s picture

StatusFileSize
new2.79 KB

This patch checks for isPrevented and then continues the execution.

subhojit777’s picture

StatusFileSize
new2.9 KB

I checked that in order to save rate to database using rate_save_vote($widget, $content_type, $content_id, $value, $ahah = FALSE, $reset = FALSE), we need the unique token. You can check this in rate_vote_ahah(). I have updated the patch, token will also be passed to the callbacks that have registered to the eventBeforeRate event.

vg3095’s picture

Status: Active » Needs review

Updated to run the test

ivnish’s picture

Status: Needs review » Closed (outdated)

Drupal 7 is EOL. Issue will be closed, but patches are still here

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.