You are here

public function ForwardForm::submitForm in Forward 8

Same name and namespace in other branches
  1. 8.3 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::submitForm()
  2. 8.2 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::submitForm()
  3. 4.x src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::submitForm()
  4. 4.0.x src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::submitForm()

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/ForwardForm.php, line 437

Class

ForwardForm
Forward a page to a friend

Namespace

Drupal\forward\Form

Code

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

  // Get form values
  $entity = $form_state
    ->get('#entity');
  $recipient = $form_state
    ->getValue('recipient');

  // Use the entity language to drive translation
  $langcode = $entity
    ->language()
    ->getId();

  // Switch to anonymous user session if logged in, unless bypassing access control
  $switched = FALSE;
  if ($this
    ->currentUser()
    ->isAuthenticated() && empty($this->settings['forward_bypass_access_control'])) {
    $this->accountSwitcher
      ->switchTo(new AnonymousUserSession());
    $switched = TRUE;
  }
  try {

    // Build the message subject line
    $token = $this
      ->getToken($form_state);
    $params['subject'] = $this->tokenService
      ->replace($this->settings['forward_email_subject'], $token, array(
      'langcode' => $langcode,
    ));

    // Build the entity content
    $view_mode = '';
    $elements = array();
    if ($entity
      ->access('view')) {
      $view_builder = $this->entityTypeManager
        ->getViewBuilder($entity
        ->getEntityTypeId());
      $view_mode = 'forward';
      if ($this
        ->isValidDisplay($entity, $view_mode)) {
        $elements = $view_builder
          ->view($entity, $view_mode, $langcode);
      }
      if (empty($elements)) {
        $view_mode = 'teaser';
        if ($this
          ->isValidDisplay($entity, $view_mode)) {
          $elements = $view_builder
            ->view($entity, $view_mode, $langcode);
        }
      }
      if (empty($elements)) {
        $view_mode = 'full';
        $elements = $view_builder
          ->view($entity, $view_mode, $langcode);
      }
    }
    $elements['#forward_build'] = TRUE;

    // prevent recursion
    $content = $this->renderer
      ->render($elements);

    // Build the header line
    $header = [
      '#markup' => $this->tokenService
        ->replace($this->settings['forward_email_message'], $token, array(
        'langcode' => $langcode,
      )),
    ];

    // Build the personal message if present
    $message = '';
    if ($this->settings['forward_personal_message']) {
      if ($this->settings['forward_personal_message_filter']) {

        // HTML allowed in personal message, so filter out anything but the allowed tags
        $raw_values = $form_state
          ->getUserInput();
        $allowed_tags = explode(',', $this->settings['forward_personal_message_tags']);
        $message = !empty($raw_values['message']) ? Xss::filter($raw_values['message'], $allowed_tags) : '';
        $message = [
          '#markup' => nl2br($message),
        ];
      }
      else {

        // HTML not allowed in personal message, so use the sanitized version converted to plain text
        $message = [
          '#plain_text' => nl2br($form_state
            ->getValue('message')),
        ];
      }
    }

    // Build the email body
    $render_array = array(
      '#theme' => 'forward',
      '#email' => $form_state
        ->getValue('email'),
      '#header' => $header,
      '#message' => $message,
      '#settings' => $this->settings,
      '#entity' => $entity,
      '#content' => $content,
      '#view_mode' => $view_mode,
    );

    // Allow modules to alter the render array for the message
    $this->moduleHandler
      ->alter('forward_mail_pre_render', $render_array, $form_state);

    // Render the message
    $params['body'] = $this->renderer
      ->render($render_array);

    // Apply filters such as Pathologic for link correction
    if ($this->settings['forward_filter_format']) {

      // This filter was setup by the Forward administrator for this purpose only, whose permission to run the filter was checked at that time
      // Therefore, no need to check filter access again here
      $params['body'] = check_markup($params['body'], $this->settings['forward_filter_format'], $langcode);
    }

    // Allow modules to alter the final message body
    $this->moduleHandler
      ->alter('forward_mail_post_render', $params['body'], $form_state);
  } catch (Exception $e) {
    if ($switched) {
      $this->accountSwitcher
        ->switchBack();
      $switched = FALSE;
    }
    $this
      ->logger('forward')
      ->error($e
      ->getMessage());
  }

  // Switch back to logged in user if necessary
  if ($switched) {
    $this->accountSwitcher
      ->switchBack();
  }

  // Build the from email address and Reply-To
  $from = $this->settings['forward_email_from_address'];
  if (empty($from)) {
    $from = $this
      ->config('system.site')
      ->get('mail');
  }
  if (empty($from)) {
    $site_mail = ini_get('sendmail_from');
  }
  $params['headers']['Reply-To'] = trim(Unicode::mimeHeaderEncode($form_state
    ->getValue('name')) . ' <' . $form_state
    ->getValue('email') . '>');

  // Prepare for Event dispatch
  $account = $this->entityTypeManager
    ->getStorage('user')
    ->load($this
    ->currentUser()
    ->id());

  // Event dispatch - before forwarding
  $event = new EntityPreforwardEvent($account, $entity, [
    'account' => $account,
    'entity' => $entity,
  ]);
  $this->eventDispatcher
    ->dispatch(EntityPreforwardEvent::EVENT_NAME, $event);

  // Send the email to the recipient
  $key = 'send_entity';
  $this->mailer
    ->mail('forward', $key, $recipient, $langcode, $params, $from);

  // Log this for tracking purposes
  $this
    ->logEvent($entity);

  // Register event for flood control
  $event = $this
    ->getFloodControlEventName();
  $this->floodInterface
    ->register($event);

  // Event dispatch - after forwarding
  $event = new EntityForwardEvent($account, $entity, [
    'account' => $account,
    'entity' => $entity,
  ]);
  $this->eventDispatcher
    ->dispatch(EntityForwardEvent::EVENT_NAME, $event);

  // Allow modules to post process the forward
  $this->moduleHandler
    ->invokeAll('forward_entity', array(
    $account,
    $entity,
    $form_state,
  ));

  // Display a confirmation message
  $message = $this->tokenService
    ->replace($this->settings['forward_form_confirmation'], $token, array(
    'langcode' => $langcode,
  ));
  if ($message) {
    drupal_set_message($message);
  }

  // Redirect back to entity page unless a redirect is already set
  if ($this->settings['forward_interface_type'] == 'link') {
    if (!$form_state
      ->getRedirect()) {
      $form_state
        ->setRedirectUrl($entity
        ->toUrl());
    }
  }
}