You are here

public function ApiKeyForm::form in Services API Key Authentication 8.2

Same name and namespace in other branches
  1. 8 src/Form/ApiKeyForm.php \Drupal\services_api_key_auth\Form\ApiKeyForm::form()
  2. 3.0.x src/Form/ApiKeyForm.php \Drupal\services_api_key_auth\Form\ApiKeyForm::form()
  3. 2.0.x src/Form/ApiKeyForm.php \Drupal\services_api_key_auth\Form\ApiKeyForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ApiKeyForm.php, line 18

Class

ApiKeyForm
Class ApiKeyForm.

Namespace

Drupal\services_api_key_auth\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $api_key = $this->entity;
  $hex = isset($api_key->key) ? $api_key->key : substr(hash('sha256', random_bytes(16)), 0, 32);
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Machine Name'),
    '#maxlength' => 255,
    '#default_value' => $api_key
      ->label(),
    '#description' => $this
      ->t("Machine Name for the API Key."),
    '#required' => TRUE,
  ];
  $form['key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('API Key'),
    '#maxlength' => 42,
    '#default_value' => $hex,
    '#description' => $this
      ->t("The generated API Key for an user."),
    '#required' => TRUE,
  ];
  $form['user_uuid'] = [
    '#type' => 'select',
    '#multiple' => FALSE,
    '#options' => self::getUser(),
    '#description' => $this
      ->t("Please select the user who gets authenticated with that API Key."),
    '#default_value' => $api_key->user_uuid,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $api_key
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\services_api_key_auth\\Entity\\ApiKey::load',
    ],
    '#disabled' => !$api_key
      ->isNew(),
  ];

  /* You will need additional form elements for your custom properties. */
  return $form;
}