You are here

public function KeyEntityFormEnhancer::alterForm in Apigee Edge 8

Alters entity forms that defines an Apigee Edge authentication key.

Parameters

array $form: Form render array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

File

src/KeyEntityFormEnhancer.php, line 131

Class

KeyEntityFormEnhancer
Enhances Apigee Edge related Key entity add/edit forms.

Namespace

Drupal\apigee_edge

Code

public function alterForm(array &$form, FormStateInterface $form_state) : void {

  // Sanity check, form must be a Key form.
  if (!$form_state
    ->getFormObject() instanceof KeyFormBase) {
    return;
  }

  /** @var \Drupal\key\KeyInterface $key */
  $key = $form_state
    ->getFormObject()
    ->getEntity();

  // Do not alter the confirmation step of the key edit form.
  if (!$key
    ->isNew() && isset($form['confirm_edit'])) {
    return;
  }
  $form['#prefix'] = '<div id="apigee-edge-key-form-enhancer">';
  $form['#suffix'] = '</div>';

  // We can not add this when AJAX reloads the page and it is sure that this
  // is an Apigee Edge Authentication key but it only validates Apigee Edge
  // Authentication keys.
  $form['#validate'][] = [
    $this,
    'validateForm',
  ];

  // Add enhancements to Apigee Edge Authentication keys.
  if ($this
    ->isApigeeKeyTypeAuthForm($form_state)) {

    /** @var \Drupal\apigee_edge\Plugin\KeyProviderRequirementsInterface $key_provider */
    $key_provider = $key
      ->getKeyProvider();

    // Warn user about key provider pre-requirement issues before form
    // submission.
    if ($key_provider instanceof KeyProviderRequirementsInterface) {
      try {
        $key_provider
          ->checkRequirements($key);
      } catch (KeyProviderRequirementsException $exception) {

        // Report key provider errors inline. This also allows us to clear
        // these error messages when the provider changes.
        $form['settings']['provider_section']['key_provider_error'] = [
          '#theme' => 'status_messages',
          '#message_list' => [
            'error' => [
              $this
                ->t('The requirements of the selected %key_provider key provider are not fulfilled. Fix errors described below or change the key provider.', [
                '%key_provider' => $key_provider
                  ->getPluginDefinition()['label'],
              ]),
              $exception
                ->getTranslatableMarkupMessage(),
            ],
          ],
          // Display it on the top of the section.
          '#weight' => -100,
        ];
      }
    }

    // Placeholder for messages. This also must be part of the form always
    // because without it we could not render messages on the top of the form.
    $form['settings']['messages'] = [
      '#theme' => 'status_messages',
      '#message_list' => [],
      '#weight' => -100,
    ];

    // Placeholder for debug.
    $form['settings']['debug_placeholder'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'id' => 'apigee-edge-auth-form-debug-info',
      ],
    ];
    $form['settings']['debug'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Debug information'),
      '#access' => FALSE,
      '#open' => FALSE,
      '#theme_wrappers' => [
        'details' => [],
        'container' => [
          '#attributes' => [
            'id' => 'apigee-edge-auth-form-debug-info',
          ],
        ],
      ],
    ];
    $form['settings']['debug']['debug_text'] = [
      '#type' => 'textarea',
      '#disabled' => TRUE,
      '#rows' => 20,
    ];
    $form['settings']['test_connection'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Test connection'),
      '#description' => $this
        ->t('Send request using the given API credentials.'),
      '#open' => TRUE,
      '#theme_wrappers' => [
        'details' => [],
        'container' => [
          '#attributes' => [
            'id' => 'apigee-edge-connection-info',
          ],
        ],
      ],
    ];
    if (!$this
      ->keyIsWritable($key)) {
      if ($key
        ->isNew()) {
        $form['settings']['test_connection']['#description'] = $this
          ->t('Send request using the stored credentials in the key provider');
      }
      else {
        $form['settings']['test_connection']['#description'] = $this
          ->t("Send request using the <a href=':key_config_uri' target='_blank'>active authentication key</a>.", [
          ':key_config_uri' => $key
            ->toUrl()
            ->toString(),
        ]);
      }
    }
    $form['settings']['test_connection']['test_connection_submit'] = [
      '#type' => 'submit',
      '#executes_submit_callback' => FALSE,
      '#value' => $this
        ->t('Send request'),
      '#name' => 'test_connection',
      '#ajax' => [
        'callback' => [
          $this,
          'testConnectionAjaxCallback',
        ],
        'wrapper' => 'apigee-edge-key-form-enhancer',
        'progress' => [
          'type' => 'throbber',
          'message' => $this
            ->t('Waiting for response...'),
        ],
      ],
      '#states' => [
        'enabled' => [
          [
            ':input[name="key_input_settings[organization]"]' => [
              'empty' => FALSE,
            ],
            ':input[name="key_input_settings[password]"]' => [
              'empty' => FALSE,
            ],
            ':input[name="key_input_settings[username]"]' => [
              'empty' => FALSE,
            ],
          ],
          [
            ':input[name="key_input_settings[instance_type]"]' => [
              'value' => EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID,
            ],
            ':input[name="key_input_settings[organization]"]' => [
              'empty' => FALSE,
            ],
            ':input[name="key_input_settings[account_json_key]"]' => [
              'empty' => FALSE,
            ],
          ],
        ],
      ],
    ];
  }
}