View source
<?php
namespace Drupal\webform\Plugin\WebformHandler;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Mail;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\webform\Element\WebformAjaxElementTrait;
use Drupal\webform\Element\WebformMessage;
use Drupal\webform\Element\WebformSelectOther;
use Drupal\webform\Plugin\WebformElement\WebformCompositeBase;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\Plugin\WebformHandlerMessageInterface;
use Drupal\webform\Twig\WebformTwigExtension;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformOptionsHelper;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EmailWebformHandler extends WebformHandlerBase implements WebformHandlerMessageInterface {
use WebformAjaxElementTrait;
const OTHER_OPTION = '_other_';
const EMPTY_OPTION = '_empty_';
const DEFAULT_OPTION = '_default_';
const DEFAULT_VALUE = '_default';
protected $currentUser;
protected $moduleHandler;
protected $languageManager;
protected $configFactory;
protected $mailManager;
protected $tokenManager;
protected $themeManager;
protected $elementManager;
protected $defaultValues;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container
->get('current_user');
$instance->moduleHandler = $container
->get('module_handler');
$instance->languageManager = $container
->get('language_manager');
$instance->mailManager = $container
->get('plugin.manager.mail');
$instance->themeManager = $container
->get('webform.theme_manager');
$instance->tokenManager = $container
->get('webform.token_manager');
$instance->elementManager = $container
->get('plugin.manager.webform.element');
return $instance;
}
public function getSummary() {
$settings = $this
->getEmailConfiguration();
array_walk($settings, function (&$value, $key) {
if (is_string($value)) {
$value = preg_replace('/\\[webform:([^:]+)\\]/', '[\\1]', $value);
$value = preg_replace('/\\[webform_role:([^:]+)\\]/', '[\\1]', $value);
$value = preg_replace('/\\[webform_access:type:([^:]+)\\]/', '[\\1]', $value);
$value = preg_replace('/\\[webform_group:role:([^:]+)\\]/', '[group:\\1]', $value);
$value = preg_replace('/\\[webform_group:owner:mail\\]/', '[group:owner]', $value);
$value = preg_replace('/\\[webform_submission:(?:node|source_entity|values):([^]]+)\\]/', '[\\1]', $value);
$value = preg_replace('/\\[webform_submission:([^]]+)\\]/', '[\\1]', $value);
$value = preg_replace('/(:raw|:value)(:html)?\\]/', ']', $value);
}
});
$states = [
WebformSubmissionInterface::STATE_DRAFT_CREATED => $this
->t('Draft created'),
WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this
->t('Draft updated'),
WebformSubmissionInterface::STATE_CONVERTED => $this
->t('Converted'),
WebformSubmissionInterface::STATE_COMPLETED => $this
->t('Completed'),
WebformSubmissionInterface::STATE_UPDATED => $this
->t('Updated'),
WebformSubmissionInterface::STATE_DELETED => $this
->t('Deleted'),
];
$settings['states'] = array_intersect_key($states, array_combine($settings['states'], $settings['states']));
if ($settings['theme_name']) {
$settings['theme_name'] = $this->themeManager
->getThemeName($settings['theme_name']);
}
return [
'#settings' => $settings,
] + parent::getSummary();
}
public function defaultConfiguration() {
return [
'states' => [
WebformSubmissionInterface::STATE_COMPLETED,
],
'to_mail' => static::DEFAULT_VALUE,
'to_options' => [],
'cc_mail' => '',
'cc_options' => [],
'bcc_mail' => '',
'bcc_options' => [],
'from_mail' => static::DEFAULT_VALUE,
'from_options' => [],
'from_name' => static::DEFAULT_VALUE,
'subject' => static::DEFAULT_VALUE,
'body' => static::DEFAULT_VALUE,
'excluded_elements' => [],
'ignore_access' => FALSE,
'exclude_empty' => TRUE,
'exclude_empty_checkbox' => FALSE,
'exclude_attachments' => FALSE,
'html' => TRUE,
'attachments' => FALSE,
'twig' => FALSE,
'debug' => FALSE,
'reply_to' => '',
'return_path' => '',
'sender_mail' => '',
'sender_name' => '',
'theme_name' => '',
'parameters' => [],
];
}
protected function getDefaultConfigurationValues() {
if (isset($this->defaultValues)) {
return $this->defaultValues;
}
$webform_settings = $this->configFactory
->get('webform.settings');
$site_settings = $this->configFactory
->get('system.site');
$body_format = $this->configuration['html'] ? 'html' : 'text';
$default_to_mail = $webform_settings
->get('mail.default_to_mail') ?: $site_settings
->get('mail') ?: ini_get('sendmail_from');
$default_from_mail = $webform_settings
->get('mail.default_from_mail') ?: $site_settings
->get('mail') ?: ini_get('sendmail_from');
$this->defaultValues = [
'states' => [
WebformSubmissionInterface::STATE_COMPLETED,
],
'to_mail' => $default_to_mail,
'to_options' => [],
'cc_mail' => $default_to_mail,
'cc_options' => [],
'bcc_mail' => $default_to_mail,
'bcc_options' => [],
'from_mail' => $default_from_mail,
'from_options' => [],
'from_name' => $webform_settings
->get('mail.default_from_name') ?: $site_settings
->get('name'),
'subject' => $webform_settings
->get('mail.default_subject') ?: 'Webform submission from: [webform_submission:source-entity]',
'body' => $this
->getBodyDefaultValues($body_format),
'reply_to' => $webform_settings
->get('mail.default_reply_to') ?: '',
'return_path' => $webform_settings
->get('mail.default_return_path') ?: '',
'sender_mail' => $webform_settings
->get('mail.default_sender_mail') ?: '',
'sender_name' => $webform_settings
->get('mail.default_sender_name') ?: '',
'theme_name' => '',
];
return $this->defaultValues;
}
protected function getDefaultConfigurationValue($name) {
$default_values = $this
->getDefaultConfigurationValues();
return $default_values[$name];
}
public function getEmailConfiguration() {
$settings = $this
->getSettings();
$email = [];
foreach ($settings as $key => $value) {
$email[$key] = $value === static::DEFAULT_VALUE ? $this
->getDefaultConfigurationValue($key) : $value;
}
return $email;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$this
->applyFormStateToConfiguration($form_state);
$text_element_options_value = [];
$text_element_options_raw = [];
$name_element_options = [];
$mail_element_options = [];
$options_element_options = [];
$elements = $this->webform
->getElementsInitializedAndFlattened();
foreach ($elements as $element_key => $element) {
$element_plugin = $this->elementManager
->getElementInstance($element);
if (!$element_plugin
->isInput($element) || !isset($element['#type'])) {
continue;
}
$element_title = isset($element['#title']) ? new FormattableMarkup('@title (@key)', [
'@title' => $element['#title'],
'@key' => $element_key,
]) : $element_key;
if (isset($element['#options'])) {
$options_element_options["[webform_submission:values:{$element_key}:raw]"] = $element_title;
}
if ($element_plugin
->hasMultipleValues($element)) {
continue;
}
if (!$element_plugin
->isComposite()) {
$text_element_options_value["[webform_submission:values:{$element_key}:value]"] = $element_title;
$text_element_options_raw["[webform_submission:values:{$element_key}:raw]"] = $element_title;
$name_element_options["[webform_submission:values:{$element_key}:raw]"] = $element_title;
if (in_array($element['#type'], [
'email',
'hidden',
'value',
'textfield',
'webform_email_multiple',
'webform_email_confirm',
])) {
$mail_element_options["[webform_submission:values:{$element_key}:raw]"] = $element_title;
}
}
switch ($element['#type']) {
case 'webform_name':
$name_element_options["[webform_submission:values:{$element_key}:value]"] = $element_title;
break;
case 'text_format':
$text_element_options_value["[webform_submission:values:{$element_key}]"] = $element_title;
break;
}
if ($element_plugin instanceof WebformCompositeBase) {
$composite_elements = $element_plugin
->getCompositeElements();
foreach ($composite_elements as $composite_key => $composite_element) {
$composite_element_plugin = $this->elementManager
->getElementInstance($element);
if (!$composite_element_plugin
->isInput($element) || !isset($composite_element['#type'])) {
continue;
}
if (isset($element['#title'])) {
$f_args = [
'@title' => $element['#title'],
'@composite_title' => $composite_element['#title'],
'@key' => $element_key,
'@composite_key' => $composite_key,
];
$composite_title = new FormattableMarkup('@title: @composite_title (@key: @composite_key)', $f_args);
}
else {
$composite_title = "{$element_key}:{$composite_key}";
}
$name_element_options["[webform_submission:values:{$element_key}:{$composite_key}:raw]"] = $composite_title;
if (in_array($composite_element['#type'], [
'email',
'webform_email_multiple',
'webform_email_confirm',
])) {
$mail_element_options["[webform_submission:values:{$element_key}:{$composite_key}:raw]"] = $composite_title;
}
}
}
}
$roles_element_options = [];
if ($roles = $this->configFactory
->get('webform.settings')
->get('mail.roles')) {
$role_names = array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names(TRUE));
if (!in_array('authenticated', $roles)) {
$role_names = array_intersect_key($role_names, array_combine($roles, $roles));
}
foreach ($role_names as $role_name => $role_label) {
$roles_element_options["[webform_role:{$role_name}]"] = new FormattableMarkup('@title (@key)', [
'@title' => $role_label,
'@key' => $role_name,
]);
}
}
$other_element_email_options = [
'[site:mail]' => 'Site email address',
'[current-user:mail]' => 'Current user email address [Authenticated only]',
'[webform:author:mail]' => 'Webform author email address',
'[webform_submission:user:mail]' => 'Webform submission owner email address [Authenticated only]',
];
$other_element_name_options = [
'[site:name]' => 'Site name',
'[current-user:display-name]' => 'Current user display name',
'[current-user:account-name]' => 'Current user account name',
'[webform:author:display-name]' => 'Webform author display name',
'[webform:author:account-name]' => 'Webform author account name',
'[webform_submission:author:display-name]' => 'Webform submission author display name',
'[webform_submission:author:account-name]' => 'Webform submission author account name',
];
$form['#attributes']['novalidate'] = 'novalidate';
$form['to'] = [
'#type' => 'details',
'#title' => $this
->t('Send to'),
'#help' => $this
->t('This is the "To:" email header which will be the person(s) responsible for receiving this webform.'),
'#open' => TRUE,
];
$form['to']['to_mail'] = $this
->buildElement('to_mail', $this
->t('To email'), $this
->t('To email address'), TRUE, $mail_element_options, $options_element_options, $roles_element_options, $other_element_email_options);
$form['to']['cc_mail'] = $this
->buildElement('cc_mail', $this
->t('CC email'), $this
->t('CC email address'), FALSE, $mail_element_options, $options_element_options, $roles_element_options, $other_element_email_options);
$form['to']['bcc_mail'] = $this
->buildElement('bcc_mail', $this
->t('BCC email'), $this
->t('BCC email address'), FALSE, $mail_element_options, $options_element_options, $roles_element_options, $other_element_email_options);
$token_types = [
'webform',
'webform_submission',
];
if (!empty($roles_element_options)) {
$token_types[] = 'webform_role';
}
if ($this->moduleHandler
->moduleExists('webform_access')) {
$token_types[] = 'webform_access';
}
if ($this->moduleHandler
->moduleExists('webform_group')) {
$token_types[] = 'webform_group';
}
$form['to']['token_tree_link'] = $this
->buildTokenTreeElement($token_types);
if (empty($roles_element_options) && $this->currentUser
->hasPermission('administer webform')) {
$route_name = 'webform.config.handlers';
$route_destination = Url::fromRoute('entity.webform.handlers', [
'webform' => $this
->getWebform()
->id(),
])
->toString();
$route_options = [
'query' => [
'destination' => $route_destination,
],
];
$t_args = [
':href' => Url::fromRoute($route_name, [], $route_options)
->toString(),
];
$form['to']['roles_message'] = [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => $this
->t('Please note: You can select which <strong>user roles</strong> are available to receive webform emails by going to the Webform module\'s <a href=":href">admin settings</a> form.', $t_args),
'#message_close' => TRUE,
'#message_id' => 'webform_email_roles_message',
'#message_storage' => WebformMessage::STORAGE_USER,
];
}
$form['from'] = [
'#type' => 'details',
'#title' => $this
->t('Send from (website/domain)'),
'#help' => $this
->t('This is the "From:" email header which should come from <em>you</em>. It should be your brand, company, organization, or website entity.'),
'#open' => TRUE,
];
$form['from']['from_mail'] = $this
->buildElement('from_mail', $this
->t('From email'), $this
->t('From email address'), TRUE, $mail_element_options, $options_element_options, NULL, $other_element_email_options);
$form['from']['from_name'] = $this
->buildElement('from_name', $this
->t('From name'), $this
->t('From name'), FALSE, $name_element_options, NULL, NULL, $other_element_name_options);
$form['from']['token_tree_link'] = $this
->buildTokenTreeElement();
$form['from']['from_name']['from_name']['#states'] = [
'!visible' => [
':input[name="settings[from_mail][other]"]' => [
'value' => [
'pattern' => ',',
],
],
],
];
$form['reply_to'] = [
'#type' => 'details',
'#title' => $this
->t('Reply to (individual/organization)'),
'#help' => $this
->t('The "Reply-To:" email header is used for replying to the email that is received. For example, if you collect a customers email, you would want to reply-to them. If you collect a lead generation form and want to reply to the coordinator, you would reply-to them.'),
'#open' => TRUE,
];
$form['reply_to']['reply_to'] = $this
->buildElement('reply_to', $this
->t('Reply-to email'), $this
->t('Reply-to email address'), FALSE, $mail_element_options, NULL, NULL, $other_element_email_options);
$form['reply_to']['token_tree_link'] = $this
->buildTokenTreeElement($token_types);
$form['message'] = [
'#type' => 'details',
'#title' => $this
->t('Message'),
'#open' => TRUE,
];
$form['message'] += $this
->buildElement('subject', $this
->t('Subject'), $this
->t('subject'), FALSE, $text_element_options_raw);
$has_edit_twig_access = WebformTwigExtension::hasEditTwigAccess() || $this->configuration['twig'];
$body_options = [];
$body_options[WebformSelectOther::OTHER_OPTION] = $this
->t('Custom body…');
if ($has_edit_twig_access) {
$body_options['twig'] = $this
->t('Twig template…');
}
$body_options[static::DEFAULT_VALUE] = $this
->t('Default');
$body_options[(string) $this
->t('Elements')] = $text_element_options_value;
$body_default_format = $this->configuration['html'] ? 'html' : 'text';
$body_default_values = $this
->getBodyDefaultValues();
$body_custom_default_values = $this
->getBodyDefaultValues();
$twig_default_body = $body_custom_default_values[$body_default_format];
$twig_default_body = preg_replace('/(\\[[^]]+\\])/', '{{ webform_token(\'\\1\', webform_submission, [], options) }}', $twig_default_body);
$body_custom_default_values['twig'] = $twig_default_body;
if (WebformOptionsHelper::hasOption($this->configuration['body'], $body_options)) {
$body_select_default_value = $this->configuration['body'];
}
elseif ($this->configuration['twig']) {
$body_select_default_value = 'twig';
$body_custom_default_values['twig'] = $this->configuration['body'];
}
else {
$body_select_default_value = WebformSelectOther::OTHER_OPTION;
$body_custom_default_values[$body_default_format] = $this->configuration['body'];
}
$form['message']['body'] = [
'#type' => 'select',
'#title' => $this
->t('Body'),
'#options' => $body_options,
'#required' => TRUE,
'#default_value' => $body_select_default_value,
];
foreach ($body_default_values as $format => $default_value) {
if ($format === 'html') {
$form['message']['body_custom_' . $format] = [
'#type' => 'webform_html_editor',
'#format' => $this->configFactory
->get('webform.settings')
->get('html_editor.mail_format'),
];
}
else {
$form['message']['body_custom_' . $format] = [
'#type' => 'webform_codemirror',
'#mode' => $format,
];
}
$form['message']['body_custom_' . $format] += [
'#title' => $this
->t('Body custom value (@format)', [
'@format' => $format,
]),
'#title_display' => 'hidden',
'#default_value' => $body_custom_default_values[$format],
'#states' => [
'visible' => [
':input[name="settings[body]"]' => [
'value' => WebformSelectOther::OTHER_OPTION,
],
':input[name="settings[html]"]' => [
'checked' => $format === 'html' ? TRUE : FALSE,
],
],
'required' => [
':input[name="settings[body]"]' => [
'value' => WebformSelectOther::OTHER_OPTION,
],
':input[name="settings[html]"]' => [
'checked' => $format === 'html' ? TRUE : FALSE,
],
],
],
];
$form['message']['body_custom_' . $format]['#parents'] = [
'settings',
'body_custom_' . $format,
];
$form['message']['body_default_' . $format] = [
'#type' => 'webform_codemirror',
'#mode' => $format,
'#title' => $this
->t('Body default value (@format)', [
'@format' => $format,
]),
'#title_display' => 'hidden',
'#default_value' => $body_default_values[$format],
'#attributes' => [
'readonly' => 'readonly',
'disabled' => 'disabled',
],
'#states' => [
'visible' => [
':input[name="settings[body]"]' => [
'value' => static::DEFAULT_VALUE,
],
':input[name="settings[html]"]' => [
'checked' => $format === 'html' ? TRUE : FALSE,
],
],
],
];
}
$form['message']['body_custom_twig'] = [
'#type' => 'webform_codemirror',
'#mode' => 'twig',
'#title' => $this
->t('Body custom value (Twig)'),
'#title_display' => 'hidden',
'#default_value' => $body_custom_default_values['twig'],
'#access' => $has_edit_twig_access,
'#states' => [
'visible' => [
':input[name="settings[body]"]' => [
'value' => 'twig',
],
],
'required' => [
':input[name="settings[body]"]' => [
'value' => 'twig',
],
],
],
'#parents' => [
'settings',
'body_custom_twig',
],
];
$form['message']['body_custom_twig_help'] = WebformTwigExtension::buildTwigHelp() + [
'#access' => $has_edit_twig_access,
'#states' => [
'visible' => [
':input[name="settings[body]"]' => [
'value' => 'twig',
],
],
],
];
$form['message']['token_tree_link'] = $this
->buildTokenTreeElement();
$form['elements'] = [
'#type' => 'details',
'#title' => $this
->t('Included email values/markup'),
'#description' => $this
->t('The selected elements will be included in the [webform_submission:values] token. Individual values may still be printed if explicitly specified as a [webform_submission:values:?] in the email body template.'),
'#open' => $this->configuration['excluded_elements'] ? TRUE : FALSE,
];
$form['elements']['excluded_elements'] = [
'#type' => 'webform_excluded_elements',
'#exclude_markup' => FALSE,
'#webform_id' => $this->webform
->id(),
'#default_value' => $this->configuration['excluded_elements'],
];
$form['elements']['ignore_access'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Always include elements with private and restricted access'),
'#description' => $this
->t('If checked, access controls for included element will be ignored.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['ignore_access'],
];
$form['elements']['exclude_empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude empty elements'),
'#description' => $this
->t('If checked, empty elements will be excluded from the email values.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['exclude_empty'],
];
$form['elements']['exclude_empty_checkbox'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude unselected checkboxes'),
'#description' => $this
->t('If checked, empty checkboxes will be excluded from the email values.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['exclude_empty_checkbox'],
];
$form['elements']['exclude_attachments'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Exclude file elements with attachments'),
'#return_value' => TRUE,
'#description' => $this
->t('If checked, file attachments will be excluded from the email values, but the selected element files will still be attached to the email.'),
'#default_value' => $this->configuration['exclude_attachments'],
'#access' => $this
->getWebform()
->hasAttachments(),
'#disabled' => !$this
->supportsAttachments(),
'#states' => [
'visible' => [
':input[name="settings[attachments]"]' => [
'checked' => TRUE,
],
],
],
];
$elements = $this->webform
->getElementsInitializedFlattenedAndHasValue();
foreach ($elements as $element) {
if (!empty($element['#access_view_roles']) || !empty($element['#private'])) {
$form['elements']['ignore_access_message'] = [
'#type' => 'webform_message',
'#message_message' => $this
->t('This webform contains private and/or restricted access elements, which will only be included if the user submitting the form has access to these elements.'),
'#message_type' => 'warning',
'#states' => [
'visible' => [
':input[name="settings[ignore_access]"]' => [
'checked' => FALSE,
],
],
],
];
break;
}
}
$form['attachments'] = [
'#type' => 'details',
'#title' => $this
->t('Attachments'),
'#access' => $this
->getWebform()
->hasAttachments(),
];
if (!$this
->supportsAttachments()) {
$t_args = [
':href_smtp' => 'https://www.drupal.org/project/smtp',
':href_mailsystem' => 'https://www.drupal.org/project/mailsystem',
':href_swiftmailer' => 'https://www.drupal.org/project/swiftmailer',
];
$form['attachments']['attachments_message'] = [
'#type' => 'webform_message',
'#message_message' => $this
->t('To send email attachments, please install and configure the <a href=":href_smtp">SMTP Authentication Support</a> module or the <a href=":href_mailsystem">Mail System</a> and <a href=":href_swiftmailer">SwiftMailer</a> module.', $t_args),
'#message_type' => 'warning',
'#message_close' => TRUE,
'#message_storage' => WebformMessage::STORAGE_SESSION,
];
}
$form['attachments']['attachments'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include files as attachments'),
'#description' => $this
->t('If checked, only file upload elements selected in the above included email values will be attached to the email.'),
'#return_value' => TRUE,
'#disabled' => !$this
->supportsAttachments(),
'#default_value' => $this->configuration['attachments'],
];
$results_disabled = $this
->getWebform()
->getSetting('results_disabled');
$form['additional'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Additional settings'),
];
$form['additional']['states'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Send email'),
'#options' => [
WebformSubmissionInterface::STATE_DRAFT_CREATED => $this
->t('…when <b>draft is created</b>.'),
WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this
->t('…when <b>draft is updated</b>.'),
WebformSubmissionInterface::STATE_CONVERTED => $this
->t('…when anonymous <b>submission is converted</b> to authenticated.'),
WebformSubmissionInterface::STATE_COMPLETED => $this
->t('…when <b>submission is completed</b>.'),
WebformSubmissionInterface::STATE_UPDATED => $this
->t('…when <b>submission is updated</b>.'),
WebformSubmissionInterface::STATE_DELETED => $this
->t('…when <b>submission is deleted</b>.'),
WebformSubmissionInterface::STATE_LOCKED => $this
->t('…when <b>submission is locked</b>.'),
],
'#access' => $results_disabled ? FALSE : TRUE,
'#default_value' => $results_disabled ? [
WebformSubmissionInterface::STATE_COMPLETED,
] : $this->configuration['states'],
];
$form['additional']['states_message'] = [
'#type' => 'webform_message',
'#message_message' => $this
->t("Because no submission state is checked, this email can only be sent using the 'Resend' form and/or custom code."),
'#message_type' => 'warning',
'#states' => [
'visible' => [
':input[name^="settings[states]"]' => [
'checked' => FALSE,
],
],
],
];
$form['additional']['return_path'] = $this
->buildElement('return_path', $this
->t('Return path'), $this
->t('Return path email address'), FALSE, $mail_element_options, NULL, NULL, $other_element_email_options);
$form['additional']['sender_mail'] = $this
->buildElement('sender_mail', $this
->t('Sender email'), $this
->t('Sender email address'), FALSE, $mail_element_options, $options_element_options, NULL, $other_element_email_options);
$form['additional']['sender_name'] = $this
->buildElement('sender_name', $this
->t('Sender name'), $this
->t('Sender name'), FALSE, $name_element_options, NULL, NULL, $other_element_name_options);
$form['additional']['html'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Send email as HTML'),
'#return_value' => TRUE,
'#access' => $this
->supportsHtml(),
'#default_value' => $this->configuration['html'],
];
$form['additional']['theme_name'] = [
'#type' => 'select',
'#title' => $this
->t('Theme to render this email'),
'#description' => $this
->t('Select the theme that will be used to render this email.'),
'#options' => $this->themeManager
->getThemeNames(),
'#default_value' => $this->configuration['theme_name'],
];
$form['additional']['parameters'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#title' => $this
->t('Custom parameters'),
'#description' => $this
->t('Enter additional custom parameters to be appended to the email message\'s parameters. Custom parameters are used by <a href=":href">email related add-on modules</a>.', [
':href' => 'https://www.drupal.org/docs/8/modules/webform/webform-add-ons#mail',
]),
'#default_value' => $this->configuration['parameters'],
];
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, sent emails will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['debug'],
];
WebformElementHelper::convertRenderMarkupToStrings($form);
$this
->elementTokenValidate($form, $token_types);
return $this
->setSettingsParents($form);
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
$values = $form_state
->getValues();
$values['twig'] = FALSE;
switch ($values['body']) {
case 'twig':
$values['body'] = $values['body_custom_twig'];
$values['twig'] = TRUE;
break;
case WebformSelectOther::OTHER_OPTION:
$body_format = $values['html'] ? 'html' : 'text';
$values['body'] = $values['body_custom_' . $body_format];
break;
}
$form_state
->setValues($values);
$this
->applyFormStateToConfiguration($form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValues();
$values['states'] = array_values(array_filter($values['states']));
foreach ($this->configuration as $name => $value) {
if (isset($values[$name])) {
if (preg_match('/_options$/', $name)) {
$this->configuration[$name] = WebformOptionsHelper::encodeConfig($values[$name]);
}
else {
$this->configuration[$name] = $values[$name];
}
}
}
$this->configuration['debug'] = (bool) $this->configuration['debug'];
}
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$state = $webform_submission
->getWebform()
->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webform_submission
->getState();
if ($this->configuration['states'] && in_array($state, $this->configuration['states'])) {
$message = $this
->getMessage($webform_submission);
$this
->sendMessage($webform_submission, $message);
}
}
public function postDelete(WebformSubmissionInterface $webform_submission) {
if (in_array(WebformSubmissionInterface::STATE_DELETED, $this->configuration['states'])) {
$message = $this
->getMessage($webform_submission);
$this
->sendMessage($webform_submission, $message);
}
}
public function getMessage(WebformSubmissionInterface $webform_submission) {
$theme_name = $this->configuration['theme_name'];
$this->themeManager
->setCurrentTheme($theme_name);
$token_options = [
'email' => TRUE,
'excluded_elements' => $this->configuration['excluded_elements'],
'ignore_access' => $this->configuration['ignore_access'],
'exclude_empty' => $this->configuration['exclude_empty'],
'exclude_empty_checkbox' => $this->configuration['exclude_empty_checkbox'],
'exclude_attachments' => $this->configuration['exclude_attachments'],
'html' => $this->configuration['html'] && $this
->supportsHtml(),
];
$token_data = [];
$message = [];
foreach ($this->configuration as $configuration_key => $configuration_value) {
list($configuration_name, $configuration_type) = strpos($configuration_key, '_') !== FALSE ? explode('_', $configuration_key) : [
$configuration_key,
'text',
];
if ($configuration_type === 'options') {
$message[$configuration_key] = $configuration_value;
continue;
}
$is_default_configuration = $configuration_value === static::DEFAULT_VALUE;
$is_global_configuration = in_array($configuration_key, [
'reply_to',
'return_path',
'sender_mail',
'sender_name',
]);
if ($is_default_configuration || !$configuration_value && $is_global_configuration) {
$configuration_value = $this
->getDefaultConfigurationValue($configuration_key);
}
if ($configuration_type === 'mail') {
$emails = $this
->getMessageEmails($webform_submission, $configuration_name, $configuration_value);
$configuration_value = implode(',', array_unique($emails));
}
if ($configuration_key === 'body' && $this->configuration['twig']) {
$message[$configuration_key] = WebformTwigExtension::renderTwigTemplate($webform_submission, $configuration_value, $token_options);
}
else {
$token_options['clear'] = strpos($configuration_key, '_mail') !== FALSE ? TRUE : FALSE;
$token_value = $this
->replaceTokens($configuration_value, $webform_submission, $token_data, $token_options);
if (!empty($token_value) && is_string($token_value) && !($token_options['html'] && $configuration_key === 'body')) {
$token_value = Html::decodeEntities($token_value);
}
$message[$configuration_key] = $token_value;
}
}
$message['body'] = trim($message['body']);
if ($this->configuration['html'] && $this
->supportsHtml()) {
if ($format = $this->configFactory
->get('webform.settings')
->get('html_editor.mail_format')) {
$build = [
'#type' => 'processed_text',
'#text' => $message['body'],
'#format' => $format,
];
$message['body'] = $this->themeManager
->renderPlain($build);
}
}
$message['attachments'] = $this
->getMessageAttachments($webform_submission);
$message['webform_submission'] = $webform_submission;
$message['handler'] = $this;
$this->themeManager
->setActiveTheme();
return $message;
}
protected function getMessageEmails(WebformSubmissionInterface $webform_submission, $configuration_name, $configuration_value) {
$emails = [];
$element_name = $this
->getElementKeyFromToken($configuration_value);
$element = $element_name ? $this->webform
->getElement($element_name) : NULL;
$element_has_options = $element && isset($element['#options']) ? TRUE : FALSE;
$email_has_options = !empty($this->configuration[$configuration_name . '_options']) ? TRUE : FALSE;
if ($element_has_options && $email_has_options) {
$email_options = WebformOptionsHelper::decodeConfig($this->configuration[$configuration_name . '_options']);
if (!empty($email_options[static::DEFAULT_OPTION])) {
$emails[] = $email_options[static::DEFAULT_OPTION];
}
$options_element_value = $webform_submission
->getElementData($element_name);
if (is_array($options_element_value)) {
$options_values = $options_element_value;
}
elseif ($options_element_value) {
$options_values = [
$options_element_value,
];
}
if (empty($options_values)) {
if (!empty($email_options[static::EMPTY_OPTION])) {
$emails[] = $email_options[static::EMPTY_OPTION];
}
}
else {
foreach ($options_values as $option_value) {
if (!empty($email_options[$option_value])) {
$emails[] = $email_options[$option_value];
}
elseif (!empty($email_options[static::OTHER_OPTION])) {
$emails[] = $email_options[static::OTHER_OPTION];
}
}
}
}
else {
$emails[] = $configuration_value;
}
$emails = implode(',', array_unique($emails));
if (in_array($configuration_name, [
'to',
'cc',
'bcc',
])) {
$roles = $this->configFactory
->get('webform.settings')
->get('mail.roles');
$token_data = [];
$token_data['webform_role'] = $roles;
if ($this->moduleHandler
->moduleExists('webform_access')) {
$token_data['webform_access'] = $webform_submission;
}
if ($this->moduleHandler
->moduleExists('webform_group')) {
$token_data['webform_group'] = $webform_submission;
}
$emails = $this
->replaceTokens($emails, $webform_submission, $token_data);
}
$emails = preg_split('/\\s*,\\s*/', $emails);
$emails = array_filter($emails);
$emails = array_unique($emails);
asort($emails);
return $emails;
}
protected function getMessageAttachments(WebformSubmissionInterface $webform_submission) {
if (empty($this->configuration['attachments']) || !$this
->supportsAttachments()) {
return [];
}
$attachments = [];
$elements = $this
->getWebform()
->getElementsInitializedAndFlattened();
$element_attachments = $this
->getWebform()
->getElementsAttachments();
foreach ($element_attachments as $element_attachment) {
if (isset($this->configuration['excluded_elements'][$element_attachment])) {
continue;
}
$element = $elements[$element_attachment];
$element_plugin = $this->elementManager
->getElementInstance($element);
$attachments = array_merge($attachments, $element_plugin
->getEmailAttachments($element, $webform_submission));
}
return $attachments;
}
public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) {
$to = $message['to_mail'];
$from = $message['from_mail'];
$message['from_name'] = preg_replace('/[<>]/', '', $message['from_name']);
if (!empty($message['from_name'])) {
$from = Mail::formatDisplayName($message['from_name']) . ' <' . $from . '>';
}
$current_langcode = $this->languageManager
->getCurrentLanguage()
->getId();
if (class_exists('\\Symfony\\Component\\Mime\\Address')) {
try {
\Symfony\Component\Mime\Address::create($from);
} catch (\Exception $exception) {
if ($this->configuration['debug']) {
$t_args = [
'%form' => $this
->getWebform()
->label(),
'%handler' => $this
->label(),
'%from_email' => $from,
];
$this
->messenger()
->addWarning($this
->t('%form: Email not sent for %handler handler because the <em>From</em> email (%from_email) is not valid.', $t_args), TRUE);
}
$context = [
'@form' => $this
->getWebform()
->label(),
'@handler' => $this
->label(),
'@from_email' => $from,
];
$this
->getLogger('webform_submission')
->error("@form: Email not sent for '@handler' handler because the 'From' email (@from_email) is not valid.", $context);
return;
}
}
if (!$this
->hasRecipient($webform_submission, $message)) {
if ($this->configuration['debug']) {
$t_args = [
'%form' => $this
->getWebform()
->label(),
'%handler' => $this
->label(),
];
$this
->messenger()
->addWarning($this
->t('%form: Email not sent for %handler handler because a <em>To</em>, <em>CC</em>, or <em>BCC</em> email was not provided.', $t_args), TRUE);
}
return;
}
$build = [
'#theme' => 'webform_email_message_' . ($this->configuration['html'] ? 'html' : 'text'),
'#message' => [
'body' => is_string($message['body']) ? Markup::create($message['body']) : $message['body'],
] + $message,
'#webform_submission' => $webform_submission,
'#handler' => $this,
];
$theme_name = $this->configuration['theme_name'];
$message['body'] = trim((string) $this->themeManager
->renderPlain($build, $theme_name));
if ($this->configuration['html']) {
$message['body'] = Markup::create($message['body']);
}
$key = $this
->getWebform()
->id() . '_' . $this
->getHandlerId();
if (drupal_valid_test_ua()) {
unset($message['webform_submission'], $message['handler']);
}
if (!empty($this->configuration['parameters'])) {
$message += $this
->replaceTokens($this->configuration['parameters'], $webform_submission);
}
unset($message['parameters']);
$result = $this->mailManager
->mail('webform', $key, $to, $current_langcode, $message, $from);
if ($webform_submission
->getWebform()
->hasSubmissionLog()) {
$context = [
'@from_name' => $message['from_name'],
'@from_mail' => $message['from_mail'],
'@to_mail' => $message['to_mail'],
'@subject' => $message['subject'],
'link' => $webform_submission
->id() ? $webform_submission
->toLink($this
->t('View'))
->toString() : NULL,
'webform_submission' => $webform_submission,
'handler_id' => $this
->getHandlerId(),
'operation' => 'sent email',
];
$this
->getLogger('webform_submission')
->notice("'@subject' sent to '@to_mail' from '@from_name' [@from_mail]'.", $context);
}
else {
$context = [
'@form' => $this
->getWebform()
->label(),
'@title' => $this
->label(),
'link' => $this
->getWebform()
->toLink($this
->t('Edit'), 'handlers')
->toString(),
];
$this
->getLogger('webform')
->notice('@form webform sent @title email.', $context);
}
if ($this->configuration['debug']) {
$t_args = [
'%from_name' => $message['from_name'],
'%from_mail' => $message['from_mail'],
'%to_mail' => $message['to_mail'],
'%subject' => $message['subject'],
];
$this
->messenger()
->addWarning($this
->t("%subject sent to %to_mail from %from_name [%from_mail].", $t_args), TRUE);
$debug_message = $this
->buildDebugMessage($webform_submission, $message);
$this
->messenger()
->addWarning($this->themeManager
->renderPlain($debug_message), TRUE);
}
return $result['send'];
}
public function hasRecipient(WebformSubmissionInterface $webform_submission, array $message) {
if (empty($message['to_mail']) && empty($message['cc_mail']) && empty($message['bcc_mail'])) {
return FALSE;
}
else {
return TRUE;
}
}
public function resendMessageForm(array $message) {
$element = [];
$element['to_mail'] = [
'#type' => 'webform_email_multiple',
'#title' => $this
->t('To email'),
'#default_value' => $message['to_mail'],
];
$element['cc_mail'] = [
'#type' => 'webform_email_multiple',
'#title' => $this
->t('CC email'),
'#default_value' => $message['cc_mail'],
];
$element['bcc_mail'] = [
'#type' => 'webform_email_multiple',
'#title' => $this
->t('BCC email'),
'#default_value' => $message['bcc_mail'],
];
$element['from_divider'] = [
'#markup' => '<hr/>',
];
$element['from_mail'] = [
'#type' => 'email',
'#title' => $this
->t('From email'),
'#required' => TRUE,
'#default_value' => $message['from_mail'],
];
$element['from_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('From name'),
'#required' => TRUE,
'#default_value' => $message['from_name'],
];
$element['reply_to_divider'] = [
'#markup' => '<hr/>',
];
$element['reply_to'] = [
'#type' => 'email',
'#title' => $this
->t('Reply to'),
'#default_value' => $message['reply_to'],
];
$element['message_divider'] = [
'#markup' => '<hr/>',
];
$element['subject'] = [
'#type' => 'textfield',
'#title' => $this
->t('Subject'),
'#default_value' => $message['subject'],
];
$element['body'] = [
'#type' => $message['html'] ? 'webform_html_editor' : 'webform_codemirror',
'#title' => $this
->t('Message'),
'#required' => TRUE,
'#default_value' => $message['body'],
];
$element['return_path'] = [
'#type' => 'value',
'#value' => $message['return_path'],
];
$element['html'] = [
'#type' => 'value',
'#value' => $message['html'],
];
$element['attachments'] = [
'#type' => 'value',
'#value' => $message['attachments'],
];
if ($message['attachments']) {
$element['files'] = [
'#type' => 'item',
'#title' => $this
->t('Attachments'),
] + $this
->buildAttachments($message['attachments']);
}
$element['#attached']['library'][] = 'webform/webform.element.html_editor';
$element['#attached']['library'][] = 'webform/webform.element.codemirror.text';
return $element;
}
public function getMessageSummary(array $message) {
return [
'#settings' => $message,
] + parent::getSummary();
}
protected function supportsHtml() {
return TRUE;
}
protected function supportsAttachments() {
if ($this->configFactory
->get('system.mail')
->get('interface.default') === 'test_mail_collector') {
return TRUE;
}
if (strpos($this
->getWebform()
->id(), 'test_') === 0 && $this->moduleHandler
->moduleExists('webform_test')) {
return TRUE;
}
$mailsystem_installed = $this->moduleHandler
->moduleExists('mailsystem');
$smtp_enabled = $this->moduleHandler
->moduleExists('smtp') && $this->configFactory
->get('smtp.settings')
->get('smtp_on');
return $mailsystem_installed || $smtp_enabled;
}
protected function buildDebugMessage(WebformSubmissionInterface $webform_submission, array $message) {
$build = [
'#type' => 'details',
'#title' => $this
->t('Debug: Email: @title', [
'@title' => $this
->label(),
]),
];
$values = [
'from_name' => $this
->t('From name'),
'from_mail' => $this
->t('From mail'),
'to_mail' => $this
->t('To mail'),
'cc_mail' => $this
->t('Cc mail'),
'bcc_mail' => $this
->t('Bcc mail'),
'reply_to' => $this
->t('Reply-to'),
'return_path' => $this
->t('Return path'),
'---' => '---',
'subject' => $this
->t('Subject'),
];
foreach ($values as $name => $title) {
if ($title === '---') {
$build[$name] = [
'#markup' => '<hr />',
];
}
elseif (!empty($message[$name])) {
$build[$name] = [
'#type' => 'item',
'#title' => $title,
'#markup' => $message[$name],
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
'style' => 'margin: 0',
],
];
}
}
$build['body'] = [
'#type' => 'item',
'#title' => $this
->t('Body'),
'#markup' => Markup::create('<pre>' . htmlentities($message['body']) . '</pre>'),
'#wrapper_attributes' => [
'style' => 'margin: 0',
],
];
if (!empty($message['attachments'])) {
$build['attachments_divider'] = [
'#markup' => '<hr />',
];
$build['attachments'] = [
'#type' => 'item',
'#title' => $this
->t('Attachments'),
'#wrapper_attributes' => [
'style' => 'margin: 0',
],
'files' => $this
->buildAttachments($message['attachments']),
];
}
return $build;
}
protected function getMailSystemFormatter() {
$mailsystem_config = $this->configFactory
->get('mailsystem.settings');
$mailsystem_formatter = $mailsystem_config
->get('defaults.formatter');
$mailsystem_formatter = $mailsystem_config
->get('modules.webform.none.formatter') ?: $mailsystem_formatter;
$key = 'email_' . $this
->getHandlerId();
$mailsystem_formatter = $mailsystem_config
->get("modules.webform.{$key}.formatter") ?: $mailsystem_formatter;
return $mailsystem_formatter;
}
protected function getBodyDefaultValues($format = NULL) {
$webform_settings = $this->configFactory
->get('webform.settings');
$formats = [
'text' => $webform_settings
->get('mail.default_body_text') ?: '[webform_submission:values]',
'html' => $webform_settings
->get('mail.default_body_html') ?: '[webform_submission:values]',
];
return $format === NULL ? $formats : $formats[$format];
}
protected function buildElement($name, $title, $label, $required = FALSE, array $element_options = [], array $options_options = NULL, array $role_options = NULL, array $other_options = NULL) {
list($element_name, $element_type) = strpos($name, '_') !== FALSE ? explode('_', $name) : [
$name,
'text',
];
$default_option = $this
->getDefaultConfigurationValue($name);
$options = [];
$options[WebformSelectOther::OTHER_OPTION] = $this
->t('Custom @label…', [
'@label' => $label,
]);
if ($default_option) {
$options[(string) $this
->t('Default')] = [
static::DEFAULT_VALUE => $default_option,
];
}
if (!empty($element_options)) {
$options[(string) $this
->t('Elements')] = $element_options;
}
if ($options_options) {
$options[(string) $this
->t('Options')] = $options_options;
}
if ($role_options) {
$options[(string) $this
->t('Roles')] = $role_options;
}
if ($other_options) {
$options[(string) $this
->t('Other')] = $other_options;
}
$element = [];
$element[$name] = [
'#type' => 'webform_select_other',
'#title' => $title,
'#options' => $options,
'#empty_option' => !$required ? $this
->t('- None -') : NULL,
'#other__title' => $title,
'#other__title_display' => 'invisible',
'#other__placeholder' => $this
->t('Enter @label…', [
'@label' => $label,
]),
'#other__type' => $element_type === 'mail' ? 'webform_email_multiple' : 'textfield',
'#other__allow_tokens' => TRUE,
'#required' => $required,
'#default_value' => $this->configuration[$name],
];
if (in_array($name, [
'reply_to',
'return_path',
'sender_mail',
'sender_name',
])) {
$element[$name]['#empty_option'] = $this
->t('- Default -');
}
if (in_array($name, [
'subject',
])) {
$element[$name]['#other__maxlength'] = NULL;
}
switch ($name) {
case 'from_mail':
$element[$name]['#other__description'] = $this
->t('Multiple email addresses may be separated by commas.') . ' ' . $this
->t("If multiple email addresses are entered the '@name' will be not included in the email.", [
'@name' => $this
->t('From name'),
]);
break;
case 'reply_to':
$element[$name]['#description'] = $this
->t('The email address that a recipient will see when they replying to an email.');
break;
case 'return_path':
$element[$name]['#description'] = $this
->t('The email address to which bounce messages are delivered.');
break;
case 'sender_mail':
$element[$name]['#description'] = $this
->t('The email address submitting the message, if other than shown by the From header');
break;
}
if (in_array($name, [
'reply_to',
'return_path',
'sender_mail',
])) {
$element[$name]['#other__type'] = 'webform_email_multiple';
$element[$name]['#other__cardinality'] = 1;
$element[$name]['#other__description'] = '';
$t_args = [
'@title' => $title,
];
if ($default_email = $this
->getDefaultConfigurationValue($name)) {
$t_args['%email'] = $default_email;
$element[$name]['#description'] .= ' ' . $this
->t("Leave blank to use %email as the '@title' email.", $t_args);
}
else {
$element[$name]['#description'] .= ' ' . $this
->t("Leave blank to automatically use the 'From' address.", $t_args);
}
}
if (!$options_options) {
return $element;
}
$ajax_id = 'webform-email-handler-' . $name;
$this
->buildAjaxElementTrigger($ajax_id, $element[$name]);
$this
->buildAjaxElementUpdate($ajax_id, $element);
$options_name = $element_name . '_options';
if (isset($options_options[$this->configuration[$name]]) && ($token_element_name = $this
->getElementKeyFromToken($this->configuration[$name]))) {
$options_element = $this->webform
->getElement($token_element_name);
$mapping_options = $options_element['#options'];
array_walk($mapping_options, function (&$value, $key) {
$value = '<b>' . $value . '</b>';
});
if (preg_match('/_other$/', $options_element['#type'])) {
$mapping_options[static::OTHER_OPTION] = $this
->t("Other (Used when 'other' value is entered)");
}
if (empty($options_element['#required'])) {
$mapping_options[static::EMPTY_OPTION] = $this
->t('Empty (Used when no option is selected)');
}
$mapping_options[static::DEFAULT_OPTION] = $this
->t('Default (This email address will always be included)');
$destination_placeholder_emails = [
'example@example.com',
'[site:mail]',
];
if ($role_options) {
$role_names = array_keys($role_options);
$destination_placeholder_emails[] = $role_names[0] === '[webform_role:authenticated]' && isset($role_names[1]) ? $role_names[1] : $role_names[0];
}
$element[$options_name] = [
'#type' => 'webform_mapping',
'#title' => $this
->t('@title options', [
'@title' => $title,
]),
'#description' => $this
->t('The selected element has multiple options. You may enter email addresses for each choice. When that choice is selected, an email will be sent to the corresponding addresses. If a field is left blank, no email will be sent for that option. You may use tokens.') . '<br /><br />',
'#description_display' => 'before',
'#required' => TRUE,
'#default_value' => WebformOptionsHelper::decodeConfig($this->configuration[$options_name]),
'#source' => $mapping_options,
'#source__title' => $this
->t('Option'),
'#destination__type' => 'webform_email_multiple',
'#destination__allow_tokens' => TRUE,
'#destination__title' => $this
->t('Email addresses'),
'#destination__description' => NULL,
'#destination__placeholder' => implode(', ', $destination_placeholder_emails),
];
}
else {
$element[$options_name] = [
'#type' => 'value',
'#value' => [],
];
}
$this
->buildAjaxElementWrapper($ajax_id, $element[$options_name]);
return $element;
}
protected function buildAttachments(array $attachments) {
$build = [];
foreach ($attachments as $attachment) {
$t_args = [
'@filename' => $attachment['filename'],
'@filemime' => $attachment['filemime'],
'@filesize' => format_size(mb_strlen($attachment['filecontent'])),
];
if (!empty($attachment['_fileurl'])) {
$t_args[':href'] = $attachment['_fileurl'];
$build[] = [
'#markup' => $this
->t('<strong><a href=":href">@filename</a></strong> (@filemime) - @filesize ', $t_args),
];
}
else {
$build[] = [
'#markup' => $this
->t('<strong>@filename</strong> (@filemime) - @filesize ', $t_args),
];
}
}
return $build;
}
protected function getElementKeyFromToken($token, $format = 'raw') {
if (preg_match('/^\\[webform_submission:values:([^:]+):' . $format . '\\]$/', $token, $match)) {
return $match[1];
}
else {
return NULL;
}
}
protected function buildTokenTreeElement(array $token_types = [
'webform',
'webform_submission',
], $description = NULL) {
$description = $description ?: $this
->t('Use [webform_submission:values:ELEMENT_KEY:raw] to get plain text values.');
return parent::buildTokenTreeElement($token_types, $description);
}
}