You are here

public function EmailWebformHandler::getMessage in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformHandler/EmailWebformHandler.php \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::getMessage()

Get a fully populated email for a webform submission.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

Return value

array An array containing message parameters.

Overrides WebformHandlerMessageInterface::getMessage

3 calls to EmailWebformHandler::getMessage()
EmailWebformHandler::postDelete in src/Plugin/WebformHandler/EmailWebformHandler.php
Acts on deleted a webform submission before the delete hook is invoked.
EmailWebformHandler::postSave in src/Plugin/WebformHandler/EmailWebformHandler.php
Acts on a saved webform submission before the insert or update hook is invoked.
ScheduleEmailWebformHandler::scheduleMessage in modules/webform_scheduled_email/src/Plugin/WebformHandler/ScheduleEmailWebformHandler.php
Schedule the sending of an email.

File

src/Plugin/WebformHandler/EmailWebformHandler.php, line 917

Class

EmailWebformHandler
Emails a webform submission.

Namespace

Drupal\webform\Plugin\WebformHandler

Code

public function getMessage(WebformSubmissionInterface $webform_submission) {
  $theme_name = $this->configuration['theme_name'];

  // Switch to custom or default theme.
  $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 = [];

  // Copy configuration to $message.
  foreach ($this->configuration as $configuration_key => $configuration_value) {

    // Get configuration name (to, cc, bcc, from, name, subject, mail)
    // and type (mail, options, or text).
    list($configuration_name, $configuration_type) = strpos($configuration_key, '_') !== FALSE ? explode('_', $configuration_key) : [
      $configuration_key,
      'text',
    ];

    // Set options and continue.
    if ($configuration_type === 'options') {
      $message[$configuration_key] = $configuration_value;
      continue;
    }

    // Determine if configuration value set to '_default'.
    $is_default_configuration = $configuration_value === static::DEFAULT_VALUE;

    // Determine if configuration value should use global configuration.
    $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);
    }

    // Set email addresses.
    if ($configuration_type === 'mail') {
      $emails = $this
        ->getMessageEmails($webform_submission, $configuration_name, $configuration_value);
      $configuration_value = implode(',', array_unique($emails));
    }

    // If Twig enabled render and body, render the Twig template.
    if ($configuration_key === 'body' && $this->configuration['twig']) {
      $message[$configuration_key] = WebformTwigExtension::renderTwigTemplate($webform_submission, $configuration_value, $token_options);
    }
    else {

      // Clear tokens from email values.
      $token_options['clear'] = strpos($configuration_key, '_mail') !== FALSE ? TRUE : FALSE;

      // Get replace token values.
      $token_value = $this
        ->replaceTokens($configuration_value, $webform_submission, $token_data, $token_options);

      // Decode entities for all message values except the HTML message body.
      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;
    }
  }

  // Trim the message body.
  $message['body'] = trim($message['body']);

  // Convert message body to HTML.
  if ($this->configuration['html'] && $this
    ->supportsHtml()) {

    // Apply optional global format to body.
    // NOTE: $message['body'] is not passed-thru Xss::filter() to allow
    // style tags to be supported.
    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);
    }
  }

  // Add attachments.
  $message['attachments'] = $this
    ->getMessageAttachments($webform_submission);

  // Add webform submission.
  $message['webform_submission'] = $webform_submission;

  // Add handler.
  $message['handler'] = $this;

  // Switch back to active theme.
  $this->themeManager
    ->setActiveTheme();
  return $message;
}