You are here

protected function ForwardForm::getToken in Forward 4.x

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

Get a token.

2 calls to ForwardForm::getToken()
ForwardForm::buildForm in src/Form/ForwardForm.php
Form constructor.
ForwardForm::submitForm in src/Form/ForwardForm.php
Form submission handler.

File

src/Form/ForwardForm.php, line 638

Class

ForwardForm
Forward a page to a friend.

Namespace

Drupal\forward\Form

Code

protected function getToken(FormStateInterface $form_state = NULL) {
  $token = [];
  if ($form_state && $form_state
    ->getValue('name')) {

    // Harden the name field against abuse. @see https://www.drupal.org/node/2793891
    $token = [
      'forward' => [
        'sender_name' => $this
          ->cleanString($form_state
          ->getValue('name')),
      ],
    ];
  }
  elseif ($this
    ->currentUser()
    ->isAuthenticated()) {
    $token = [
      'forward' => [
        'sender_name' => $this
          ->currentUser()
          ->getDisplayName(),
      ],
    ];
  }
  if ($form_state && $form_state
    ->getValue('email')) {
    $token['forward']['sender_email'] = $form_state
      ->getValue('email');
  }
  elseif ($this
    ->currentUser()
    ->isAuthenticated()) {
    $token['forward']['sender_email'] = $this
      ->currentUser()
      ->getEmail();
  }
  if ($form_state) {
    $token['forward']['entity'] = $form_state
      ->get('#entity');
  }
  if ($form_state && $form_state
    ->getValue('recipient')) {
    $token['forward']['recipients'] = $this
      ->splitEmailAddresses($form_state
      ->getValue('recipient'));
  }

  // Allow other modules to add more tokens.
  if ($extra_tokens = $this->moduleHandler
    ->invokeAll('forward_token', [
    $form_state,
  ])) {
    $token += $extra_tokens;
  }
  return $token;
}