Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\action\Plugin\Action;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Render\PlainTextOutput;
|
Chris@0
|
6 use Drupal\Core\Access\AccessResult;
|
Chris@0
|
7 use Drupal\Core\Action\ConfigurableActionBase;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
9 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
10 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
11 use Drupal\Core\Mail\MailManagerInterface;
|
Chris@0
|
12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
13 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
14 use Drupal\Core\Utility\Token;
|
Chris@0
|
15 use Psr\Log\LoggerInterface;
|
Chris@0
|
16 use Egulias\EmailValidator\EmailValidator;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Sends an email message.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @Action(
|
Chris@0
|
23 * id = "action_send_email_action",
|
Chris@0
|
24 * label = @Translation("Send email"),
|
Chris@0
|
25 * type = "system"
|
Chris@0
|
26 * )
|
Chris@0
|
27 */
|
Chris@0
|
28 class EmailAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The token service.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var \Drupal\Core\Utility\Token
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $token;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * The user storage.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $storage;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * A logger instance.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var \Psr\Log\LoggerInterface
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $logger;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * The mail manager
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var \Drupal\Core\Mail\MailManagerInterface
|
Chris@0
|
55 */
|
Chris@0
|
56 protected $mailManager;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * The language manager.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
62 */
|
Chris@0
|
63 protected $languageManager;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * The email validator.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @var \Egulias\EmailValidator\EmailValidator
|
Chris@0
|
69 */
|
Chris@0
|
70 protected $emailValidator;
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Constructs a EmailAction object.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @param array $configuration
|
Chris@0
|
76 * A configuration array containing information about the plugin instance.
|
Chris@0
|
77 * @param string $plugin_id
|
Chris@0
|
78 * The plugin ID for the plugin instance.
|
Chris@0
|
79 * @param mixed $plugin_definition
|
Chris@0
|
80 * The plugin implementation definition.
|
Chris@0
|
81 * @param \Drupal\Core\Utility\Token $token
|
Chris@0
|
82 * The token service.
|
Chris@0
|
83 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
84 * The entity manager.
|
Chris@0
|
85 * @param \Psr\Log\LoggerInterface $logger
|
Chris@0
|
86 * A logger instance.
|
Chris@0
|
87 * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
|
Chris@0
|
88 * The mail manager.
|
Chris@0
|
89 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
90 * The language manager.
|
Chris@0
|
91 * @param \Egulias\EmailValidator\EmailValidator $email_validator
|
Chris@0
|
92 * The email validator.
|
Chris@0
|
93 */
|
Chris@0
|
94 public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityManagerInterface $entity_manager, LoggerInterface $logger, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EmailValidator $email_validator) {
|
Chris@0
|
95 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
96
|
Chris@0
|
97 $this->token = $token;
|
Chris@0
|
98 $this->storage = $entity_manager->getStorage('user');
|
Chris@0
|
99 $this->logger = $logger;
|
Chris@0
|
100 $this->mailManager = $mail_manager;
|
Chris@0
|
101 $this->languageManager = $language_manager;
|
Chris@0
|
102 $this->emailValidator = $email_validator;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * {@inheritdoc}
|
Chris@0
|
107 */
|
Chris@0
|
108 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
109 return new static($configuration, $plugin_id, $plugin_definition,
|
Chris@0
|
110 $container->get('token'),
|
Chris@0
|
111 $container->get('entity.manager'),
|
Chris@0
|
112 $container->get('logger.factory')->get('action'),
|
Chris@0
|
113 $container->get('plugin.manager.mail'),
|
Chris@0
|
114 $container->get('language_manager'),
|
Chris@0
|
115 $container->get('email.validator')
|
Chris@0
|
116 );
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function execute($entity = NULL) {
|
Chris@0
|
123 if (empty($this->configuration['node'])) {
|
Chris@0
|
124 $this->configuration['node'] = $entity;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 $recipient = PlainTextOutput::renderFromHtml($this->token->replace($this->configuration['recipient'], $this->configuration));
|
Chris@0
|
128
|
Chris@0
|
129 // If the recipient is a registered user with a language preference, use
|
Chris@0
|
130 // the recipient's preferred language. Otherwise, use the system default
|
Chris@0
|
131 // language.
|
Chris@0
|
132 $recipient_accounts = $this->storage->loadByProperties(['mail' => $recipient]);
|
Chris@0
|
133 $recipient_account = reset($recipient_accounts);
|
Chris@0
|
134 if ($recipient_account) {
|
Chris@0
|
135 $langcode = $recipient_account->getPreferredLangcode();
|
Chris@0
|
136 }
|
Chris@0
|
137 else {
|
Chris@0
|
138 $langcode = $this->languageManager->getDefaultLanguage()->getId();
|
Chris@0
|
139 }
|
Chris@0
|
140 $params = ['context' => $this->configuration];
|
Chris@0
|
141
|
Chris@16
|
142 $message = $this->mailManager->mail('system', 'action_send_email', $recipient, $langcode, $params);
|
Chris@16
|
143 // Error logging is handled by \Drupal\Core\Mail\MailManager::mail().
|
Chris@16
|
144 if ($message['result']) {
|
Chris@0
|
145 $this->logger->notice('Sent email to %recipient', ['%recipient' => $recipient]);
|
Chris@0
|
146 }
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * {@inheritdoc}
|
Chris@0
|
151 */
|
Chris@0
|
152 public function defaultConfiguration() {
|
Chris@0
|
153 return [
|
Chris@0
|
154 'recipient' => '',
|
Chris@0
|
155 'subject' => '',
|
Chris@0
|
156 'message' => '',
|
Chris@0
|
157 ];
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * {@inheritdoc}
|
Chris@0
|
162 */
|
Chris@0
|
163 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
164 $form['recipient'] = [
|
Chris@0
|
165 '#type' => 'textfield',
|
Chris@0
|
166 '#title' => t('Recipient email address'),
|
Chris@0
|
167 '#default_value' => $this->configuration['recipient'],
|
Chris@0
|
168 '#maxlength' => '254',
|
Chris@0
|
169 '#description' => t('You may also use tokens: [node:author:mail], [comment:author:mail], etc. Separate recipients with a comma.'),
|
Chris@0
|
170 ];
|
Chris@0
|
171 $form['subject'] = [
|
Chris@0
|
172 '#type' => 'textfield',
|
Chris@0
|
173 '#title' => t('Subject'),
|
Chris@0
|
174 '#default_value' => $this->configuration['subject'],
|
Chris@0
|
175 '#maxlength' => '254',
|
Chris@0
|
176 '#description' => t('The subject of the message.'),
|
Chris@0
|
177 ];
|
Chris@0
|
178 $form['message'] = [
|
Chris@0
|
179 '#type' => 'textarea',
|
Chris@0
|
180 '#title' => t('Message'),
|
Chris@0
|
181 '#default_value' => $this->configuration['message'],
|
Chris@0
|
182 '#cols' => '80',
|
Chris@0
|
183 '#rows' => '20',
|
Chris@0
|
184 '#description' => t('The message that should be sent. You may include placeholders like [node:title], [user:account-name], [user:display-name] and [comment:body] to represent data that will be different each time message is sent. Not all placeholders will be available in all contexts.'),
|
Chris@0
|
185 ];
|
Chris@0
|
186 return $form;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * {@inheritdoc}
|
Chris@0
|
191 */
|
Chris@0
|
192 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
193 if (!$this->emailValidator->isValid($form_state->getValue('recipient')) && strpos($form_state->getValue('recipient'), ':mail') === FALSE) {
|
Chris@0
|
194 // We want the literal %author placeholder to be emphasized in the error message.
|
Chris@0
|
195 $form_state->setErrorByName('recipient', t('Enter a valid email address or use a token email address such as %author.', ['%author' => '[node:author:mail]']));
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * {@inheritdoc}
|
Chris@0
|
201 */
|
Chris@0
|
202 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
203 $this->configuration['recipient'] = $form_state->getValue('recipient');
|
Chris@0
|
204 $this->configuration['subject'] = $form_state->getValue('subject');
|
Chris@0
|
205 $this->configuration['message'] = $form_state->getValue('message');
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * {@inheritdoc}
|
Chris@0
|
210 */
|
Chris@0
|
211 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@0
|
212 $result = AccessResult::allowed();
|
Chris@0
|
213 return $return_as_object ? $result : $result->isAllowed();
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 }
|