IndexReindexConfirmForm.php in Search API 8
File
src/Form/IndexReindexConfirmForm.php
View source
<?php
namespace Drupal\search_api\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use Drupal\search_api\SearchApiException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class IndexReindexConfirmForm extends EntityConfirmFormBase {
protected $messenger;
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
$messenger = $container
->get('messenger');
return new static($messenger);
}
public function getQuestion() {
return $this
->t('Are you sure you want to reindex the search index %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getDescription() {
return $this
->t('Indexed data will remain on the search server until all items have been reindexed. Searches on this index will continue to yield results. This action cannot be undone.');
}
public function getCancelUrl() {
return new Url('entity.search_api_index.canonical', [
'search_api_index' => $this->entity
->id(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$index = $this
->getEntity();
try {
$index
->reindex();
$this->messenger
->addStatus($this
->t('The search index %name was successfully queued for reindexing.', [
'%name' => $index
->label(),
]));
} catch (SearchApiException $e) {
$this->messenger
->addError($this
->t('Failed to queue items for reindexing on search index %name.', [
'%name' => $index
->label(),
]));
$message = '%type while trying to queue items for reindexing on index %name: @message in %function (line %line of %file)';
$variables = [
'%name' => $index
->label(),
];
$variables += Error::decodeException($e);
$this
->getLogger('search_api')
->error($message, $variables);
}
$form_state
->setRedirect('entity.search_api_index.canonical', [
'search_api_index' => $index
->id(),
]);
}
}