You are here

public function PetPreviewForm::buildForm in Previewable email templates 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/PetPreviewForm.php \Drupal\pet\Form\PetPreviewForm::buildForm()
  2. 8 src/Form/PetPreviewForm.php \Drupal\pet\Form\PetPreviewForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/PetPreviewForm.php, line 32

Class

PetPreviewForm
PetPreviewForm.

Namespace

Drupal\pet\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, PetInterface $pet = NULL) {
  $storage = $form_state
    ->getStorage();
  $storage['pet'] = $pet;
  if (!isset($storage['step'])) {
    $storage['step'] = 1;
  }
  $has_recipient_callback = isset($storage['recipient_callback']);
  switch ($storage['step']) {
    case 1:
      if ($has_recipient_callback) {
        $recipients = $this
          ->t('Recipient list will be generated for preview.');
      }
      elseif (isset($storage['recipients'])) {
        $recipients = $storage['recipients'];
      }
      else {
        $recipients = \Drupal::currentUser()
          ->getEmail();
      }
      $form['recipients'] = [
        '#title' => $this
          ->t('To'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#default_value' => $recipients,
        '#description' => $this
          ->t('Enter the recipient(s) comma separated. A separate email will be sent to each, with user token substitution if the email corresponds to a site user.'),
        '#disabled' => $has_recipient_callback,
      ];
      $form['reply_to'] = [
        '#title' => $this
          ->t('Reply-To'),
        '#type' => 'email',
        '#required' => FALSE,
        '#default_value' => isset($storage['reply_to']) ? $storage['reply_to'] : $pet
          ->getReplyTo(),
        '#description' => $this
          ->t('Reply-To email address. If empty, the site email address will be user.'),
      ];
      $form['copies'] = [
        '#title' => $this
          ->t('Copies'),
        '#type' => 'details',
        '#open' => $pet
          ->getCc() && $pet
          ->getBcc(),
      ];
      $form['copies']['cc'] = [
        '#title' => $this
          ->t('Cc'),
        '#type' => 'textfield',
        '#rows' => 3,
        '#default_value' => isset($storage['cc']) ? $storage['cc'] : $pet
          ->getCc(),
        '#description' => $this
          ->t('Enter any Cc recipients comma separated.'),
      ];
      $form['copies']['bcc'] = [
        '#title' => $this
          ->t('Bcc'),
        '#type' => 'textfield',
        '#rows' => 3,
        '#default_value' => isset($storage['bcc']) ? $storage['bcc'] : $pet
          ->getBcc(),
        '#description' => $this
          ->t('Enter any Bcc recipients comma separated.'),
      ];
      $form['subject'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Subject'),
        '#maxlength' => 255,
        '#default_value' => isset($storage['subject']) ? $storage['subject'] : $pet
          ->getSubject(),
        '#required' => TRUE,
        '#description' => $this
          ->t('The subject line of the email template. May include tokens of any token type specified below.'),
      ];
      $form['body'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Body'),
        '#default_value' => isset($storage['body']) ? $storage['body'] : $pet
          ->getBody(),
        '#rows' => 15,
        '#description' => $this
          ->t('The body of the email template. May include tokens of any token type specified below.'),
      ];
      if (PetHelper::hasMimeMail()) {
        $form['mimemail'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Mime Mail Options'),
          '#open' => TRUE,
        ];
        $form['mimemail']['body_html'] = [
          '#type' => 'text_format',
          '#title' => $this
            ->t('HTML Body'),
          '#default_value' => isset($storage['body_html']['value']) ? $storage['body_html']['value'] : $pet
            ->getBodyHtml(),
          '#rows' => 15,
          '#format' => isset($storage['body_html']['format']) ? $storage['body_html']['format'] : $pet
            ->getFormat(),
          '#description' => $this
            ->t('The plain text body of the email template. May include tokens of any token type specified below. If left empty Mime Mail will create a plain text version of the email.'),
        ];
        $form['mimemail']['send_plain'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Send only plain text'),
          '#default_value' => isset($storage['send_plain']) ? $storage['send_plain'] : $pet
            ->getSendPlain(),
          '#description' => $this
            ->t('Send email as plain text only. If checked, only the plain text here will be sent. If unchecked both will be sent as multipart mime.'),
        ];
      }
      $form['tokens'] = pet_token_help();
      $form['note'] = [
        '#markup' => '<p>' . $this
          ->t('Review and edit the template before previewing. This will not change the template for future emails, just for this preview.') . '</p>',
      ];
      $form['preview'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Preview'),
      ];
      break;
    case 2:
      $form['recipients'] = [
        '#title' => $this
          ->t('To'),
        '#type' => 'hidden',
        '#required' => TRUE,
        '#default_value' => $storage['recipients'],
        '#disabled' => $has_recipient_callback,
      ];
      $form['recipients_display'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('To'),
        '#rows' => 4,
        '#value' => $this
          ->formatRecipients($storage['recipients_array']),
        '#disabled' => TRUE,
      ];
      if ($storage['reply_to']) {
        $form['reply_to'] = [
          '#title' => $this
            ->t('Reply-To'),
          '#type' => 'email',
          '#value' => $storage['reply_to'],
          '#disabled' => TRUE,
        ];
      }
      if ($storage['cc']) {
        $form['cc'] = [
          '#type' => 'textarea',
          '#title' => $this
            ->t('Cc'),
          '#rows' => 4,
          '#value' => $storage['cc'],
          '#disabled' => TRUE,
        ];
      }
      if ($storage['bcc']) {
        $form['bcc'] = [
          '#type' => 'textarea',
          '#title' => $this
            ->t('Bcc'),
          '#rows' => 4,
          '#value' => $storage['bcc'],
          '#disabled' => TRUE,
        ];
      }
      $form['subject'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Subject'),
        '#size' => 80,
        '#value' => $storage['subject_preview'],
        '#disabled' => TRUE,
      ];
      if (PetHelper::hasMimeMail()) {
        $form['body_html'] = [
          '#type' => 'textarea',
          '#title' => $this
            ->t('Raw HTML body'),
          '#rows' => 15,
          '#value' => $storage['body_html']['value'],
          '#disabled' => TRUE,
        ];
        if (isset($storage['send_plain']) && !$storage['send_plain']) {
          $form['body_preview_html'] = [
            '#prefix' => '<div class="form-item form-disabled form-type-pet">',
            '#suffix' => '</div>',
            '#type' => 'inline_template',
            '#template' => '<label>{% trans %}Processed HTML Body{% endtrans %}</label><div class="preview">{{ preview|raw }}</div>',
            '#context' => [
              'preview' => $storage['body_preview_html'],
            ],
          ];
          $form['#attached'] = [
            'library' => [
              'pet/pet.preview',
            ],
          ];
        }
      }
      $form['body'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Body'),
        '#rows' => 15,
        '#value' => $storage['body_preview'],
        '#disabled' => TRUE,
      ];
      $form['back'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Back'),
        '#submit' => [
          '::stepBack',
        ],
      ];
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Send email'),
      ];
      break;
  }
  $form_state
    ->setStorage($storage);
  return $form;
}