You are here

public function IndexForm::save in Elasticsearch Connector 8.6

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

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/IndexForm.php, line 271

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.', array(
        '%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', array(
        '%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.', array(
      '%label' => $this->entity
        ->label(),
    )));
    $form_state
      ->setRedirect('elasticsearch_connector.config_entity.list');
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  }
}