View source
<?php
namespace Drupal\yamlform_templates\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class YamlFormtemplatesFilterForm extends FormBase {
public function getFormId() {
return 'yamlform_templates_filter_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $search = NULL) {
$form['#attributes'] = [
'class' => [
'yamlform-filter-form',
],
];
$form['filter'] = [
'#type' => 'details',
'#title' => $this
->t('Filter templates'),
'#open' => TRUE,
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$form['filter']['search'] = [
'#type' => 'search',
'#title' => $this
->t('Filter by title, description, or elements'),
'#title_display' => 'invisible',
'#placeholder' => $this
->t('Filter by title, description, or elements'),
'#maxlength' => 128,
'#size' => 40,
'#autocomplete_route_name' => 'entity.yamlform.templates.autocomplete',
'#default_value' => $search,
];
$form['filter']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Filter'),
];
if (!empty($search)) {
$form['filter']['reset'] = [
'#type' => 'submit',
'#submit' => [
'::resetForm',
],
'#value' => $this
->t('Reset'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$query = [
'search' => trim($form_state
->getValue('search')),
];
$form_state
->setRedirect($this
->getRouteMatch()
->getRouteName(), $this
->getRouteMatch()
->getRawParameters()
->all(), [
'query' => $query,
]);
}
public function resetForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect($this
->getRouteMatch()
->getRouteName(), $this
->getRouteMatch()
->getRawParameters()
->all());
}
}