You are here

public function PetPreviewForm::buildForm in Previewable email templates 8

Same name and namespace in other branches
  1. 8.4 src/Form/PetPreviewForm.php \Drupal\pet\Form\PetPreviewForm::buildForm()
  2. 8.3 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 22

Class

PetPreviewForm

Namespace

Drupal\pet\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, PetInterface $pet = NULL) {
  $messenger = \Drupal::messenger();
  $body_description = $this
    ->t('Review and edit standard template before previewing. This will not change the template for future emailings, just for this one. To change the template permanently, go to the template page. You may use the tokens below.');
  $storage = $form_state
    ->getStorage();
  if (pet_isset_or($storage['step']) == 3) {
    $messenger
      ->addMessage($this
      ->t('Email(s) sent'));
    $form_state
      ->setStorage([]);
  }
  $step = empty($storage['step']) ? 1 : $storage['step'];
  $storage['step'] = $step;
  $storage['pet'] = $pet;

  // Get any query args
  $nid = $storage['nid'] = pet_is_natural(pet_isset_or($_REQUEST['nid'])) ? $_REQUEST['nid'] : NULL;
  $uid = $storage['uid'] = pet_is_natural(pet_isset_or($_REQUEST['uid'])) ? $_REQUEST['uid'] : NULL;
  $recipient_callback = $storage['recipient_callback'] = pet_isset_or($_REQUEST['recipient_callback']) === 'true' || pet_isset_or($_REQUEST['uid']) === '0';
  switch ($step) {
    case 1:
      if ($recipient_callback) {
        $default_mail = t('Recipient list will be generated for preview.');
      }
      elseif (pet_isset_or($storage['recipients_raw'])) {
        $default_mail = $storage['recipients_raw'];
      }
      else {
        $default_mail = '';
        if ($uid) {
          if ($account = User::load($uid)) {
            $default_mail = $account
              ->getEmail();
          }
          else {
            $messenger
              ->addMessage($this
              ->t('Cannot load a user with uid @uid.', [
              '@uid' => $uid,
            ]), 'error');
          }
        }
      }
      $form['recipients'] = [
        '#title' => t('To'),
        '#type' => 'email',
        // #2931344 - enable this when core allows multiple values in email field.
        // '#attributes' => ['multiple' => 'multiple'],
        // '#element_validate' => [],
        '#required' => TRUE,
        '#default_value' => $default_mail,
        '#description' => t('Enter the recipient(s) separated by lines or commas. A separate email will be sent to each, with token substitution if the email corresponds to a site user.'),
        '#disabled' => $recipient_callback,
      ];
      $form['copies'] = [
        '#title' => t('Copies'),
        '#type' => 'details',
        '#collapsed' => empty($pet
          ->getCCDefault()) && empty($pet
          ->getBCCDefault()),
      ];
      $form['copies']['cc'] = [
        '#title' => t('Cc'),
        '#type' => 'email',
        '#rows' => 3,
        '#default_value' => pet_isset_or($storage['cc']) ? $storage['cc'] : $pet
          ->getCCDefault(),
        '#description' => t('Enter any copied emails separated by lines or commas.'),
      ];
      $form['copies']['bcc'] = [
        '#title' => t('Bcc'),
        '#type' => 'email',
        '#rows' => 3,
        '#default_value' => pet_isset_or($storage['bcc']) ? $storage['bcc'] : $pet
          ->getBCCDefault(),
        '#description' => t('Enter any blind copied emails separated by lines or commas.'),
      ];
      $form['subject'] = [
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#maxlength' => 255,
        '#default_value' => isset($storage['subject']) ? $storage['subject'] : $pet
          ->getSubject(),
        '#required' => TRUE,
      ];
      if (!(pet_has_mimemail() && $pet
        ->getSendPlain())) {
        $form['mail_body'] = [
          '#type' => 'text_format',
          '#title' => t('Body'),
          '#default_value' => pet_isset_or($storage['mail_body']) ? $storage['mail_body'] : $pet
            ->getMailbody(),
          '#rows' => 15,
          '#description' => $body_description,
        ];
      }
      if (pet_has_mimemail()) {
        $form['mimemail'] = [
          '#type' => 'details',
          '#title' => t('Plain text body'),
          '#collapsible' => TRUE,
          '#collapsed' => !(pet_has_mimemail() && $pet->send_plain),
        ];
        $form['mimemail']['mail_body_plain'] = [
          '#type' => 'textarea',
          '#title' => t('Plain text body'),
          '#default_value' => isset($storage['mail_body_plain']) ? $storage['mail_body_plain'] : $pet
            ->getMailbodyPlain(),
          '#rows' => 15,
          '#description' => $body_description,
        ];
      }
      $form['tokens'] = pet_token_help();
      $form['preview'] = [
        '#type' => 'submit',
        '#value' => t('Preview'),
      ];
      break;
    case 2:
      $values = $form_state
        ->getValues();
      $form['info'] = [
        '#value' => t('A preview of the email is shown below. If you\'re satisfied, click Send. If not, click Back to edit the email.'),
      ];
      $form['recipients'] = [
        '#title' => t('To'),
        '#type' => 'hidden',
        '#required' => TRUE,
        '#default_value' => $storage['recipients'],
        '#description' => t('Enter the recipient(s) separated by lines or commas. A separate email will be sent to each, with token substitution if the email corresponds to a site user.'),
        '#disabled' => $recipient_callback,
      ];
      $form['recipients_display'] = [
        '#type' => 'textarea',
        '#title' => t('To'),
        '#rows' => 4,
        '#value' => pet_recipients_formatted($storage['recipients']),
      ];
      if ($values['cc']) {
        $form['cc'] = [
          '#type' => 'textarea',
          '#title' => t('CC'),
          '#rows' => 4,
          '#value' => $values['cc'],
          '#disabled' => TRUE,
        ];
      }
      if ($values['bcc']) {
        $form['bcc'] = [
          '#type' => 'textarea',
          '#title' => t('BCC'),
          '#rows' => 4,
          '#value' => $values['bcc'],
          '#disabled' => TRUE,
        ];
      }
      $form['subject'] = [
        '#type' => 'textfield',
        '#title' => t('Subject'),
        '#size' => 80,
        '#value' => $storage['subject_preview'],
        '#disabled' => TRUE,
      ];
      if (!pet_has_mimemail() || !$pet
        ->getSendPlain()) {
        $form['body_label'] = [
          '#prefix' => '<div class="pet_body_label">',
          '#suffix' => '</div>',
          '#markup' => '<label>' . t('Body as HTML') . '</label>',
        ];
        $form['body_preview'] = [
          '#prefix' => '<div class="pet_body_preview">',
          '#suffix' => '</div>',
          '#markup' => $storage['body_preview'],
        ];
        $form['mail_body'] = [
          '#type' => 'textarea',
          '#title' => t('Body'),
          '#rows' => 15,
          '#value' => $storage['body_preview'],
          '#disabled' => TRUE,
        ];
      }
      $plain_text = trim($storage['body_preview_plain']);
      if (pet_has_mimemail() && ($pet
        ->getSendPlain() || !empty($plain_text))) {
        $form['mail_body_plain'] = [
          '#type' => 'textarea',
          '#title' => t('Plain text body'),
          '#rows' => 15,
          '#value' => $storage['body_preview_plain'],
          '#disabled' => TRUE,
        ];
      }
      $form['back'] = [
        '#type' => 'submit',
        '#value' => t('Back'),
        '#submit' => [
          '::stepBack',
        ],
      ];
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => t('Send email(s)'),
      ];
      break;
  }
  $form_state
    ->setStorage($storage);
  return $form;
}