You are here

function l10n_client_contributor_form_locale_translate_settings_validate in Localization client 8

Validation for added settings on localization client UI settings form.

1 string reference to 'l10n_client_contributor_form_locale_translate_settings_validate'
l10n_client_contributor_form_locale_translate_settings_alter in l10n_client_contributor/l10n_client_contributor.module
Implements hook_form_FORM_ID_alter().

File

l10n_client_contributor/l10n_client_contributor.module, line 83
Submits translations to a remote localization server.

Code

function l10n_client_contributor_form_locale_translate_settings_validate($form, FormStateInterface $form_state) {

  // Verify connection with the server if enabled.
  if ($form_state
    ->getValue('l10n_client_contributor_use_server')) {
    if (!$form_state
      ->isValueEmpty('l10n_client_contributor_server')) {

      // Try to invoke the remote string submission with a test request.
      $response = xmlrpc($form_state
        ->getValue('l10n_client_contributor_server') . '/xmlrpc.php', array(
        'l10n.server.test' => array(
          '2.0',
        ),
      ));
      if ($response && !empty($response['name']) && !empty($response['version'])) {
        if (empty($response['supported']) || !$response['supported']) {
          $form_state
            ->setErrorByName('l10n_client_contributor_server', t('The given server could not handle the v2.0 remote submission API.'));
        }
        else {
          \Drupal::messenger()
            ->addMessage(t('Verified that the specified server can handle remote string submissions. Supported languages: %languages.', array(
            '%languages' => $response['languages'],
          )));
        }
      }
      else {
        $form_state
          ->setErrorByName('l10n_client_contributor_server', t('Invalid localization server address specified. Make sure you specified the right server address.'));
      }
    }
    else {
      $form_state
        ->setErrorByName('l10n_client_contributor_server', t('You should provide a server address, such as https://localize.drupal.org/'));
    }
  }
}