You are here

public function MassContactForm::submitForm in Mass Contact 8

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

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 ContentEntityForm::submitForm

File

src/Form/MassContactForm.php, line 403

Class

MassContactForm
Main form for sending Mass Contact emails.

Namespace

Drupal\mass_contact\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $configuration = [
    'use_bcc' => $form_state
      ->getValue('use_bcc'),
    'sender_name' => $form_state
      ->getValue('sender_name'),
    'sender_mail' => $form_state
      ->getValue('sender_mail'),
    'create_archive_copy' => $form_state
      ->getValue('create_archive_copy'),
    'respect_opt_out' => $form_state
      ->getValue('optout'),
  ];

  // Add the sender's email to the configs, if the 'Send yourself a copy'
  // option has been chosen.
  if ($form_state
    ->getValue('copy')) {
    $configuration['send_me_copy_user'] = $this
      ->currentUser()
      ->id();
  }
  else {
    $configuration['send_me_copy_user'] = FALSE;
  }
  $message = $this->entityTypeManager
    ->getStorage('mass_contact_message')
    ->create([
    'subject' => $form_state
      ->getValue('subject'),
    'body' => $form_state
      ->getValue('body'),
  ]);
  $categories = [];
  $field_categories_value = array_filter($form_state
    ->getValue('categories'));
  if (!empty($field_categories_value)) {
    foreach ($field_categories_value as $id) {
      $categories[] = [
        'target_id' => $id,
      ];
    }
    $message->categories = $categories;
  }

  // Store data needed for the confirmation form in the user's private temp
  // storage.
  $store = \Drupal::service("user.private_tempstore")
    ->get('mass_contact_confirm_info');
  $store
    ->set($message
    ->uuid(), [
    'mass_contact_message' => $message,
    'configuration' => $configuration,
  ]);

  // Redirect to the confirmation form.
  $form_state
    ->setRedirect('entity.mass_contact.confirm_before_send', [
    'mass_contact_confirm_info' => $message
      ->uuid(),
  ]);
}