You are here

public function EmailYamlFormHandler::sendMessage in YAML Form 8

Sends and logs a form submission message.

Parameters

array $message: An array of message parameters.

Overrides YamlFormHandlerMessageInterface::sendMessage

1 call to EmailYamlFormHandler::sendMessage()
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 550

Class

EmailYamlFormHandler
Emails a form submission.

Namespace

Drupal\yamlform\Plugin\YamlFormHandler

Code

public function sendMessage(array $message) {

  // Send mail.
  $to = $message['to_mail'];
  $from = $message['from_mail'] . ($message['from_name'] ? ' <' . $message['from_name'] . '>' : '');
  $current_langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $this->mailManager
    ->mail('yamlform', 'email.' . $this
    ->getHandlerId(), $to, $current_langcode, $message, $from);

  // Log message.
  $context = [
    '@form' => $this
      ->getYamlForm()
      ->label(),
    '@title' => $this
      ->label(),
  ];
  $this->logger
    ->notice('@form form sent @title email.', $context);

  // Debug by displaying send email onscreen.
  if ($this->configuration['debug']) {
    $t_args = [
      '%from_name' => $message['from_name'],
      '%from_mail' => $message['from_mail'],
      '%to_mail' => $message['to_mail'],
      '%subject' => $message['subject'],
    ];
    $build = [];
    $build['message'] = [
      '#markup' => $this
        ->t('%subject sent to %to_mail from %from_name [%from_mail].', $t_args),
      '#prefix' => '<b>',
      '#suffix' => '</b>',
    ];
    if ($message['html']) {
      $build['body'] = [
        '#markup' => $message['body'],
        '#allowed_tags' => Xss::getAdminTagList(),
        '#prefix' => '<div>',
        '#suffix' => '</div>',
      ];
    }
    else {
      $build['body'] = [
        '#markup' => $message['body'],
        '#prefix' => '<pre>',
        '#suffix' => '</pre>',
      ];
    }
    drupal_set_message(\Drupal::service('renderer')
      ->render($build), 'warning');
  }
}