$49

Upvote

Documentation  •  Changelog

Documentation

Events

When are events triggered?

When a user casts an upvote or downvote, the "Vote" events are triggered.

When a user removes an existing vote, the "Unvote" events are triggered.

When a user switches to the opposing vote, it will "Unvote" the original before casting a new "Vote".


Craft 2

Upvote events follow the same basic pattern as standard Craft events.

// Do something BEFORE a vote is cast...
craft()->on('upvote.onBeforeVote', function(Event $event) {

    // Available values
    $event->params['id']   // The element ID for the item being voted upon
    $event->params['key']  // An optional key (defaults to NULL)
    $event->params['vote'] // The value of the vote

});
// Do something AFTER a vote is cast...
craft()->on('upvote.onVote', function(Event $event) {

    // Available values
    $event->params['id']   // The element ID for the item being voted upon
    $event->params['key']  // An optional key (defaults to NULL)
    $event->params['vote'] // The value of the vote

});

There are also two events which are raised then a vote is removed (or swapped)...

 // Do something BEFORE a vote is removed...
craft()->on('upvote.onBeforeUnvote', function(Event $event) {

    // Available values
    $event->params['id']   // The element ID for the item being voted upon
    $event->params['key']  // An optional key (defaults to NULL)

});
// Do something AFTER a vote is removed...
craft()->on('upvote.onUnvote', function(Event $event) {

    // Available values
    $event->params['id']       // The element ID for the item being voted upon
    $event->params['key']      // An optional key (defaults to NULL)
    $event->params['antivote'] // The opposing vote value

});

Craft 3

Upvote events follow the same pattern as standard Craft events.

use doublesecretagency\upvote\Upvote;
use doublesecretagency\upvote\events\VoteEvent;
use yii\base\Event;

// Do something BEFORE a vote is cast...
Event::on(Upvote::class, Upvote::EVENT_BEFORE_VOTE, function(VoteEvent $event) {

    // Available values
    $event->id   // The element ID for the item being voted upon
    $event->key  // An optional key (defaults to NULL)
    $event->vote // The value of the vote

});
use doublesecretagency\upvote\Upvote;
use doublesecretagency\upvote\events\VoteEvent;
use yii\base\Event;

// Do something AFTER a vote is cast...
Event::on(Upvote::class, Upvote::EVENT_AFTER_VOTE, function(VoteEvent $event) {

    // Available values
    $event->id   // The element ID for the item being voted upon
    $event->key  // An optional key (defaults to NULL)
    $event->vote // The value of the vote

});

There are also two events which are raised then a vote is removed (or swapped)...

use doublesecretagency\upvote\Upvote;
use doublesecretagency\upvote\events\UnvoteEvent;
use yii\base\Event;

// Do something BEFORE a vote is removed...
Event::on(Upvote::class, Upvote::EVENT_BEFORE_UNVOTE, function(UnvoteEvent $event) {

    // Available values
    $event->id       // The element ID for the item being voted upon
    $event->key      // An optional key (defaults to NULL)
    $event->antivote // NULL

});
use doublesecretagency\upvote\Upvote;
use doublesecretagency\upvote\events\UnvoteEvent;
use yii\base\Event;

// Do something AFTER a vote is removed...
Event::on(Upvote::class, Upvote::EVENT_AFTER_UNVOTE, function(UnvoteEvent $event) {

    // Available values
    $event->id       // The element ID for the item being voted upon
    $event->key      // An optional key (defaults to NULL)
    $event->antivote // The opposing vote value

});

$event->antivote

During the `EVENT_BEFORE_UNVOTE` event, the `antivote` value will always be `NULL`.

This is because the antivote value is calculated while the vote is being removed. The antivote value is based on the previously existing vote value, which is not available to the event before removing it.

After the vote has been removed, it's possible to know what the vote value was, and therefore know the antivote value.