solrlog-1.0.1/src/Form/ClearLogConfirmForm.php
src/Form/ClearLogConfirmForm.php
<?php
namespace Drupal\solrlog\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\search_api_solr\SolrConnectorInterface;
use Drupal\solrlog\SolariumTrait;
/**
* Provides a confirmation form before clearing out the logs.
*
* @internal
*/
class ClearLogConfirmForm extends ConfirmFormBase {
use SolariumTrait;
/**
* Solr connector.
*
* @var \Drupal\search_api_solr\SolrConnectorInterface
*/
protected SolrConnectorInterface $connector;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'solrlog_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the recent logs?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('solrlog.overview');
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$connector = $this->getConnector();
if (empty($connector)) {
$form_state->setError($form, $this->t('There is no solr connection configured'));
return;
}
$this->connector = $connector;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$query = $this->connector->getUpdateQuery();
$query->addDeleteQuery('ss_module_id:solrlog')
->addCommit();
$this->connector->execute($query);
$this->messenger()->addStatus($this->t('Solr log cleared.'));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
