View source
<?php
namespace Drupal\social_user\Plugin\Action;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Token;
use Drupal\user\UserInterface;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface;
use Egulias\EmailValidator\EmailValidator;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialSendEmail extends ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface, ViewsBulkOperationsPreconfigurationInterface {
protected $token;
protected $storage;
protected $logger;
protected $mailManager;
protected $languageManager;
protected $emailValidator;
protected $allowTextFormat;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EmailValidator $email_validator, $allow_text_format) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->storage = $entity_type_manager
->getStorage('user');
$this->logger = $logger;
$this->mailManager = $mail_manager;
$this->languageManager = $language_manager;
$this->emailValidator = $email_validator;
$this->allowTextFormat = $allow_text_format;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'), $container
->get('entity_type.manager'), $container
->get('logger.factory')
->get('action'), $container
->get('plugin.manager.mail'), $container
->get('language_manager'), $container
->get('email.validator'), $container
->get('current_user')
->hasPermission('use text format mail_html'));
}
public function execute($entity = NULL) {
if (!$entity
->getEntityTypeId() === 'user') {
$this->logger
->notice('Can not send e-mail for %entity', [
'%entity' => $entity
->getEntityTypeId() . ':' . $entity
->id(),
]);
return;
}
if ($entity) {
$langcode = $entity
->getPreferredLangcode();
}
else {
$langcode = $this->languageManager
->getDefaultLanguage()
->getId();
}
$params = [
'context' => $this->configuration,
];
$email = $this
->getEmail($entity);
$message = $this->mailManager
->mail('system', 'action_send_email', $email, $langcode, $params, $this->configuration['reply']);
if ($message['result']) {
$this->logger
->notice('Sent email to %recipient', [
'%recipient' => $email,
]);
}
return $this
->t('Send email');
}
public function getEmail(UserInterface $account) {
return $account
->getEmail();
}
public function buildPreConfigurationForm(array $form, array $values, FormStateInterface $form_state) {
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['reply'] = [
'#type' => 'email',
'#title' => $this
->t('Reply-to'),
'#description' => $this
->t("The email you are about to send is sent from the platform's email address. If you wish to receive replies on this email on your own email address, please specify your email address in this field."),
];
$form['subject'] = [
'#type' => 'textfield',
'#title' => $this
->t('Subject'),
'#required' => TRUE,
'#default_value' => $form_state
->getValue('subject'),
'#maxlength' => '254',
];
$form['message'] = [
'#type' => 'textarea',
'#title' => $this
->t('Message'),
'#required' => TRUE,
'#default_value' => $form_state
->getValue('message'),
'#cols' => '80',
'#rows' => '20',
];
if ($this->allowTextFormat) {
$form['message']['#type'] = 'text_format';
$form['message']['#allowed_formats'] = [
'mail_html',
];
}
$form['#title'] = $this
->t('Send an email to :selected_count members', [
':selected_count' => $this->context['selected_count'],
]);
if (isset($form['list'])) {
unset($form['list']);
}
$form['actions']['submit']['#value'] = $this
->t('Send email');
$classes = [
'button',
'btn',
'waves-effect',
'waves-btn',
];
$form['actions']['submit']['#attributes']['class'] = [
'button--primary',
'js-form-submit',
'form-submit',
'js-form-submit',
'btn-raised',
'btn-primary',
'waves-light',
] + $classes;
$form['actions']['cancel']['#attributes']['class'] = [
'button--danger',
'btn-flat',
] + $classes;
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if ($this->allowTextFormat) {
$this->configuration['message'] = $this->configuration['message']['value'];
}
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return TRUE;
}
}