You are here

public function IndexForm::save in Elasticsearch Connector 8.7

Same name and namespace in other branches
  1. 8 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm::save()
  2. 8.2 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm::save()
  3. 8.5 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm::save()
  4. 8.6 src/Form/IndexForm.php \Drupal\elasticsearch_connector\Form\IndexForm::save()

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides EntityForm::save

File

src/Form/IndexForm.php, line 301

Class

IndexForm
Form controller for node type forms.

Namespace

Drupal\elasticsearch_connector\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $cluster = $this->entityTypeManager
    ->getStorage('elasticsearch_cluster')
    ->load($this->entity->server);
  $client = $this->clientManager
    ->getClientForCluster($cluster);
  $index_params['index'] = $this->entity->index_id;
  $index_params['body']['settings']['number_of_shards'] = $form_state
    ->getValue('num_of_shards');
  $index_params['body']['settings']['number_of_replicas'] = $form_state
    ->getValue('num_of_replica');
  $index_params['body']['settings']['codec'] = $form_state
    ->getValue('codec');
  try {
    $response = $client
      ->indices()
      ->create($index_params);
    if ($client
      ->CheckResponseAck($response)) {
      $this
        ->messenger()
        ->addMessage(t('The index %index having id %index_id has been successfully created.', [
        '%index' => $form_state
          ->getValue('name'),
        '%index_id' => $form_state
          ->getValue('index_id'),
      ]));
    }
    else {
      $this
        ->messenger()
        ->addError(t('Fail to create the index %index having id @index_id', [
        '%index' => $form_state
          ->getValue('name'),
        '@index_id' => $form_state
          ->getValue('index_id'),
      ]));
    }
    parent::save($form, $form_state);
    $this
      ->messenger()
      ->addMessage(t('Index %label has been added.', [
      '%label' => $this->entity
        ->label(),
    ]));
    $form_state
      ->setRedirect('elasticsearch_connector.config_entity.list');
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  }
}