You are here

public function ContentHubSettingsForm::submitForm in Acquia Content Hub 8

Same name and namespace in other branches
  1. 8.2 src/Form/ContentHubSettingsForm.php \Drupal\acquia_contenthub\Form\ContentHubSettingsForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/ContentHubSettingsForm.php, line 222

Class

ContentHubSettingsForm
Defines the form to configure the Content Hub connection settings.

Namespace

Drupal\acquia_contenthub\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  // We assume here all inserted values have passed validation.
  // First Register the site to Content Hub.
  $client_name = $form_state
    ->getValue('client_name');
  if ($this->contentHubSubscription
    ->registerClient($client_name)) {

    // Registration was successful. Save the rest of the values.
    // Get the admin config.
    $config = $this
      ->config('acquia_contenthub.admin_settings');
    if ($form_state
      ->hasValue('hostname')) {

      // Remove trailing slash at end of URL.
      $hostname = rtrim($form_state
        ->getValue('hostname'), '/');
      $config
        ->set('hostname', $hostname);
    }
    if ($form_state
      ->hasValue('api_key')) {
      $config
        ->set('api_key', $form_state
        ->getValue('api_key'));
    }
    if ($form_state
      ->hasValue('secret_key')) {
      $secret = $form_state
        ->getValue('secret_key');
      $config
        ->set('secret_key', $secret);
    }
    $config
      ->save();
  }
  else {

    // Get the admin config.
    $config = $this
      ->config('acquia_contenthub.admin_settings');

    // Call drupal_get_messages, to override the dsm. Otherwise,
    // on save it will show two messages.
    // @todo I don't believe this line is needed with the messenger service.
    $this->messenger
      ->all();
    if (!empty($config
      ->get('origin'))) {
      $this
        ->messenger()
        ->addError($this
        ->t('Client is already registered with Acquia Content Hub.'));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('There is a problem connecting to Acquia Content Hub. Please ensure that your hostname and credentials are correct.'));
    }
  }
}