You are here

protected function MailhandlerNode::getContentType in Mailhandler 8

Returns the content type.

Parameters

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

Return value

string The content type.

Throws

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

1 call to MailhandlerNode::getContentType()
MailhandlerNode::createNode in src/Plugin/inmail/Handler/MailhandlerNode.php
Creates a new node from given mail message.

File

src/Plugin/inmail/Handler/MailhandlerNode.php, line 158

Class

MailhandlerNode
Message handler that creates a node from a mail message.

Namespace

Drupal\mailhandler\Plugin\inmail\Handler

Code

protected function getContentType(DefaultAnalyzerResult $result) {
  $content_type = $this->configuration['content_type'];
  $node = TRUE;
  if ($content_type == '_mailhandler' && $result
    ->hasContext('entity_type')) {
    $node = $result
      ->getContext('entity_type')
      ->getContextValue()['entity_type'] == 'node';
    $content_type = $result
      ->getContext('entity_type')
      ->getContextValue()['bundle'];
  }
  if (!$content_type || !$node) {
    throw new \Exception('Failed to process the message. The content type does not exist or node entity type is not specified.');
  }

  // Authorize a user.
  $access = $this->entityTypeManager
    ->getAccessControlHandler('node')
    ->createAccess($content_type, $result
    ->getAccount(), [], TRUE);
  if (!$access
    ->isAllowed()) {
    throw new \Exception('Failed to process the message. User is not authorized to create a node of type "' . $content_type . '".');
  }
  return $content_type;
}