MailInvoiceForm.php in Ubercart 8.4
File
uc_order/src/Form/MailInvoiceForm.php
View source
<?php
namespace Drupal\uc_order\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\uc_order\OrderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MailInvoiceForm extends FormBase {
protected $order;
protected $mailManager;
public function __construct(MailManagerInterface $mail_manager) {
$this->mailManager = $mail_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.mail'));
}
public function getFormId() {
return 'uc_order_mail_invoice_form';
}
public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL) {
$this->order = $uc_order;
$form['email'] = [
'#type' => 'email',
'#title' => $this
->t('Recipient e-mail address'),
'#default_value' => $uc_order
->getEmail(),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Mail invoice'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$recipient = $form_state
->getValue('email');
$params = [
'order' => $this->order,
];
$this->mailManager
->mail('uc_order', 'invoice', $recipient, uc_store_mail_recipient_langcode($recipient), $params, uc_store_email_from());
$message = $this
->t('Invoice e-mailed to @email.', [
'@email' => $recipient,
]);
$this
->messenger()
->addMessage($message);
$this->order
->logChanges([
$message,
]);
}
}
Classes
Name |
Description |
MailInvoiceForm |
Defines a form to set the recipient of an invoice, then mails it. |