You are here

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

Same name and namespace in other branches
  1. 8.2 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 19

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', Crypt::randomBytes(16)), 0, 32);
  $form['label'] = array(
    '#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'] = array(
    '#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'] = array(
    '#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'] = array(
    '#type' => 'machine_name',
    '#default_value' => $api_key
      ->id(),
    '#machine_name' => array(
      '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;
}