You are here

protected function MailhandlerComment::getEntityId in Mailhandler 8

Returns a referenced entity ID.

Parameters

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

Return value

string The entity ID.

Throws

\Exception. Throws an exception in case entity ID is not valid.

1 call to MailhandlerComment::getEntityId()
MailhandlerComment::createComment in mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php
Creates a new comment from given mail message.

File

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

Class

MailhandlerComment
Message handler that supports posting comments via email.

Namespace

Drupal\mailhandler_comment\Plugin\inmail\Handler

Code

protected function getEntityId(DefaultAnalyzerResult $result) {
  $subject = $result
    ->getSubject();
  if (!preg_match('/^\\[#(\\d+)\\]\\s+/', $subject, $matches)) {
    throw new \Exception('Referenced entity ID of the comment could not be identified.');
  }

  // Get an entity ID and update the subject.
  $entity_id = $matches[1];
  $subject = str_replace(reset($matches), '', $subject);
  $result
    ->setSubject($subject);
  $entity_type = $this->configuration['entity_type'];
  $commentable_entity_types = \Drupal::entityManager()
    ->getFieldMapByFieldType('comment');
  if (!isset($commentable_entity_types[$entity_type])) {
    throw new \Exception('The referenced entity type ' . $entity_type . ' does not support comments.');
  }
  if (!($entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id))) {
    throw new \Exception('The referenced entity ID (' . $entity_type . ':' . $entity_id . ') does not exists.');
  }
  $allowed_entity_bundles = $commentable_entity_types[$entity_type]['comment']['bundles'];
  $entity_bundle = $entity
    ->bundle();
  if (!in_array($entity_bundle, $allowed_entity_bundles)) {
    throw new \Exception('The bundle ' . $entity_bundle . ' of entity (' . $entity_type . ':' . $entity_id . ') does not support comments.');
  }
  return $entity_id;
}