comments icon indicating copy to clipboard operation
comments copied to clipboard

Set a notice through EVENT_BEFORE_SAVE

Open cloudgrayau opened this issue 4 months ago • 1 comments

Question

Is it possible to set a notice on a valid comment, that has been manually marked as spam through a separate plugin? The $comment->addError function doesn't appear to return a notice. Any ideas?

public function parse(string $integration): void {
    $this->integration = $integration;
    Event::on(\verbb\comments\elements\Comment::class, \verbb\comments\elements\Comment::EVENT_BEFORE_SAVE, function(ModelEvent $e){
      $comment = $e->sender;
      $params = [
        'email' => ($comment->userId) ? Craft::$app->getUser()->getIdentity()->email : $comment->email,
        'content' => $comment->getComment()
      ];
      if ((OOPSpam::$plugin->settings->enableContextual) && (!empty(OOPSpam::$plugin->settings->contextualContent)) && (in_array($this->integration, OOPSpam::$plugin->settings->contextual))){
        $entry = Entry::find()->id($comment->ownerId)->one();
        $params['contextual'] = true;
        $params['context'] = 'Title: '.$entry->title.' | Description: '.OOPSpam::$plugin->settings->contextualContent;
      }
      if (!OOPSpam::$plugin->antiSpam->checkSpam($params, $this->getName())){
        $comment->status = \verbb\comments\elements\Comment::STATUS_SPAM;
        $comment->addError('comment', 'Comment blocked due to spam.');
      }
    });
  }

Additional context

No response

cloudgrayau avatar Sep 09 '25 00:09 cloudgrayau