You are here

function template_preprocess_easy_email_type_preview in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x easy_email.module \template_preprocess_easy_email_type_preview()

Prepares variables for Email entities.

Default template: easy_email.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing the user information and any
  • attributes: HTML attributes for the containing element.

File

./easy_email.module, line 233
Contains easy_email.module.

Code

function template_preprocess_easy_email_type_preview(array &$variables) {

  // Fetch EasyEmail Entity Object.

  /** @var \Drupal\easy_email\Entity\EasyEmailInterface $easy_email */
  $easy_email = $variables['easy_email'];
  if ($easy_email
    ->hasField('body_html') && !empty($easy_email
    ->getHtmlBody())) {
    $variables['preview_html_url'] = Url::fromRoute('entity.easy_email_type.preview_html', [
      'easy_email_type' => $easy_email
        ->bundle(),
    ], [
      'query' => [
        'uuid' => $easy_email
          ->uuid(),
      ],
    ]);
  }

  /** @var \Drupal\easy_email\Entity\EasyEmailTypeInterface $easy_email_type */
  $easy_email_type = \Drupal::entityTypeManager()
    ->getStorage('easy_email_type')
    ->load($easy_email
    ->bundle());
  if ($easy_email
    ->hasField('body_plain') && (!empty($easy_email_type
    ->getGenerateBodyPlain()) || !empty($easy_email
    ->getPlainBody()))) {
    $variables['preview_plain_url'] = Url::fromRoute('entity.easy_email_type.preview_plain', [
      'easy_email_type' => $easy_email
        ->bundle(),
    ], [
      'query' => [
        'uuid' => $easy_email
          ->uuid(),
      ],
    ]);
  }
  $variables['headers'] = [];

  /** @var \Drupal\easy_email\Service\EmailHandlerInterface $email_handler */
  $email_handler = \Drupal::service('easy_email.handler');
  $message = $email_handler
    ->preview($easy_email);
  if (!empty($message['headers'])) {
    foreach ($message['headers'] as $header => $value) {
      $variables['headers'][] = [
        'name' => $header,
        'value' => $value,
      ];
    }
  }
  $variables['headers'][] = [
    'name' => 'To',
    'value' => implode(', ', $easy_email
      ->getRecipientAddresses()),
  ];
  $variables['headers'][] = [
    'name' => 'Subject',
    'value' => $easy_email
      ->getSubject(),
  ];
  $variables['inbox_preview'] = [
    '#theme' => 'easy_email_inbox_preview_view',
    '#easy_email' => $easy_email,
  ];
  $attachments = [];

  /** @var \Drupal\easy_email\Service\EmailAttachmentEvaluatorInterface $attachment_evalutator */
  $attachment_evalutator = \Drupal::service('easy_email.attachment_evaluator');
  $attachment_evalutator
    ->evaluateAttachments($easy_email, FALSE);
  foreach ($easy_email
    ->getEvaluatedAttachments() as $file) {
    $attachments[] = Link::fromTextAndUrl($file->filename, Url::fromUri(file_create_url($file->uri), [
      'attributes' => [
        'target' => '_blank',
      ],
    ]));
  }
  if (!empty($attachments)) {
    $variables['attachments'] = [
      '#theme' => 'item_list',
      '#items' => $attachments,
    ];
  }
}