EntityTypeAnalyzer.php in Mailhandler 8
File
src/Plugin/inmail/Analyzer/EntityTypeAnalyzer.php
View source
<?php
namespace Drupal\mailhandler\Plugin\inmail\Analyzer;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\inmail\DefaultAnalyzerResult;
use Drupal\inmail\MIME\MimeMessageInterface;
use Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase;
use Drupal\inmail\ProcessorResultInterface;
class EntityTypeAnalyzer extends AnalyzerBase {
public function analyze(MimeMessageInterface $message, ProcessorResultInterface $processor_result) {
$result = $processor_result
->getAnalyzerResult();
$this
->findEntityType($message, $result);
}
protected function findEntityType(MimeMessageInterface $message, DefaultAnalyzerResult $result) {
$subject = $result
->getSubject() ?: $message
->getSubject();
$entity_type = NULL;
$bundle = NULL;
if (preg_match('/^\\[(\\w+)\\]/', $subject, $matches)) {
$entity_type = \Drupal::entityTypeManager()
->hasDefinition($matches[1]) ? $matches[1] : NULL;
$subject = str_replace(reset($matches), '', $subject);
if ($entity_type && preg_match('/^\\[(\\w+)\\]\\s+/', $subject, $matches)) {
$bundle = $this
->getBundle($entity_type, $matches[1]);
$subject = str_replace(reset($matches), '', $subject);
}
}
$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);
}
protected function getBundle($entity_type, $bundle) {
if (\Drupal::entityTypeManager()
->getDefinition($entity_type, FALSE)
->hasKey('bundle')) {
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type);
if (in_array($bundle, array_keys($bundles))) {
return $bundle;
}
}
return NULL;
}
}