$49

Star Ratings

Documentation  •  Changelog

Documentation

Events

Craft 2

Star Ratings events follow the same pattern as standard Craft events.

craft()->on('starRatings.onBeforeRate', function(Event $event) {
    // Do something BEFORE the rating is cast...
});
craft()->on('starRatings.onRate', function(Event $event) {
    // Do something AFTER the rating is cast...
});

Both events give you access to special parameters in the `$event` variable. The contents of that variable will look like this...

$event->params['id']          // The element ID for the item being rated
$event->params['key']         // An optional key (defaults to NULL)
$event->params['rating']      // The value of the rating
$event->params['changedFrom'] // The previous rating value (if it exists)
$event->params['userId']      // ID of user who cast rating (null if anonymous)

The optional `key` value allows the user to cast different types of votes on the same element.


Craft 3

Star Ratings events follow the same pattern as standard Craft events.

use doublesecretagency\starratings\StarRatings;
use doublesecretagency\starratings\events\RateEvent;
use yii\base\Event;

Event::on(StarRatings::class, StarRatings::EVENT_BEFORE_RATE, function(RateEvent $event) {
    // Do something BEFORE the rating is cast...
});
use doublesecretagency\starratings\StarRatings;
use doublesecretagency\starratings\events\RateEvent;
use yii\base\Event;

Event::on(StarRatings::class, StarRatings::EVENT_AFTER_RATE, function(RateEvent $event) {
    // Do something AFTER the rating is cast...
});

Both events give you access to the `RateEvent` model in the `$event` variable. The contents of that variable will look like this...

$event->id          // The element ID for the item being rated
$event->key         // An optional key (defaults to NULL)
$event->rating      // The value of the rating
$event->changedFrom // The previous rating value (if it exists)
$event->userId      // ID of user who cast rating (null if anonymous)

The optional `key` value allows the user to cast different types of votes on the same element.