You are here

public function ConfigForm::buildForm in JSON Web Token Authentication (JWT) 8

Same name and namespace in other branches
  1. 8.0 src/Form/ConfigForm.php \Drupal\jwt\Form\ConfigForm::buildForm()

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 ConfigFormBase::buildForm

File

src/Form/ConfigForm.php, line 98

Class

ConfigForm
Class ConfigForm.

Namespace

Drupal\jwt\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['key-container'] = [
    '#type' => 'container',
    '#prefix' => '<div id="jwt-key-container">',
    '#suffix' => '</div>',
    '#weight' => 10,
  ];
  $form['jwt_algorithm'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Algorithm'),
    '#options' => $this->transcoder
      ->getAlgorithmOptions(),
    '#ajax' => [
      'callback' => '::ajaxCallback',
      'event' => 'change',
      'wrapper' => 'jwt-key-container',
      'progress' => [
        'type' => 'throbber',
      ],
    ],
    '#default_value' => $this
      ->config('jwt.config')
      ->get('algorithm'),
  ];
  if ($form_state
    ->isValueEmpty('jwt_algorithm')) {
    if (!empty($this
      ->config('jwt.config')
      ->get('algorithm'))) {
      $type = $this->transcoder
        ->getAlgorithmType($this
        ->config('jwt.config')
        ->get('algorithm'));
    }
    else {
      $type = 'jwt_hs';
    }
  }
  else {
    $type = $this->transcoder
      ->getAlgorithmType($form_state
      ->getValue('jwt_algorithm'));
  }
  $text = $type == 'jwt_hs' ? $this
    ->t('Secret') : $this
    ->t('Private Key');
  $form['key-container']['jwt_key'] = [
    '#type' => 'key_select',
    '#title' => $text,
    '#default_value' => $this
      ->config('jwt.config')
      ->get('key_id'),
    '#key_filters' => [
      'type' => $type,
    ],
    '#validated' => TRUE,
    '#required' => TRUE,
  ];
  return parent::buildForm($form, $form_state);
}