You are here

public function UserKeyAuthForm::buildForm in Key auth 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/UserKeyAuthForm.php, line 69

Class

UserKeyAuthForm
Class UserKeyAuthForm.

Namespace

Drupal\key_auth\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {

  // Extract the user's key.
  $key = $user->api_key->value;

  // Store the user ID.
  $form['#uid'] = $user
    ->id();
  $form['key'] = [
    'label' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $this
        ->t('Key'),
    ],
    'key' => [
      '#type' => 'item',
      '#markup' => $key ? $key : $this
        ->t('You currently do not have a key'),
    ],
  ];
  $form['auth'] = [
    'label' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $this
        ->t('Authentication options'),
    ],
    '#access' => (bool) $key,
  ];

  // Check if header detection is enabled.
  if (in_array(KeyAuth::DETECTION_METHOD_HEADER, $this->config
    ->get('detection_methods'))) {
    $form['auth']['header'] = [
      'label' => [
        '#type' => 'html_tag',
        '#tag' => 'h5',
        '#value' => $this
          ->t('Header'),
      ],
      'instructions' => [
        '#type' => 'item',
        '#markup' => $this
          ->t('Include the following header in your API requests.'),
      ],
      'example' => [
        '#type' => 'html_tag',
        '#tag' => 'pre',
        '#value' => $this->config
          ->get('param_name') . ': ' . $key,
      ],
    ];
  }

  // Check if query detection is enabled.
  if (in_array(KeyAuth::DETECTION_METHOD_QUERY, $this->config
    ->get('detection_methods'))) {
    $form['auth']['query'] = [
      'label' => [
        '#type' => 'html_tag',
        '#tag' => 'h5',
        '#value' => $this
          ->t('Query'),
      ],
      'instructions' => [
        '#type' => 'item',
        '#markup' => $this
          ->t('Include the following query in the URL of your API requests.'),
      ],
      'example' => [
        '#type' => 'html_tag',
        '#tag' => 'pre',
        '#value' => '?' . $this->config
          ->get('param_name') . '=' . $key,
      ],
    ];
  }
  $form['actions'] = [
    'new' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Generate new key'),
    ],
    'delete' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete current key'),
      '#access' => (bool) $key,
      '#submit' => [
        '::deleteKey',
      ],
    ],
  ];
  return $form;
}