SavedSearchForm.php in Search API Saved Searches 8
File
src/Form/SavedSearchForm.php
View source
<?php
namespace Drupal\search_api_saved_searches\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class SavedSearchForm extends ContentEntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$search = $this
->getEntity();
$args['%search_label'] = $search
->label();
$form['#title'] = $this
->t('Edit saved search %search_label', $args);
if ($search
->getType()
->getOption('allow_keys_change', FALSE) && !is_array($search
->getQuery()
->getOriginalKeys())) {
$keywords = $search
->getQuery()
->getOriginalKeys();
$form['search_keywords'] = [
'#type' => 'textfield',
'#title' => $this
->t('Fulltext keywords'),
'#description' => $this
->t('The fulltext keywords set on this search.'),
'#default_value' => $keywords,
];
}
return $form;
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$search = parent::buildEntity($form, $form_state);
if ($form_state
->hasValue('search_keywords')) {
$keywords = trim($form_state
->getValue('search_keywords'));
if ($keywords === '') {
$keywords = NULL;
}
$query = $search
->getQuery();
$query
->keys($keywords);
$search
->setQuery($query);
}
return $search;
}
}