You are here

public function SearchApiElasticsearchAbstractService::configurationForm in Search API Elasticsearch 7

Overrides configurationForm().

Overrides SearchApiAbstractService::configurationForm

1 call to SearchApiElasticsearchAbstractService::configurationForm()
SearchApiElasticsearchElastica::viewSettings in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Overrides viewSettings().

File

includes/SearchApiElasticsearchAbstractService.inc, line 29
Provides a Elasticsearch-based service class for the Search API.

Class

SearchApiElasticsearchAbstractService
Elasticsearch service abstract class.

Code

public function configurationForm(array $form, array &$form_state) {
  $options = $this->options + array(
    'host' => '127.0.0.1',
    'port' => 9200,
    'path' => '',
    'url' => NULL,
    'transport' => 'Http',
    'persistent' => TRUE,
    'timeout' => 300,
    'log' => FALSE,
    'retryOnConflict' => 0,
  );

  // Daemon settings.
  $form['daemon_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Elasticsearch client settings'),
    '#tree' => TRUE,
    '#prefix' => '<div id="elasticsearch-ajax-wrapper">',
    '#suffix' => '</div>',
  );
  $delta = 1;
  $i = 1;
  if (isset($form_state['values']['options']['form']) && !empty($form_state['values']['options']['form']) && !isset($form_state['values']['remove_delta'])) {
    unset($form_state['values']['options']['form']['add_more']);
    if (isset($form_state['values']['options']['form']['facet_limit'])) {
      unset($form_state['values']['options']['form']['facet_settings']);
      unset($form_state['values']['options']['form']['facet_limit']);
    }
    $delta = count($form_state['values']['options']['form']) + 1;
  }
  elseif (isset($form_state['values']['remove_delta'])) {
    unset($form_state['values']['options']['form']['add_more']);
    if (isset($form_state['values']['options']['form']['facet_limit'])) {
      unset($form_state['values']['options']['form']['facet_settings']);
      unset($form_state['values']['options']['form']['facet_limit']);
    }
    $delta = count($form_state['values']['options']['form']);
  }
  elseif (isset($this->options) && !empty($this->options)) {
    if (isset($this->options['facet_limit'])) {
      unset($this->options['facet_limit']);
    }
    $delta = count($this->options);
  }
  for ($c = 0; $c < $delta; $c++) {
    if (isset($form_state['values']['remove_delta']) && $c == $form_state['values']['remove_delta']) {
      unset($form_state['values']['options']['form'][$form_state['values']['remove_delta']]);
      continue;
    }
    else {

      // Daemon settings.
      $form['daemon_settings']['fieldset'][$c] = array(
        '#type' => 'fieldset',
        '#title' => t('Node :id', array(
          ':id' => $i,
        )),
        '#tree' => TRUE,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      // Elasticsearch daemon host.
      $form['daemon_settings']['fieldset'][$c]['host'] = array(
        '#type' => 'textfield',
        '#title' => t('Host'),
        '#description' => t('Host to which the elasticsearch daemon will listen for this server. Default is %default.', array(
          '%default' => $options['host'],
        )),
        '#required' => TRUE,
        '#default_value' => isset($options[$c]['host']) ? $options[$c]['host'] : $options['host'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'host',
        ),
      );

      // Elasticsearch daemon port.
      $form['daemon_settings']['fieldset'][$c]['port'] = array(
        '#type' => 'textfield',
        '#title' => t('Port'),
        '#description' => t('Port to which the elasticsearch daemon will listen for this server. Default is %default.', array(
          '%default' => $options['port'],
        )),
        '#required' => TRUE,
        '#default_value' => isset($options[$c]['port']) ? $options[$c]['port'] : $options['port'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'port',
        ),
      );

      //Elasticsearch basic authentication.
      $form['daemon_settings']['fieldset'][$c]['headers'] = array(
        '#type' => 'fieldset',
        '#tree' => TRUE,
        '#title' => t('HTTP Basic Authentication'),
        '#description' => t('If your Elasticsearch server is protected by basic HTTP authentication, enter the login data here.'),
      );

      //Elasticsearch basic authentication username.
      $form['daemon_settings']['fieldset'][$c]['headers']['http_user'] = array(
        '#type' => 'textfield',
        '#title' => t('Username'),
        '#default_value' => !empty($options[$c]['headers']) && !empty($options[$c]['headers']['http_user']) ? $options[$c]['headers']['http_user'] : '',
        '#parents' => array(
          'options',
          'form',
          $c,
          'headers',
          'http_user',
        ),
      );

      //Elasticsearch basic password.
      $form['daemon_settings']['fieldset'][$c]['headers']['http_pass'] = array(
        '#type' => 'password',
        '#title' => t('Password'),
        '#parents' => array(
          'options',
          'form',
          $c,
          'headers',
          'http_pass',
        ),
      );

      //Elasticsearch basic authentication.
      $form['daemon_settings']['fieldset'][$c]['aws'] = array(
        '#type' => 'fieldset',
        '#tree' => TRUE,
        '#title' => t('AWS IAM Authentication'),
        '#description' => t('If your Elasticsearch server is protected by basic AWS authentication, enter the IAM credentials here, you shoul use the AwsAuthV4 transporter.'),
      );

      //Elasticsearch AWS Access Key.
      $form['daemon_settings']['fieldset'][$c]['aws']['aws_access_key_id'] = array(
        '#type' => 'textfield',
        '#title' => t('Access Key'),
        '#default_value' => !empty($options[$c]['aws']) && !empty($options[$c]['aws']['aws_access_key_id']) ? $options[$c]['aws']['aws_access_key_id'] : '',
        '#description' => t('IAM Access Key ID.'),
        '#parents' => array(
          'options',
          'form',
          $c,
          'aws',
          'aws_access_key_id',
        ),
      );

      //Elasticsearch AWS Secret Key.
      $form['daemon_settings']['fieldset'][$c]['aws']['aws_secret_access_key'] = array(
        '#type' => 'textfield',
        '#title' => t('Secret Key'),
        '#default_value' => !empty($options[$c]['aws']) && !empty($options[$c]['aws']['aws_secret_access_key']) ? $options[$c]['aws']['aws_secret_access_key'] : '',
        '#description' => t('IAM Secret Access Key.'),
        '#parents' => array(
          'options',
          'form',
          $c,
          'aws',
          'aws_secret_access_key',
        ),
      );

      //Elasticsearch AWS Region.
      $form['daemon_settings']['fieldset'][$c]['aws']['aws_region'] = array(
        '#type' => 'textfield',
        '#title' => t('AWS Region'),
        '#default_value' => !empty($options[$c]['aws']) && !empty($options[$c]['aws']['aws_region']) ? $options[$c]['aws']['aws_region'] : '',
        '#description' => t('Region where is installed your ElasticSearch Service Instance.'),
        '#parents' => array(
          'options',
          'form',
          $c,
          'aws',
          'aws_region',
        ),
      );

      // Elasticsearch daemon path.
      $form['daemon_settings']['fieldset'][$c]['path'] = array(
        '#type' => 'textfield',
        '#title' => t('Elasticsearch path prefix'),
        '#description' => t('Normally empty. Use when you have remapped the Elasticsearch server API path.'),
        '#required' => FALSE,
        '#default_value' => isset($options[$c]['path']) ? $options[$c]['path'] : $options['path'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'path',
        ),
      );

      // Elasticsearch daemon URL.
      $form['daemon_settings']['fieldset'][$c]['url'] = array(
        '#type' => 'textfield',
        '#title' => t('Elasticsearch url'),
        '#description' => t('Normally empty. Use instead of host/port when you have remapped the Elasticsearch server API url.'),
        '#required' => FALSE,
        '#default_value' => isset($options[$c]['url']) ? $options[$c]['url'] : $options['url'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'url',
        ),
      );

      // Elasticsearch daemon transport.
      $form['daemon_settings']['fieldset'][$c]['transport'] = array(
        '#type' => 'select',
        '#title' => t('Select transport'),
        '#description' => t('Transport to connect to this elasticsearch server.'),
        '#options' => $this
          ->getTransportOptions(),
        '#default_value' => isset($options[$c]['transport']) ? $options[$c]['transport'] : $options['transport'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'transport',
        ),
      );

      // Elasticsearch daemon persistent.
      $form['daemon_settings']['fieldset'][$c]['persistent'] = array(
        '#type' => 'checkbox',
        '#title' => t('Persistent connection'),
        '#description' => t('Use persistent connection when connecting to this node.'),
        '#default_value' => isset($options[$c]['persistent']) ? $options[$c]['persistent'] : $options['persistent'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'persistent',
        ),
      );

      // Elasticsearch daemon timeout.
      $form['daemon_settings']['fieldset'][$c]['timeout'] = array(
        '#type' => 'textfield',
        '#title' => t('Timeout in ms'),
        '#description' => t('Timeout in ms for waiting this elastic server to respond'),
        '#default_value' => isset($options[$c]['timeout']) ? $options[$c]['timeout'] : $options['timeout'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'timeout',
        ),
      );

      // Elasticsearch daemon log.
      $form['daemon_settings']['fieldset'][$c]['log'] = array(
        '#type' => 'checkbox',
        '#title' => t('Log'),
        '#description' => t('Log this elasticsearch server queries to the default log.'),
        '#default_value' => isset($options[$c]['log']) ? $options[$c]['log'] : $options['log'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'log',
        ),
      );
      if (!class_exists('\\Psr\\Log\\AbstractLogger')) {
        $form['daemon_settings']['fieldset'][$c]['log']['#disabled'] = TRUE;
        $form['daemon_settings']['fieldset'][$c]['log']['#description'] = t('Logging Elasticsearch queries requires the <a href="@psr3_url">PSR-3 logger</a> to be installed and available. It is recommended to install the <a href="@psr3_watchdog_url">PSR-3 Watchdog module</a> or <a href="@monolog_url">Monolog module</a>.', array(
          '@psr_url' => url('https://packagist.org/packages/psr/log'),
          '@psr3_watchdog_url' => url('https://drupal.org/project/psr3_watchdog'),
          '@monolog_url' => url('https://www.drupal.org/project/monolog'),
        ));
      }

      // Elasticsearch daemon retryOnConflict.
      $form['daemon_settings']['fieldset'][$c]['retryOnConflict'] = array(
        '#type' => 'textfield',
        '#title' => t('retryOnConflict'),
        '#description' => t('Sets the number of retries of a version conflict occurs because the document was updated between getting it and updating it.'),
        '#default_value' => isset($options[$c]['retryOnConflict']) ? $options[$c]['retryOnConflict'] : $options['retryOnConflict'],
        '#parents' => array(
          'options',
          'form',
          $c,
          'retryOnConflict',
        ),
      );
      if (!isset($form_state['values']['remove_delta']) && $delta > 1 || isset($form_state['values']['remove_delta']) && $delta > 2) {

        // Elasticsearch daemon retryOnConflict.
        $form['daemon_settings']['fieldset'][$c]['remove_node'] = array(
          '#type' => 'submit',
          '#value' => t('Remove node') . ' ' . $i,
          '#submit' => array(
            '_search_api_elasticsearch_configuration_form_remove_custom',
          ),
          '#ajax' => array(
            'callback' => '_search_api_elasticsearch_configuration_form_remove_ajax',
            'wrapper' => 'elasticsearch-ajax-wrapper',
            'method' => 'replace',
            'effect' => 'fade',
          ),
          '#remove_delta' => $c,
          '#parents' => array(
            'options',
            'form',
            $c,
            'remove_node',
          ),
        );
      }
    }
    $i++;
  }

  // Elasticsearch daemon retryOnConflict.
  $form['add_more'] = array(
    '#type' => 'submit',
    '#value' => t('+'),
    '#submit' => array(
      '_search_api_elasticsearch_configuration_form_submit_custom',
    ),
    '#ajax' => array(
      'callback' => '_search_api_elasticsearch_configuration_form_ajax',
      'wrapper' => 'elasticsearch-ajax-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  if (module_exists('search_api_facetapi')) {

    // Facet settings.
    $form['facet_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Elasticsearch facet settings'),
      '#tree' => FALSE,
    );

    // Elasticsearch facet limit.
    $default = 10;
    $form['facet_settings']['facet_limit'] = array(
      '#type' => 'textfield',
      '#title' => t('Facet limit'),
      '#description' => t("Maximum number of facet elements to be returned by the server if 'no limit' is selected as hard limit is the facet option. Default is %default.", array(
        '%default' => $default,
      )),
      '#required' => TRUE,
      '#default_value' => isset($options['facet_limit']) ? $options['facet_limit'] : $default,
      '#parents' => array(
        'options',
        'form',
        'facet_limit',
      ),
    );
  }
  return $form;
}