postoffice-1.0.x-dev/extensions/postoffice_compat/src/Email/FallbackEmail.php
extensions/postoffice_compat/src/Email/FallbackEmail.php
<?php
namespace Drupal\postoffice_compat\Email;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Markup;
use Drupal\postoffice\Email\LocalizedEmailInterface;
use Drupal\postoffice\Email\SiteEmailInterface;
use Drupal\postoffice\Email\SiteEmailTrait;
use Drupal\postoffice\Email\TemplateAttachmentsInterface;
use Drupal\postoffice\Email\TemplateAttachmentsTrait;
use Drupal\postoffice\Email\ThemedEmailInterface;
use Drupal\postoffice\Email\UrlOptionsTrait;
use Symfony\Component\Mime\Email;
/**
* Postoffice fallback email for messages generated by core and contrib modules.
*/
class FallbackEmail extends Email implements LocalizedEmailInterface, SiteEmailInterface, TemplateAttachmentsInterface, ThemedEmailInterface {
use CompatEmailTrait;
use SiteEmailTrait;
use TemplateAttachmentsTrait;
use UrlOptionsTrait;
/**
* Constructs a new email for messages generated by the core user module.
*/
public function __construct(
protected string $module,
protected string $key,
protected string $langcode,
?ImmutableConfig $siteConfig = NULL,
?LanguageManagerInterface $languageManager = NULL,
) {
parent::__construct();
$this->siteConfig = $siteConfig;
$this->languageManager = $languageManager;
}
/**
* Constructs a new user email from a core message structure.
*/
public static function createFromMessage(array $message): static {
return (new static($message['module'], $message['key'], $message['langcode']))
->headersFromMessage($message)
->to($message['to'])
->subject($message['subject'])
->html($message['body']);
}
/**
* {@inheritdoc}
*/
public function buildThemedEmail(): ?array {
return [
'#theme' => 'postoffice_compat_email_fallback',
'#body' => [
'#markup' => Markup::create($this->getHtmlBody()),
],
'#email' => $this,
'#module' => $this->module,
'#key' => $this->key,
'#langcode' => $this->langcode,
];
}
/**
* {@inheritdoc}
*/
public function getLangcode(): string {
return $this->langcode;
}
}
