You are here

public function AuthenticationForm::buildForm in Apigee Edge 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 KeyEditForm::buildForm

File

src/Form/AuthenticationForm.php, line 111

Class

AuthenticationForm
Provides a form for saving the Apigee Edge API authentication key.

Namespace

Drupal\apigee_edge\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Do the same trick as Key does in its add form.
  // (Key should provide a "default" form that should be to handle
  // both key add and edit operations just like Node does.
  // Drupal\key\Form\KeyAddForm::buildForm()
  // Only when the form is first built.
  if ($this->entity
    ->isNew() && !$form_state
    ->isRebuilding()) {

    // Set the key value data to NULL, since this is a new key.
    $form_state
      ->set('key_value', [
      'original' => NULL,
      'processed_original' => NULL,
      'obscured' => NULL,
      'current' => '',
    ]);
  }

  // Hide the confirmation step added by Key.
  if (!$this->editConfirmed) {
    $this
      ->confirmEdit($form, $form_state);
    $form_state
      ->setRebuild(FALSE);
  }
  $form = parent::buildForm($form, $form_state);

  // Do not override title which is coming from the route.
  unset($form['#title']);

  // Hide fields that users should not be able to modify in this form.
  $form['label']['#access'] = FALSE;
  $form['id']['#access'] = FALSE;
  $form['description']['#access'] = FALSE;
  $form['settings']['type_section']['#access'] = FALSE;
  $form['settings']['input_section']['#title'] = $this
    ->t('Apigee Edge connection settings');
  $form['settings']['input_section']['#weight'] = 0;
  $form['settings']['provider_section']['#title'] = $this
    ->t('Advanced settings');

  // Provider selection should be closed by default unless the form rebuild
  // trigger by the provider selector or there is an error with the
  // key provider.

  /** @var \Drupal\apigee_edge\Plugin\KeyProviderRequirementsInterface $key_provider */
  $key_provider = $this->entity
    ->getKeyProvider();
  $key_provider_requirements_error = FALSE;

  // Warn user about key provider pre-requirement issues before form
  // submission.
  if ($key_provider instanceof KeyProviderRequirementsInterface) {
    try {
      $key_provider
        ->checkRequirements($this->entity);
    } catch (KeyProviderRequirementsException $exception) {
      $key_provider_requirements_error = TRUE;
      $form['settings']['provider_section']['key_provider']['#attributes']['class'][] = 'error';
    }
  }
  $form['settings']['provider_section']['#open'] = $key_provider_requirements_error || $form_state
    ->getTriggeringElement() && $form_state
    ->getTriggeringElement()['#name'] === 'key_provider';
  $form['settings']['provider_section']['#weight'] = $form['settings']['input_section']['#weight'] + 1;

  // Override the title of the submit button.
  $form['actions']['submit']['#value'] = $this
    ->t('Save configuration');

  // Hide the Delete button.
  $form['actions']['delete']['#access'] = FALSE;
  $form['#attached']['library'][] = 'apigee_edge/apigee_edge.admin';
  return $form;
}