You are here

public function ContentHubSettingsForm::validateForm in Acquia Content Hub 8

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

Form validation 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 FormBase::validateForm

File

src/Form/ContentHubSettingsForm.php, line 161

Class

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

Namespace

Drupal\acquia_contenthub\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $hostname = NULL;
  if (UrlHelper::isValid($form_state
    ->getValue('hostname'), TRUE)) {

    // Remove trailing slash at end of URL.
    $hostname = rtrim($form_state
      ->getValue('hostname'), '/');
  }
  else {
    return $form_state
      ->setErrorByName('hostname', $this
      ->t('This is not a valid URL. Please insert it again.'));
  }
  $api = NULL;

  // Important. This should never validate if it is an UUID. Lift 3 does not
  // use UUIDs for the api_key but they are valid for Content Hub.
  if ($form_state
    ->getValue('api_key')) {
    $api = $form_state
      ->getValue('api_key');
  }
  else {
    return $form_state
      ->setErrorByName('api_key', $this
      ->t('Please insert an API Key.'));
  }
  $secret = NULL;
  if ($form_state
    ->hasValue('secret_key')) {
    $secret = $form_state
      ->getValue('secret_key');
  }
  else {
    return $form_state
      ->setErrorByName('secret_key', $this
      ->t('Please insert a Secret Key.'));
  }
  if ($form_state
    ->hasValue('client_name')) {
    $client_name = $form_state
      ->getValue('client_name');
  }
  else {
    return $form_state
      ->setErrorByName('client_name', $this
      ->t('Please insert a Client Name.'));
  }
  if (Uuid::isValid($form_state
    ->getValue('origin'))) {
    $origin = $form_state
      ->getValue('origin');
  }
  else {
    $origin = '';
  }

  // Validate that the client name does not exist yet.
  $this->clientManager
    ->resetConnection([
    'hostname' => $hostname,
    'api' => $api,
    'secret' => $secret,
    'origin' => $origin,
  ]);
  if ($this->clientManager
    ->isClientNameAvailable($client_name) === FALSE) {
    $message = $this
      ->t('The client name "%name" is already being used. Please insert another one.', [
      '%name' => $client_name,
    ]);
    return $form_state
      ->setErrorByName('client_name', $message);
  }
}