class EmailAction in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php \Drupal\Core\Action\Plugin\Action\EmailAction
- 10 core/lib/Drupal/Core/Action/Plugin/Action/EmailAction.php \Drupal\Core\Action\Plugin\Action\EmailAction
Sends an email message.
Plugin annotation
@Action(
id = "action_send_email_action",
label = @Translation("Send email"),
type = "system"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Action\ConfigurableActionBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, PluginFormInterface
- class \Drupal\Core\Action\Plugin\Action\EmailAction implements ContainerFactoryPluginInterface
- class \Drupal\Core\Action\ConfigurableActionBase implements ConfigurableInterface, ConfigurablePluginInterface, DependentPluginInterface, PluginFormInterface
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EmailAction
File
- core/
lib/ Drupal/ Core/ Action/ Plugin/ Action/ EmailAction.php, line 28
Namespace
Drupal\Core\Action\Plugin\ActionView source
class EmailAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The user storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The mail manager
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The email validator.
*
* @var \Drupal\Component\Utility\EmailValidatorInterface
*/
protected $emailValidator;
/**
* Constructs a EmailAction object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Component\Utility\EmailValidatorInterface $email_validator
* The email validator.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EmailValidatorInterface $email_validator) {
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if (empty($this->configuration['node'])) {
$this->configuration['node'] = $entity;
}
$recipient = PlainTextOutput::renderFromHtml($this->token
->replace($this->configuration['recipient'], $this->configuration));
// If the recipient is a registered user with a language preference, use
// the recipient's preferred language. Otherwise, use the system default
// language.
$recipient_accounts = $this->storage
->loadByProperties([
'mail' => $recipient,
]);
$recipient_account = reset($recipient_accounts);
if ($recipient_account) {
$langcode = $recipient_account
->getPreferredLangcode();
}
else {
$langcode = $this->languageManager
->getDefaultLanguage()
->getId();
}
$params = [
'context' => $this->configuration,
];
$message = $this->mailManager
->mail('system', 'action_send_email', $recipient, $langcode, $params);
// Error logging is handled by \Drupal\Core\Mail\MailManager::mail().
if ($message['result']) {
$this->logger
->notice('Sent email to %recipient', [
'%recipient' => $recipient,
]);
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'recipient' => '',
'subject' => '',
'message' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['recipient'] = [
'#type' => 'textfield',
'#title' => t('Recipient email address'),
'#default_value' => $this->configuration['recipient'],
'#maxlength' => '254',
'#description' => t('You may also use tokens: [node:author:mail], [comment:author:mail], etc. Separate recipients with a comma.'),
];
$form['subject'] = [
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => $this->configuration['subject'],
'#maxlength' => '254',
'#description' => t('The subject of the message.'),
];
$form['message'] = [
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => $this->configuration['message'],
'#cols' => '80',
'#rows' => '20',
'#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.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$this->emailValidator
->isValid($form_state
->getValue('recipient')) && strpos($form_state
->getValue('recipient'), ':mail') === FALSE) {
// We want the literal %author placeholder to be emphasized in the error message.
$form_state
->setErrorByName('recipient', t('Enter a valid email address or use a token email address such as %author.', [
'%author' => '[node:author:mail]',
]));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['recipient'] = $form_state
->getValue('recipient');
$this->configuration['subject'] = $form_state
->getValue('subject');
$this->configuration['message'] = $form_state
->getValue('message');
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowed();
return $return_as_object ? $result : $result
->isAllowed();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActionBase:: |
public | function |
Executes the plugin for an array of objects. Overrides ActionInterface:: |
3 |
ConfigurableActionBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
ConfigurableActionBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ConfigurableActionBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EmailAction:: |
protected | property | The email validator. | |
EmailAction:: |
protected | property | The language manager. | |
EmailAction:: |
protected | property | A logger instance. | |
EmailAction:: |
protected | property | The mail manager | |
EmailAction:: |
protected | property | The user storage. | |
EmailAction:: |
protected | property | The token service. | |
EmailAction:: |
public | function |
Checks object access. Overrides ActionInterface:: |
|
EmailAction:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
EmailAction:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
EmailAction:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableActionBase:: |
|
EmailAction:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
EmailAction:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
EmailAction:: |
public | function |
Form validation handler. Overrides ConfigurableActionBase:: |
|
EmailAction:: |
public | function |
Constructs a EmailAction object. Overrides ConfigurableActionBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |