View source
<?php
namespace Drupal\crm_core_contact\Plugin\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SendEmailAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
protected $token;
protected $mailManager;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->mailManager = $mail_manager;
$this->languageManager = $language_manager;
}
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('plugin.manager.mail'), $container
->get('language_manager'));
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $return_as_object ? AccessResult::allowed() : AccessResult::allowed()
->isAllowed();
}
public function executeMultiple(array $objects) {
$subject = isset($this->configuration['subject']) ? $this->configuration['subject'] : '';
$message = isset($this->configuration['message']) ? $this->configuration['message'] : '';
foreach ($objects as $contact) {
$data = array(
'crm_core_contact' => $contact,
);
$options = array(
'clear' => TRUE,
);
$subject = $this->token
->replace($subject, $data, $options);
$message = $this->token
->replace($message, $data, $options);
$email = $contact
->getPrimaryEmail()->value;
$params = array(
'subject' => $subject,
'message' => $message,
);
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$this->mailManager
->mail('crm_core_contact', 'send_email', $email, $langcode, $params);
}
}
public function execute($object = NULL) {
$this
->executeMultiple(array(
$object,
));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = array();
$form['subject'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Subject'),
'#description' => $this
->t('The subject of the message.'),
'#default_value' => $form_state
->getValue('subject', ''),
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => $this
->t('Message'),
'#description' => $this
->t('The message that should be sent.'),
'#default_value' => $form_state
->getValue('message', ''),
);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['subject'] = $form_state
->getValue('subject');
$this->configuration['message'] = $form_state
->getValue('message');
}
}