You are here

public function EmailYamlFormHandler::getMessage in YAML Form 8

Get a fully populated email for a form submission.

Parameters

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Return value

array An array containing message parameters.

Overrides YamlFormHandlerMessageInterface::getMessage

1 call to EmailYamlFormHandler::getMessage()
EmailYamlFormHandler::postSave in src/Plugin/YamlFormHandler/EmailYamlFormHandler.php
Acts on a saved form submission before the insert or update hook is invoked.

File

src/Plugin/YamlFormHandler/EmailYamlFormHandler.php, line 473

Class

EmailYamlFormHandler
Emails a form submission.

Namespace

Drupal\yamlform\Plugin\YamlFormHandler

Code

public function getMessage(YamlFormSubmissionInterface $yamlform_submission) {
  $token_data = [
    'yamlform-submission-options' => [
      'email' => TRUE,
      'excluded_elements' => $this->configuration['excluded_elements'],
      'html' => $this->configuration['html'] && $this
        ->supportsHtml(),
    ],
  ];
  $message = $this->configuration;

  // Replace 'default' values and [tokens] with configuration default values.
  foreach ($message as $key => $value) {
    if ($value === 'default') {
      $message[$key] = $this
        ->getDefaultConfigurationValue($key);
    }
    if (is_string($message[$key])) {
      $message[$key] = $this->tokenManager
        ->replace($message[$key], $yamlform_submission, $token_data);
    }
  }

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

  // Alter body based on the mail system sender.
  if ($this->configuration['html'] && $this
    ->supportsHtml()) {
    switch ($this
      ->getMailSystemSender()) {
      case 'swiftmailer':

        // SwiftMailer requires that the body be valid Markup.
        $message['body'] = Markup::create($message['body']);
        break;
    }
  }
  else {

    // Since Drupal might be rendering a token into the body as markup
    // we need to decode all HTML entities which are being sent as plain text.
    $message['body'] = html_entity_decode($message['body']);
  }

  // Add attachments.
  $message['attachments'] = [];
  if ($this->configuration['attachments'] && $this
    ->supportsAttachments()) {
    $elements = $this->yamlform
      ->getElementsInitializedAndFlattened();
    foreach ($elements as $key => $element) {
      if (!isset($element['#type']) || $element['#type'] != 'managed_file') {
        continue;
      }
      $fids = $yamlform_submission
        ->getData($key);
      if (empty($fids)) {
        continue;
      }

      /** @var \Drupal\file\FileInterface[] $files */
      $files = File::loadMultiple(is_array($fids) ? $fids : [
        $fids,
      ]);
      foreach ($files as $file) {
        $filepath = \Drupal::service('file_system')
          ->realpath($file
          ->getFileUri());
        $message['attachments'][] = [
          'filecontent' => file_get_contents($filepath),
          'filename' => $file
            ->getFilename(),
          'filemime' => $file
            ->getMimeType(),
          // Add URL to be used by resend webform.
          'file' => $file,
        ];
      }
    }
  }

  // Add form submission.
  $message['yamlform_submission'] = $yamlform_submission;
  return $message;
}