You are here

protected function EntityTypeAnalyzer::findEntityType in Mailhandler 8

Analyzes the message subject to extract entity type and bundle information.

Parameters

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

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

1 call to EntityTypeAnalyzer::findEntityType()
EntityTypeAnalyzer::analyze in src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php

File

src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php, line 50

Class

EntityTypeAnalyzer
An entity type and bundle analyzer.

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer

Code

protected function findEntityType(MimeMessageInterface $message, DefaultAnalyzerResult $result) {
  $subject = $result
    ->getSubject() ?: $message
    ->getSubject();
  $entity_type = NULL;
  $bundle = NULL;

  // Match entity type.
  if (preg_match('/^\\[(\\w+)\\]/', $subject, $matches)) {
    $entity_type = \Drupal::entityTypeManager()
      ->hasDefinition($matches[1]) ? $matches[1] : NULL;
    $subject = str_replace(reset($matches), '', $subject);

    // In case entity type was identified successfully, continue to bundle.
    if ($entity_type && preg_match('/^\\[(\\w+)\\]\\s+/', $subject, $matches)) {
      $bundle = $this
        ->getBundle($entity_type, $matches[1]);
      $subject = str_replace(reset($matches), '', $subject);
    }
  }

  // Add entity type context.
  $context_data = [
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  ];
  $context_definition = new ContextDefinition('any', $this
    ->t('Entity type context'));
  $context = new Context($context_definition, $context_data);
  $result
    ->setContext('entity_type', $context);
  $result
    ->setSubject($subject);
}