You are here

public function IndexDeleteForm::submitForm in Elasticsearch Connector 8.6

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

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

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

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

Overrides EntityForm::submitForm

File

src/Form/IndexDeleteForm.php, line 65

Class

IndexDeleteForm
Defines a confirmation form for deletion of a custom menu.

Namespace

Drupal\elasticsearch_connector\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var Cluster $cluster */
  $cluster = $this->entityTypeManager
    ->getStorage('elasticsearch_cluster')
    ->load($this->entity->server);
  $client = $this->clientManager
    ->getClientForCluster($cluster);
  try {
    if ($client
      ->indices()
      ->exists(array(
      'index' => $this->entity->index_id,
    ))) {
      $client
        ->indices()
        ->delete([
        'index' => $this->entity->index_id,
      ]);
    }
    $this->entity
      ->delete();
    $this
      ->messenger()
      ->addMessage($this
      ->t('The index %title has been deleted.', array(
      '%title' => $this->entity
        ->label(),
    )));
    $form_state
      ->setRedirect('elasticsearch_connector.config_entity.list');
  } catch (Missing404Exception $e) {

    // The index was not found, so just remove it anyway.
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  }
}