You are here

public function ContactEmail::getBody in Contact Emails 8

Get the email body.

Parameters

\Drupal\contact\MessageInterface $message: The contact message.

Return value

string The body of the email.

Overrides ContactEmailInterface::getBody

File

src/Entity/ContactEmail.php, line 302

Class

ContactEmail
Defines the Contact Email entity.

Namespace

Drupal\contact_emails\Entity

Code

public function getBody(MessageInterface $message) {
  $body = $this
    ->get('message');
  $format = $this
    ->getFormat($message);

  // Prepare render array based on text format.
  if ($format == 'text/plain; charset=UTF-8; format=flowed; delsp=yes') {
    $build = [
      '#plain_text' => $this
        ->tokenizeString($body->value, $message),
    ];
  }
  else {
    $build['text'] = [
      '#type' => 'processed_text',
      '#format' => $body->format,
      '#text' => $this
        ->tokenizeString($body->value, $message),
    ];
  }

  // Maybe append the entire message.
  if ($this
    ->get('append_message')->value) {

    // Render the contact message using the mail view mode.
    $render_controller = \Drupal::entityTypeManager()
      ->getViewBuilder($message
      ->getEntityTypeId());
    $message_build = $render_controller
      ->view($message, 'mail');

    // Either add to the html text or plan text.
    if (isset($build['text']['#text'])) {
      $build['message'] = $message_build;
      $build['message']['#prefix'] = '<br /><br />';
    }
    else {
      $message_markup = \Drupal::service('renderer')
        ->renderPlain($message_build);
      $build['#plain_text'] .= "\n\n" . $message_markup;
    }
  }

  // Render the body.
  return \Drupal::service('renderer')
    ->renderPlain($build);
}