You are here

public function IndexForm::submit in Elasticsearch Connector 8

File

src/Form/IndexForm.php, line 236
Contains \Drupal\elasticsearch_connector\Form\IndexForm.

Class

IndexForm
Form controller for node type forms.

Namespace

Drupal\elasticsearch_connector\Form

Code

public function submit(array $form, FormStateInterface $form_state) {
  $values = $values;
  $cluster_url = self::getSelectedClusterUrl($values['server']);
  $client = Cluster::getClientByUrls(array(
    $cluster_url,
  ));
  $index_params = array();
  if ($client) {
    try {
      $index_params['index'] = $values['index_id'];
      $index_params['body']['settings']['number_of_shards'] = $values['num_of_shards'];
      $index_params['body']['settings']['number_of_replicas'] = $values['num_of_replica'];
      $index_params['body']['settings']['cluster_machine_name'] = $values['server'];
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    try {
      $response = $client
        ->indices()
        ->create($index_params);
      if (Cluster::elasticsearchCheckResponseAck($response)) {
        drupal_set_message(t('The index %index having id %index_id has been successfully created.', array(
          '%index' => $values['name'],
          '%index_id' => $values['index_id'],
        )));
      }
      else {
        drupal_set_message(t('Fail to create the index %index having id @index_id', array(
          '%index' => $values['name'],
          '@index_id' => $values['index_id'],
        )), 'error');
      }
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
  return parent::submit($form, $form_state);
}