You are here

public function MassContactForm::form in Mass Contact 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/MassContactForm.php, line 96

Class

MassContactForm
Main form for sending Mass Contact emails.

Namespace

Drupal\mass_contact\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $categories = [];
  $default_category = [];
  $default_category_name = '';

  /** @var \Drupal\mass_contact\Entity\MassContactCategoryInterface $category */
  foreach ($this->entityTypeManager
    ->getStorage('mass_contact_category')
    ->loadMultiple() as $category) {
    if ($category
      ->access('view')) {
      $categories[$category
        ->id()] = $category
        ->label();
      if ($category
        ->getSelected()) {
        $default_category[] = $category
          ->id();
        $default_category_name = $category
          ->label();
      }
    }
  }
  $form['contact_information'] = [
    '#markup' => Xss::filterAdmin($this->config
      ->get('form_information')),
  ];

  // Add the field for specifying the sender's name.
  $default_sender_name = $this->config
    ->get('default_sender_name');
  $sender_name_title = $this
    ->t('Sender name');
  if ($default_sender_name) {
    if ($this
      ->currentUser()
      ->hasPermission('mass contact change default sender information')) {
      $form['sender_name'] = [
        '#type' => 'textfield',
        '#title' => $sender_name_title,
        '#maxlength' => 255,
        '#default_value' => $default_sender_name,
        '#required' => TRUE,
      ];
    }
    else {
      $form['sender_name'] = [
        '#type' => 'item',
        '#title' => $sender_name_title,
        '#value' => $default_sender_name,
      ];
    }
  }
  else {
    $form['sender_name'] = [
      '#type' => 'textfield',
      '#title' => $sender_name_title,
      '#maxlength' => 255,
      '#default_value' => $this
        ->currentUser()
        ->getDisplayName(),
      '#required' => TRUE,
    ];
  }

  // Add the field for specifying the sender's email address.
  $default_sender_email = $this->config
    ->get('default_sender_email');
  $sender_name_title = $this
    ->t('Sender email');
  if ($default_sender_email) {
    if ($this
      ->currentUser()
      ->hasPermission('mass contact change default sender information')) {
      $form['sender_mail'] = [
        '#type' => 'email',
        '#title' => $sender_name_title,
        '#default_value' => $default_sender_email,
        '#required' => TRUE,
      ];
    }
    else {
      $form['sender_mail'] = [
        '#type' => 'item',
        '#title' => $sender_name_title,
        '#value' => $default_sender_email,
      ];
    }
  }
  else {
    $form['sender_mail'] = [
      '#type' => 'email',
      '#title' => $sender_name_title,
      '#default_value' => $this
        ->currentUser()
        ->getEmail(),
      '#required' => TRUE,
    ];
  }

  // Add the field for specifying the category(ies).
  // Categories are optional. This means that if there is no category
  // configured, the user can send a copy to him/herself.
  // If there are categories configured and one of them has been configured as
  // 'selected' by default, present a list of categories to choose from,
  // defaulted to the one configured as one.
  if (count($categories) > 0) {

    // Display a choice when one is needed.
    $form['categories'] = [
      '#type' => $this->config
        ->get('category_display'),
      '#title' => $this
        ->t('Category'),
      '#default_value' => $default_category,
      '#options' => $categories,
      '#multiple' => TRUE,
    ];
  }
  else {

    // There is a default category selected and it is only one category
    // configured.
    if ($default_category) {

      // Otherwise, just use the default category.
      $form['categories'] = [
        '#type' => 'value',
        '#value' => $default_category,
      ];
      $form['cid-info'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Category'),
        '#markup' => $this
          ->t('This message will be sent to all users in the %category category.', [
          '%category' => $default_category_name,
        ]),
      ];
    }
    else {

      // There are no categories configured.
      $form['cid-info'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Category'),
        '#markup' => $this
          ->t('No categories have been configured.'),
      ];
    }
  }

  // Add the field for specifying whether opt-outs are respected or not.
  $optout_setting = $this->config
    ->get('optout_enabled');

  // Allow users to opt-out of mass emails:
  // 'disabled' => 'No', 'global' == 'Yes', 'category' == 'Selected
  // categories'.
  if ($optout_setting !== MassContactInterface::OPT_OUT_DISABLED) {

    // @todo https://www.drupal.org/node/2867177
    // Allow to override or respect opt-outs if admin, otherwise use
    // default.
    if ($this
      ->currentUser()
      ->hasPermission('mass contact administer')) {
      $form['optout'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Respect user opt-outs.'),
        '#default_value' => 1,
      ];
    }
    else {
      $form['optout'] = [
        '#type' => 'hidden',
        '#default_value' => 1,
      ];
    }
  }
  else {
    $form['optout'] = [
      '#type' => 'hidden',
      '#default_value' => 0,
    ];
  }

  // Add the field for specifying whether the recipients are in the To or
  // BCC field of the message.
  // Check if the user is allowed to override the BCC setting.
  if ($this
    ->currentUser()
    ->hasPermission('mass contact override bcc')) {
    $form['use_bcc'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Send as BCC (hide recipients).'),
      '#default_value' => $this->config
        ->get('use_bcc'),
    ];
  }
  else {
    $form['use_bcc'] = [
      '#type' => 'value',
      '#value' => $this->config
        ->get('use_bcc'),
    ];
    $form['bcc-info'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Send as BCC (hide recipients)'),
      '#markup' => $this->config
        ->get('use_bcc') ? $this
        ->t('Recipients will be hidden from each other.') : $this
        ->t('Recipients will NOT be hidden from each other.'),
    ];
  }

  // Add the field for specifying the subject of the message.
  $form['subject'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  ];

  // Add the field for specifying the body and text format of the message.
  // Get the HTML input format setting and the corresponding name.
  // Get the admin specified default text format.
  $default_filter_format = $this->config
    ->get('message_format');

  // Check if the user is allowed to override the text format.
  $form['body'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Message'),
    '#format' => $default_filter_format ?: filter_default_format(),
    '#rows' => 12,
    '#required' => TRUE,
  ];
  if (!$this
    ->currentUser()
    ->hasPermission('mass contact override text format')) {

    // The user is not allowed to override the text format, so lock it down
    // to the default one.
    $form['body']['#allowed_formats'] = [
      $default_filter_format ?: filter_default_format(),
    ];
  }
  if (!$this->moduleHandler
    ->moduleExists('mimemail') && !$this->moduleHandler
    ->moduleExists('swiftmailer')) {

    // No HTML email handling, lock down to plain text.
    $form['body']['#allowed_formats'] = [
      'plain_text',
    ];
    $form['body']['#format'] = 'plain_text';
  }

  // If the user has access, add the field for specifying the attachment.
  if (FALSE && ($this->moduleHandler
    ->moduleExists('mimemail') || $this->moduleHandler
    ->moduleExists('swiftmailer'))) {

    // @todo Port email attachments.
    // @see https://www.drupal.org/node/2867544
    if ($this
      ->currentUser()
      ->hasPermission('mass contact include attachments')) {
      for ($i = 1; $i <= $this->config
        ->get('number_of_attachments'); $i++) {
        $form['attachment_' . $i] = [
          '#type' => 'file',
          '#title' => $this
            ->t('Attachment #!number', [
            '!number' => $i,
          ]),
        ];
      }
    }
  }

  // We do not allow anonymous users to send themselves a copy because it
  // can be abused to spam people.
  // @todo Why are anonymous users allowed to hit this form at all?!
  if ($this
    ->currentUser()
    ->id()) {
    $form['copy'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Send yourself a copy.'),
    ];
  }

  // Check if the user is allowed to override the node copy setting.
  if ($this
    ->currentUser()
    ->hasPermission('mass contact override archiving')) {
    $form['create_archive_copy'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Archive a copy of this message on this website.'),
      '#default_value' => $this->config
        ->get('create_archive_copy'),
    ];
  }
  else {
    $form['create_archive_copy'] = [
      '#type' => 'value',
      '#value' => $this->config
        ->get('create_archive_copy'),
    ];
    $form['archive_notice'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Archive a copy of this message on this website'),
      '#markup' => $this->config
        ->get('create_archive_copy') ? $this
        ->t('A copy of this message will be archived on this website.') : $this
        ->t('A copy of this message will NOT be archived on this website.'),
    ];
  }

  // Add the submit button.
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Send email'),
  ];
  $this
    ->buildTaskList($form);
  return $form;
}