You are here

public function MassContactMessageConfirmForm::submitForm in Mass Contact 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/MassContactMessageConfirmForm.php, line 233

Class

MassContactMessageConfirmForm
Form object for the Mass Contact Confirm form.

Namespace

Drupal\mass_contact\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Process via cron if configured.
  if ($this->config
    ->get('send_with_cron')) {

    // Utilize cron/job queue system.
    $this->massContact
      ->processMassContactMessage($this->massContactMessage, $this->messageConfigs);
  }
  else {

    // Process immediately via the batch system.
    $all_recipients = $this->massContact
      ->getRecipients($this->massContactMessage
      ->getCategories(), $this->messageConfigs['respect_opt_out']);

    // Add the sender's email to the recipient list if 'Send yourself a copy'
    // option has been chosen AND the email is not already in the recipient
    // list.
    // Add this user as the first user in the list. If the user exists in the
    // recipient list, remove the user and add the user again as first in the
    // list.
    if ($this->messageConfigs['send_me_copy_user']) {
      if (!empty($all_recipients)) {
        $send_me_copy_user_key = array_search($this->messageConfigs['send_me_copy_user'], $all_recipients);
        if ($send_me_copy_user_key) {
          unset($all_recipients[$send_me_copy_user_key]);
        }
      }
      $all_recipients = [
        $this
          ->currentUser()
          ->id() => $this
          ->currentUser()
          ->id(),
      ] + $all_recipients;
    }
    $batch = [
      'title' => $this
        ->t('Sending message'),
      'operations' => [],
    ];
    foreach ($this->massContact
      ->getGroupedRecipients($all_recipients) as $recipients) {
      $data = [
        'recipients' => $recipients,
        'message' => $this->massContactMessage,
        'configuration' => $this->messageConfigs,
      ];
      $batch['operations'][] = [
        [
          static::class,
          'processRecipients',
        ],
        $data,
      ];
    }
    batch_set($batch);
  }

  // Create a copy of a message if requested.
  if ($this->messageConfigs['create_archive_copy']) {
    $this->massContactMessage
      ->save();
    if ($this->massContactMessage
      ->id()) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Mass Contact message sent successfully. A copy has been archived <a href="@url">here</a>.', [
        '@url' => $this->massContactMessage
          ->toUrl()
          ->toString(),
      ]));
    }
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Mass Contact message sent successfully.'));
  }

  // Delete the entry from the user's tempstore.
  $store = $this->tempStoreFactory
    ->get('mass_contact_confirm_info');
  if (!empty($store)) {
    $store
      ->delete($this->massContactMessage
      ->uuid());
  }

  // Redirect to the add mass contact message form.
  $form_state
    ->setRedirect('entity.mass_contact_message.add_form');
}