, (*1)
Yii2 Moderation Extension
A simple Content Moderation System for Yii2 that allows you to Approve or Reject resources like posts, comments, etc., (*2)
, (*3)
Installation
The preferred way to install this extension is through composer., (*4)
Either run, (*5)
php composer.phar require --prefer-dist yii2mod/yii2-moderation "*"
or add, (*6)
"yii2mod/yii2-moderation": "*"
to the require section of your composer.json., (*7)
Configuration
To enable moderation for a model, use the yii2mod\moderation\ModerationBehavior
behavior and add the status
and moderated_by
columns to your model's table., (*8)
Create a migration to add the new columns. Example Migration:, (*9)
<?php
use yii\db\Migration;
/**
* Handles adding moderation columns to table `post`.
*/
class m161117_092603_add_moderation_columns_to_post_table extends Migration
{
/**
* @inheritdoc
*/
public function up()
{
$this->addColumn('post', 'status', $this->smallInteger());
$this->addColumn('post', 'moderated_by', $this->integer());
}
/**
* @inheritdoc
*/
public function down()
{
$this->dropColumn('post', 'status');
$this->dropColumn('post', 'moderated_by');
}
}
To use ModerationBehavior, insert the following code to your ActiveRecord class:, (*10)
use yii2mod\moderation\ModerationBehavior;
class Post extends ActiveRecord
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
ModerationBehavior::class,
];
}
}
By default, ModerationBehavior will automatically set the moderated_by
attribute., (*11)
If your attribute names are different, you may configure the [[statusAttribute]] and [[moderatedByAttribute]]
properties like the following:, (*12)
use yii2mod\moderation\ModerationBehavior;
class Post extends ActiveRecord
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => ModerationBehavior::class,
'statusAttribute' => 'status_id',
'moderatedByAttribute' => 'moderator_id', // or set to `false` to disable this attribute.
],
];
}
}
Usage
In next examples I will use Post model to demonstrate how the behavior and query class works. You can moderate any ActiveRecord Model., (*13)
Moderate Models
ModerationBehavior provides the following methods for model moderation:, (*14)
$post->markApproved(); // Change post status to Approved
$post->markRejected(); // Change post status to Rejected
$post->markPostponed(); // Change post status to Postponed
$post->markPending(); // Change post status to Pending
Model Status
ModerationBehavior also provides the following methods for checking the moderation status:, (*15)
$post->isPending(); // Check if a post is pending
$post->isApproved(); // Check if a post is approved
$post->isRejected(); // Check if a post is rejected
$post->isPostponed(); // Check if a post is postponed
Events
By default [[yii2mod\moderation\ModerationBehavior]] triggers [[yii2mod\moderation\ModerationBehavior::EVENT_BEFORE_MODERATION]], (*16)
You may attach the event handlers for this event to your ActiveRecord object:, (*17)
$post = Post::findOne($id);
$post->on(ModerationBehavior::EVENT_BEFORE_MODERATION, function ($event) {
$event->isValid = false; // prevent moderation for the model
});
You may also handle these events inside your ActiveRecord class by declaring the corresponding method:, (*18)
use yii2mod\moderation\ModerationBehavior;
class Post extends ActiveRecord
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
ModerationBehavior::class,
];
}
public function beforeModeration()
{
$this->moderated_at = time(); // log the moderation date
return true;
}
}
Query Models
ModerationQuery
adds the ability of getting only approved, rejected, postponed or pending models. Usage example:, (*19)
use yii2mod\moderation\ModerationQuery;
class Post extends ActiveRecord
{
public static function find()
{
return new ModerationQuery(get_called_class());
}
}
Now you can use the following methods:, (*20)
Post::find()->approved()->all(); // It will return all Approved Posts
Post::find()->pending()->all(); // It will return all Pending Posts
Post::find()->rejected()->all(); // It will return all Rejected Posts
Post::find()->postponed()->all(); // It will return all Postponed Posts
Post::find()->approvedWithPending()->all() // It will return all Approved and Pending Posts
Support us
Does your business depend on our contributions? Reach out and support us on Patreon.
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*21)