View source
<?php
namespace Drupal\invite_by_email\Plugin\Invite;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\invite\InvitePluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InviteByEmail extends PluginBase implements InvitePluginInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $messenger;
public function getMessenger() {
return $this->messenger;
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('messenger'));
}
public function send($invite) {
$bubbleable_metadata = new BubbleableMetadata();
$token = \Drupal::service('token');
$mail = \Drupal::service('plugin.manager.mail');
$mail_key = $invite
->get('type')->value;
$message = $mail
->mail('invite_by_email', $mail_key, $invite
->get('field_invite_email_address')->value, $invite->activeLangcode, [], $invite
->getOwner()
->getEmail(), FALSE);
if (unserialize(\Drupal::config('invite.invite_type.' . $invite
->get('type')->value)
->get('data'))['html_email']) {
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
}
$message['subject'] = $token
->replace($invite
->get('field_invite_email_subject')->value, [
'invite' => $invite,
], [], $bubbleable_metadata);
$body = [
'#theme' => 'invite_by_email',
'#body' => $token
->replace($invite
->get('field_invite_email_body')->value, [
'invite' => $invite,
], [], $bubbleable_metadata),
];
$message['body'] = \Drupal::service('renderer')
->render($body)
->__toString();
$system = $mail
->getInstance([
'module' => 'invite_by_email',
'key' => $mail_key,
]);
$result = $system
->mail($message);
if ($result) {
$this
->getMessenger()
->addStatus($this
->t('Invitation has been sent.'));
$mail_user = $message['to'];
\Drupal::logger('invite')
->notice('Invitation has been sent for: @mail_user.', [
'@mail_user' => $mail_user,
]);
}
else {
$this
->getMessenger()
->addStatus($this
->t('Failed to send a message.'), 'error');
\Drupal::logger('invite')
->error('Failed to send a message.');
}
}
}