postoffice-1.0.x-dev/extensions/postoffice_compat/src/Email/ContactPageEmail.php
extensions/postoffice_compat/src/Email/ContactPageEmail.php
<?php
namespace Drupal\postoffice_compat\Email;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\contact\ContactFormInterface;
use Drupal\contact\MessageInterface;
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 Drupal\user\UserInterface;
use Symfony\Component\Mime\Email;
/**
* Email for contact form messages generated by the core contact module.
*/
class ContactPageEmail extends Email implements ContactFormEmailInterface, ContactMessageEmailInterface, LocalizedEmailInterface, SiteEmailInterface, TemplateAttachmentsInterface, ThemedEmailInterface {
use CompatEmailTrait;
use ContactMessageEmailTrait;
use SiteEmailTrait;
use TemplateAttachmentsTrait;
use UrlOptionsTrait;
use ContactFormEmailTrait;
/**
* Language code for this message.
*/
protected string $langcode;
/**
* Constructs a new email for messages generated by the core user module.
*/
public function __construct(
string $langcode,
ContactFormInterface $contactForm,
MessageInterface $contactMessage,
UserInterface $sender,
bool $isCopy,
?ImmutableConfig $siteConfig = NULL,
?LanguageManagerInterface $languageManager = NULL,
) {
parent::__construct();
$this->langcode = $langcode;
$this->contactForm = $contactForm;
$this->contactMessage = $contactMessage;
$this->contactSender = $sender;
$this->contactCopy = $isCopy;
$this->siteConfig = $siteConfig;
$this->languageManager = $languageManager;
}
/**
* Constructs a new contact page email from a core message structure.
*/
public static function createFromMessage(array $message, bool $isCopy): static {
return (new static(
$message['langcode'],
$message['params']['contact_form'],
$message['params']['contact_message'],
$message['params']['sender'],
$isCopy
))->headersFromMessage($message)
->to($message['to'])
->subject($message['subject']);
}
/**
* {@inheritdoc}
*/
public function buildThemedEmail(): ?array {
return [
'#theme' => 'postoffice_compat_email_contact_page',
'#contact_form_id' => $this->contactForm->id(),
'#copy' => $this->contactCopy,
'#email' => $this,
'#langcode' => $this->langcode,
'#message' => $this->viewContactMessage(),
];
}
/**
* {@inheritdoc}
*/
public function getLangcode(): string {
return $this->langcode;
}
}
