diff --git a/README.md b/README.md index ebd0d6e..a3fa007 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,18 @@ class Commenter extends NovaCommenter Then use this class instead of the default `Commenter` class within your resources. +### Customizing the comment + +By default, the `Comment` model will strip tags from the comment body. If you would like to modify this behavior, you can pass an callback to the `Comment::whenCreating` method. This callback will receive the comment model instance as a parameter. + +```php +Comment::whenCreating(function (Comment $comment) { + $comment->comment = strip_tags($comment->comment); +}); +``` + +```php + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. diff --git a/src/Models/Comment.php b/src/Models/Comment.php index 30548a7..8d7c29a 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -4,6 +4,7 @@ namespace KirschbaumDevelopment\NovaComments\Models; +use Closure; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -17,6 +18,11 @@ class Comment extends Model */ protected $table = 'nova_comments'; + /** + * @var Closure|callable|null + */ + protected static $whenCreating = null; + /** * The "booting" method of the model. */ @@ -24,13 +30,17 @@ public static function boot(): void { parent::boot(); - static::creating( - function ($comment): void { - if (auth()->check()) { - $comment->commenter_id = auth()->id(); - } + static::creating(function ($comment): void { + if (auth()->check()) { + $comment->commenter_id = auth()->id(); + } + + if (static::$whenCreating) { + call_user_func(static::$whenCreating, $comment); + } else { + $comment->comment = strip_tags($comment->comment); } - ); + }); } /** @@ -48,4 +58,12 @@ public function commenter(): BelongsTo { return $this->belongsTo(config('auth.providers.users.model'), 'commenter_id'); } + + /** + * @param Closure|callable $callback + */ + public static function whenCreating($callback): void + { + static::$whenCreating = $callback; + } }