You are here

public function ServerClearConfirmForm::submitForm in Search API 8

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/ServerClearConfirmForm.php, line 68

Class

ServerClearConfirmForm
Defines a confirm form for clearing a server.

Namespace

Drupal\search_api\Form

Code

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

  /** @var \Drupal\search_api\ServerInterface $server */
  $server = $this
    ->getEntity();
  try {
    $server
      ->deleteAllItems();
    $this->messenger
      ->addStatus($this
      ->t('All indexed data was successfully deleted from the server.'));
  } catch (SearchApiException $e) {
    $this->messenger
      ->addError($this
      ->t('Indexed data could not be cleared for some indexes. Check the logs for details.'));
  }
  $failed_reindexing = [];
  $properties = [
    'status' => TRUE,
    'read_only' => FALSE,
  ];
  foreach ($server
    ->getIndexes($properties) as $index) {
    try {
      $index
        ->reindex();
    } catch (SearchApiException $e) {
      $message = '%type while clearing index %index: @message in %function (line %line of %file).';
      $variables = [
        '%index' => $index
          ->label(),
      ];
      $variables += Error::decodeException($e);
      $this
        ->getLogger('search_api')
        ->error($message, $variables);
      $failed_reindexing[] = $index
        ->label();
    }
  }
  if ($failed_reindexing) {
    $args = [
      '@indexes' => implode(', ', $failed_reindexing),
    ];
    $this->messenger
      ->addWarning($this
      ->t('Failed to mark the following indexes for reindexing: @indexes. Check the logs for details.', $args));
  }
  $form_state
    ->setRedirect('entity.search_api_server.canonical', [
    'search_api_server' => $server
      ->id(),
  ]);
}