SuggestionIndexForm.php in Autocomplete Search Suggestions 8.2
File
src/Form/SuggestionIndexForm.php
View source
<?php
namespace Drupal\suggestion\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\suggestion\SuggestionHelper as Helper;
use Drupal\suggestion\SuggestionStorage as Storage;
class SuggestionIndexForm extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$synced = Helper::getConfig('synced');
$form['feedback'] = [
'#markup' => '<div id="suggestion-index-feedback">' . ($synced ? $this
->t('No indexing required.') : $this
->t('Indexing required.')) . '</div>',
'#weight' => 10,
];
$form['flush'] = [
'#title' => $this
->t('Flush all suggestions'),
'#description' => $this
->t('Flushes all suggestions including priority and surfer suggestions.'),
'#type' => 'checkbox',
'#default_value' => FALSE,
'#required' => FALSE,
'#weight' => 20,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Index Suggestions'),
'#weight' => 30,
'#ajax' => [
'callback' => '\\Drupal\\suggestion\\Form\\SuggestionIndexForm::submitForm',
'effect' => 'fade',
'method' => 'replace',
'wrapper' => 'suggestion-index-feedback',
'progress' => [
'type' => 'throbber',
'message' => 'Please wait...',
],
],
];
return $form;
}
public function getFormId() {
return 'suggestion_index';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$t = new TranslatableMarkup('Suggestions Indexed');
if ($form_state
->getValue('flush')) {
Storage::truncateSuggestion();
}
Helper::index();
return [
'#markup' => '<div id="suggestion-index-feedback"><p>' . $t
->render() . '</p></div>',
];
}
}