diff -u b/src/ContactEmailerServiceProvider.php b/src/ContactEmailerServiceProvider.php --- b/src/ContactEmailerServiceProvider.php +++ b/src/ContactEmailerServiceProvider.php @@ -9,6 +9,7 @@ use Drupal\Component\Render\FormattableMarkup; use Egulias\EmailValidator\EmailValidator; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Mail\MailFormatHelper; /** * Class ContactEmailerServiceProvider. @@ -122,7 +123,7 @@ } // Subject and body. - $params['subject'] = $this->setSubject($email->subject); + $params['subject'] = $email->subject; $params['message'] = $this->setBody($email->body); // Set to html mail by default. @@ -133,7 +134,7 @@ } // Subject and body. - $params['subject'] = $email->subject; + $params['subject'] = $this->setSubject($email->subject); $params['message'] = $this->setBody($email->body); // Set to html mail by default. @@ -208,39 +209,6 @@ } /** - * Set the email message subject. - * - * @var mixed $subject - * A string. - * - * @return string - * The rendered html or plain text. - */ - protected function setSubject($subject) { - if (!$subject) { - return ''; - } - $subject = $this->tokenizeSubject($subject); - // Send FormattableMarkup to ensure SwiftMailer doesn't further escape it. - return new FormattableMarkup($subject, []); - } - - /** - * Apply tokens to subject value. - * - * @param string $subject - * The subject string value. - * - * @return string - * The tokenized subject. - */ - protected function tokenizeSubject($subject) { - return $this->token->replace($subject, [ - 'contact_message' => $this->contactMessage, - ]); - } - - /** * Set the email message body. * * @var mixed $body @@ -301,6 +269,26 @@ } /** + * Set the email message subject. + * + * @var mixed $body + * A string or text format array. + * + * @return string + * The plain text. + */ + protected function setSubject($subject) { + $subject = $this->tokenizeString($subject); + + // Convert any html to plain text. + $subject = MailFormatHelper::htmlToText($subject); + + // Remove any line breaks as the above method assumes new lines allowed. + $subject = str_replace("\n", '', $subject); + return $subject; + } + + /** * Set the email message body. * * @var mixed $body @@ -321,7 +309,7 @@ // No text format, plain text. $body = [ - '#plain_text' => $this->tokenizeBody($body), + '#plain_text' => $this->tokenizeString($body), ]; } else { @@ -330,7 +318,7 @@ $body = [ '#type' => 'processed_text', '#format' => $body['format'], - '#text' => $this->tokenizeBody($body['value']), + '#text' => $this->tokenizeString($body['value']), ]; } @@ -344,14 +332,14 @@ /** * Apply tokens to body value. * - * @param string $body - * The body string value. + * @param string $string + * The string value such as the subject or body. * * @return string - * The tokenized body. + * The tokenized value. */ - protected function tokenizeBody($body) { - return $this->token->replace($body, [ + protected function tokenizeString($string) { + return $this->token->replace($string, [ 'contact_message' => $this->contactMessage, ]); } only in patch2: unchanged: --- a/src/Form/ContactEmailSettingsForm.php +++ b/src/Form/ContactEmailSettingsForm.php @@ -76,10 +76,12 @@ class ContactEmailSettingsForm extends FormBase { $form['subject'] = [ '#type' => 'textfield', '#title' => $this->t('Subject'), - '#description' => $this->t('The subject of the email.'), '#maxlength' => 255, '#required' => TRUE, '#size' => 64, + '#description' => $this->t('The subject of the email. To add a variable, click within the above field and then click the "Browse available tokens" link below. You will find the fields of your forms within Contact Message. Note that tokens that produce html markup are not supported for an email subject.'), + '#element_validate' => ['token_element_validate'], + '#token_types' => ['contact_message'], '#default_value' => ($email ? $email['subject'] : ''), ];