View source
<?php
namespace Drupal\yamlform\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
abstract class YamlFormFilterFormBase extends FormBase {
public function buildForm(array $form, FormStateInterface $form_state, $search = NULL, $state = NULL, array $state_options = []) {
$form['#attributes'] = [
'class' => [
'yamlform-filter-form',
],
];
$form['filter'] = [
'#type' => 'details',
'#title' => $this
->t('Filter submissions'),
'#open' => TRUE,
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$form['filter']['search'] = [
'#type' => 'search',
'#title' => $this
->t('Filter by submitted data and/or notes'),
'#title_display' => 'invisible',
'#placeholder' => $this
->t('Filter by submitted data and/or notes'),
'#maxlength' => 128,
'#size' => 40,
'#default_value' => $search,
];
$form['filter']['state'] = [
'#type' => 'select',
'#title' => $this
->t('State'),
'#title_display' => 'invisible',
'#options' => $state_options,
'#default_value' => $state,
];
$form['filter']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Filter'),
];
if (!empty($search) || !empty($state)) {
$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')),
'state' => trim($form_state
->getValue('state')),
];
$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());
}
}