You are here

public function EntryForm::buildForm in Two-factor Authentication (TFA) 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EntryForm.php, line 135

Class

EntryForm
TFA entry form.

Namespace

Drupal\tfa\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $user = NULL) {
  $alternate_plugin = $this
    ->getRequest()
    ->get('plugin');
  $validation_plugin_definitions = $this->tfaValidationManager
    ->getDefinitions();
  $user_settings = $this->userData
    ->get('tfa', $user
    ->id(), 'tfa_user_settings');
  $user_enabled_validation_plugins = isset($user_settings['data']['plugins']) ? $user_settings['data']['plugins'] : [];

  // Default validation plugin, then check for enabled alternate plugin.
  $validation_plugin = $this->tfaSettings
    ->get('default_validation_plugin');
  if ($alternate_plugin && !empty($validation_plugin_definitions[$alternate_plugin]) && !empty($user_enabled_validation_plugins[$alternate_plugin])) {
    $validation_plugin = $alternate_plugin;
    $form['#cache'] = [
      'max-age' => 0,
    ];
  }

  // Get current validation plugin form.
  $this->tfaValidationPlugin = $this->tfaValidationManager
    ->createInstance($validation_plugin, [
    'uid' => $user
      ->id(),
  ]);
  $form = $this->tfaValidationPlugin
    ->getForm($form, $form_state);
  $this->tfaLoginPlugins = $this->tfaLoginManager
    ->getPlugins([
    'uid' => $user
      ->id(),
  ]);
  if ($this->tfaLoginPlugins) {
    foreach ($this->tfaLoginPlugins as $login_plugin) {
      if (method_exists($login_plugin, 'getForm')) {
        $form = $login_plugin
          ->getForm($form, $form_state);
      }
    }
  }
  $form['account'] = [
    '#type' => 'value',
    '#value' => $user,
  ];

  // Build a list of links for using other enabled validation methods.
  $other_validation_plugin_links = [];
  foreach ($user_enabled_validation_plugins as $user_enabled_validation_plugin) {

    // Do not show the current plugin.
    if ($validation_plugin == $user_enabled_validation_plugin) {
      continue;
    }

    // Do not show plugins without labels.
    if (empty($validation_plugin_definitions[$user_enabled_validation_plugin]['label'])) {
      continue;
    }
    $other_validation_plugin_links[$user_enabled_validation_plugin] = [
      'title' => $validation_plugin_definitions[$user_enabled_validation_plugin]['label'],
      'url' => Url::fromRoute('tfa.entry', [
        'user' => $user
          ->id(),
        'hash' => $this
          ->getRequest()
          ->get('hash'),
        'plugin' => $user_enabled_validation_plugin,
      ]),
    ];
  }

  // Show other enabled and configured validation plugins.
  $form['validation_plugin'] = [
    '#type' => 'value',
    '#value' => $validation_plugin,
  ];

  // Don't show an empty fieldset when no other tfa methods are available.
  if (!empty($other_validation_plugin_links)) {
    $form['change_validation_plugin'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Having Trouble?'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      'content' => [
        'help' => [
          '#markup' => $this
            ->t('Try one of your other enabled validation methods.'),
        ],
        'other_validation_plugins' => [
          '#theme' => 'links',
          '#links' => $other_validation_plugin_links,
        ],
      ],
    ];
  }
  return $form;
}