You are here

public function SearchApiElasticsearchService::configurationFormValidate in Search API Elasticsearch 7.2

@inheritdoc

Overrides SearchApiAbstractService::configurationFormValidate

File

includes/SearchApiElasticsearchService.inc, line 306
Provides Elasticsearch service for Search API.

Class

SearchApiElasticsearchService
@file Provides Elasticsearch service for Search API.

Code

public function configurationFormValidate(array $form, array &$values, array &$form_state) {
  unset($values['add_more']);
  if (module_exists('search_api_facetapi')) {

    // Facet limit.
    if (filter_var($values['facet_limit'], FILTER_VALIDATE_INT, array(
      'options' => array(
        'min_range' => 0,
      ),
    )) === FALSE) {
      form_set_error('options][form][facet_limit', t('You must enter a positive integer for the elasticsearch facet limit.'));
    }
  }
  foreach ($values as $i => $setting) {
    if ($i != 'facet_limit') {

      // Daemon IP address.
      if (filter_var($values[$i]['host'], FILTER_VALIDATE_IP) === FALSE) {
        form_set_error('options][form]' . $i . '[host', t('You must enter a valid IP address for the elasticsearch daemon.'));
      }

      // Daemon Port.
      if (filter_var($values[$i]['port'], FILTER_VALIDATE_INT, array(
        'options' => array(
          'min_range' => 0,
          'max_range' => 65535,
        ),
      )) === FALSE) {
        form_set_error('options][form]' . $i . '[port', t('You must enter a valid Port (between 0 and 65535) for the elasticsearch daemon.'));
      }
      $values[$i]['path'] = $this
        ->setPath($values[$i]['path']);
    }

    // Put http_user and http_password in correct form.
    if (!empty($setting['headers']['http_user'])) {

      // If username matches the old value and password is empty, then use the old Authentication.
      if (empty($setting['headers']['http_pass'])) {
        if ($setting['headers']['http_user'] == $this->options[$i]['headers']['http_user']) {
          $values[$i]['headers']['Authorization'] = $this->options[$i]['headers']['Authorization'];
        }
        else {
          form_set_error('http_pass', t('If you are changing the username, you need to supply the password.'));
          return;
        }
      }
      else {
        $values[$i]['headers']['Authorization'] = 'Basic ' . base64_encode($setting['headers']['http_user'] . ':' . $setting['headers']['http_pass']);
      }
    }
    if (isset($values[$i]['headers']['http_pass'])) {
      unset($values[$i]['headers']['http_pass']);
    }
  }
}