You are here

protected function MailhandlerComment::createComment in Mailhandler 8

Creates a new comment from given mail message.

Parameters

\Drupal\inmail\MIME\MimeMessageInterface $message: The mail message.

\Drupal\inmail\DefaultAnalyzerResult $result: The analyzer result.

Return value

\Drupal\comment\Entity\Comment The created comment.

Throws

\Exception Throws an exception in case user is not authorized to create a comment.

1 call to MailhandlerComment::createComment()
MailhandlerComment::invoke in mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php

File

mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php, line 107

Class

MailhandlerComment
Message handler that supports posting comments via email.

Namespace

Drupal\mailhandler_comment\Plugin\inmail\Handler

Code

protected function createComment(MimeMessageInterface $message, DefaultAnalyzerResult $result) {
  $entity_id = $this
    ->getEntityId($result);

  // Validate whether user is allowed to post comments.
  $user = $this
    ->validateUser($result);

  // Create a comment entity.
  $comment = Comment::create([
    'entity_type' => $this->configuration['entity_type'],
    'entity_id' => $entity_id,
    'uid' => $user
      ->id(),
    'subject' => $result
      ->getSubject(),
    'comment_body' => [
      'value' => $result
        ->getBody(),
      'format' => 'basic_html',
    ],
    'field_name' => 'comment',
    'comment_type' => 'comment',
    'status' => CommentInterface::PUBLISHED,
  ]);
  $comment
    ->save();
  return $comment;
}