You are here

public function EasyEmailForm::buildForm in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EasyEmailForm.php \Drupal\easy_email\Form\EasyEmailForm::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 EntityForm::buildForm

File

src/Form/EasyEmailForm.php, line 69

Class

EasyEmailForm
Form controller for Email edit forms.

Namespace

Drupal\easy_email\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /* @var $entity \Drupal\easy_email\Entity\EasyEmail */
  $form = parent::buildForm($form, $form_state);

  // We have a custom form for the core properties of this entity, so hide the
  // standard field widgets.
  $properties_to_hide = [
    'key',
    'recipient_address',
    'cc_address',
    'bcc_address',
    'from_name',
    'from_address',
    'reply_to',
    'subject',
    'body_html',
    'body_plain',
    'inbox_preview',
    'sent',
    'attachment_path',
  ];
  foreach ($properties_to_hide as $field_name) {
    if (!empty($form[$field_name])) {
      $form[$field_name]['#access'] = FALSE;
    }
  }
  $recipient_reference_fields = [
    'recipient_uid',
    'cc_uid',
    'bcc_uid',
  ];
  $form['recipients'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Recipients'),
    '#weight' => 50,
  ];
  foreach ($recipient_reference_fields as $field_name) {
    if (!empty($form[$field_name])) {
      $form['recipients'][$field_name] = $form[$field_name];
      unset($form[$field_name]);
    }
  }
  $form['authoring_information'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Authoring Information'),
    '#weight' => 90,
  ];
  $form['authoring_information']['creator_uid'] = $form['creator_uid'];
  unset($form['creator_uid']);
  $form['authoring_information']['revision_log_message'] = $form['revision_log_message'];
  unset($form['revision_log_message']);
  if (!$this->entity
    ->isNew()) {
    $form['authoring_information']['new_revision'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Create new revision'),
      '#default_value' => FALSE,
      '#weight' => 10,
    ];
  }
  $entity = $this->entity;
  $form['customize'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Customize Email'),
    '#weight' => 75,
    '#open' => empty($entity
      ->getRecipientAddresses()),
  ];
  $form['customize']['to'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Recipients'),
  ];
  $form['customize']['to']['recipient'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('To'),
    '#maxlength' => 1024,
    '#default_value' => !empty($entity
      ->getRecipientAddresses()) ? implode(', ', $entity
      ->getRecipientAddresses()) : NULL,
  ];
  $form['customize']['to']['cc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('CC'),
    '#maxlength' => 1024,
    '#default_value' => !empty($entity
      ->getCCAddresses()) ? implode(', ', $entity
      ->getCCAddresses()) : NULL,
  ];
  $form['customize']['to']['bcc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('BCC'),
    '#maxlength' => 1024,
    '#default_value' => !empty($entity
      ->getBCCAddresses()) ? implode(', ', $entity
      ->getBCCAddresses()) : NULL,
  ];
  $form['customize']['sender'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Sender'),
  ];
  $form['customize']['sender']['fromName'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('From Name'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->getFromName(),
  ];
  $form['customize']['sender']['fromAddress'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('From Address'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->getFromAddress(),
  ];
  $form['customize']['sender']['replyToAddress'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Reply To Address'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->getReplyToAddress(),
  ];
  $form['customize']['email_content'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Content'),
  ];
  $form['customize']['email_content']['subjectText'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->getSubject(),
  ];
  $form['customize']['email_content']['body'] = [
    '#type' => 'vertical_tabs',
    '#default_tab' => 'edit-body-html',
  ];
  $form['customize']['body_html'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('HTML Body'),
    '#group' => 'body',
    '#weight' => 0,
  ];
  $body_html = $entity
    ->getHtmlBody();
  $form['customize']['body_html']['bodyHtml'] = [
    '#type' => 'text_format',
    '#rows' => 30,
    '#title' => $this
      ->t('HTML Body'),
    '#default_value' => !empty($body_html) ? $body_html['value'] : NULL,
    '#format' => !empty($body_html) ? $body_html['format'] : NULL,
  ];
  $form['customize']['body_plain'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Plain Text Body'),
    '#group' => 'body',
    '#weight' => 5,
  ];
  $form['customize']['body_plain']['bodyPlain'] = [
    '#type' => 'textarea',
    '#rows' => 30,
    '#title' => $this
      ->t('Plain Text Body'),
    '#default_value' => $entity
      ->getPlainBody(),
  ];
  $form['customize']['body_inbox'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Inbox Preview'),
    '#group' => 'body',
    '#weight' => 10,
  ];
  $form['customize']['body_inbox']['inboxPreview'] = [
    '#type' => 'textarea',
    '#rows' => 5,
    '#title' => $this
      ->t('Inbox Preview'),
    '#default_value' => $entity
      ->getInboxPreview(),
  ];
  $form['customize']['content']['attachment_paths'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Attachments'),
    '#maxlength' => 1024,
    '#default_value' => !empty($entity
      ->getAttachmentPaths()) ? implode(', ', $entity
      ->getAttachmentPaths()) : NULL,
  ];
  $form['customize']['email_content']['attachment'] = $form['attachment'];
  unset($form['attachment']);
  $form['customize']['email_content']['attachment']['widget']['#title'] = $this
    ->t('Upload Attachments');
  $form['customize']['tokens'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Replacement Patterns'),
  ];
  $form['customize']['tokens']['tokens'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [
      'easy_email',
    ],
    '#show_restricted' => TRUE,
    '#recursion_limit' => 6,
  ];
  if (!$entity
    ->isSent()) {
    $form['send'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Send Email on Save'),
      '#default_value' => $entity
        ->isNew() ? TRUE : FALSE,
      '#weight' => 95,
    ];
  }
  if ($this
    ->isEntityTypePreview()) {
    $form['send']['#access'] = FALSE;
    $form['send']['#default_value'] = FALSE;
    $form['customize']['#access'] = FALSE;
    $form['authoring_information']['#access'] = FALSE;
    $store = $this->tempStoreFactory
      ->get('easy_email_type_preview');
    if ($preview = $store
      ->get($this->entity
      ->uuid())) {
      $form['preview'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Preview'),
        '#weight' => 100,
        '#attributes' => [
          'class' => [
            'easy-email-type-preview',
          ],
        ],
        '#attached' => [
          'library' => [
            'easy_email/preview',
          ],
        ],
      ];
      $form['preview']['easy_email'] = [
        '#theme' => 'easy_email_type_preview',
        '#easy_email' => $preview
          ->getFormObject()
          ->getEntity(),
      ];
    }
  }
  return $form;
}