You are here

public function ProtectedPagesSendEmailForm::submitForm in Protected Pages 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/ProtectedPagesSendEmailForm.php, line 195

Class

ProtectedPagesSendEmailForm
Provides send protected pages details email form.

Namespace

Drupal\protected_pages\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $fields = [
    'path',
  ];
  $conditions = [];
  $conditions['general'][] = [
    'field' => 'pid',
    'value' => $form_state
      ->get('pid'),
    'operator' => '=',
  ];
  $path = $this->protectedPagesStorage
    ->loadProtectedPage($fields, $conditions, TRUE);
  $module = 'protected_pages';
  $key = 'protected_pages_details_mail';
  $to = $form_state
    ->get('validated_recipents');
  $from = $this->configFactory
    ->getEditable('system.site')
    ->get('mail');
  $language_code = $this->languageManager
    ->getDefaultLanguage()
    ->getId();
  $send = TRUE;
  $params = [];
  $params['subject'] = $form_state
    ->getValue('subject');
  $params['body'] = $form_state
    ->getValue('body');
  $params['protected_page_url'] = Url::fromUri('internal:' . $path, [
    'absolute' => TRUE,
  ])
    ->toString();
  $result = $this->mailManager
    ->mail($module, $key, $to, $language_code, $params, $from, $send);
  if ($result['result'] !== TRUE) {
    $message = $this
      ->t('There was a problem sending your email notification to @email.', [
      '@email' => $to,
    ]);
    $this->loggerFactory
      ->get('protected_pages')
      ->error($message);
  }
  else {
    $message = $this
      ->t('The Email has been sent to @email.', [
      '@email' => $to,
    ]);
    $this->loggerFactory
      ->get('protected_pages')
      ->notice($message);
  }
  $form_state
    ->setRedirect('protected_pages_list');
}