3 namespace Drupal\views\Form;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Render\Element\Checkboxes;
10 use Drupal\views\ExposedFormCache;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides the views exposed form.
16 class ViewsExposedForm extends FormBase {
19 * The exposed form cache.
21 * @var \Drupal\views\ExposedFormCache
23 protected $exposedFormCache;
26 * Constructs a new ViewsExposedForm
28 * @param \Drupal\views\ExposedFormCache $exposed_form_cache
29 * The exposed form cache.
31 public function __construct(ExposedFormCache $exposed_form_cache) {
32 $this->exposedFormCache = $exposed_form_cache;
38 public static function create(ContainerInterface $container) {
39 return new static($container->get('views.exposed_form_cache'));
45 public function getFormId() {
46 return 'views_exposed_form';
52 public function buildForm(array $form, FormStateInterface $form_state) {
53 // Don't show the form when batch operations are in progress.
54 if ($batch = batch_get() && isset($batch['current_set'])) {
56 // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
61 // Make sure that we validate because this form might be submitted
62 // multiple times per page.
63 $form_state->setValidationEnforced();
64 /** @var \Drupal\views\ViewExecutable $view */
65 $view = $form_state->get('view');
66 $display = &$form_state->get('display');
68 $form_state->setUserInput($view->getExposedInput());
70 // Let form plugins know this is for exposed widgets.
71 $form_state->set('exposed', TRUE);
72 // Check if the form was already created
73 if ($cache = $this->exposedFormCache->getForm($view->storage->id(), $view->current_display)) {
79 // Go through each handler and let it generate its exposed widget.
80 foreach ($view->display_handler->handlers as $type => $value) {
81 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
82 foreach ($view->$type as $id => $handler) {
83 if ($handler->canExpose() && $handler->isExposed()) {
84 // Grouped exposed filters have their own forms.
85 // Instead of render the standard exposed form, a new Select or
86 // Radio form field is rendered with the available groups.
87 // When an user choose an option the selected value is split
88 // into the operator and value that the item represents.
89 if ($handler->isAGroup()) {
90 $handler->groupForm($form, $form_state);
91 $id = $handler->options['group_info']['identifier'];
94 $handler->buildExposedForm($form, $form_state);
96 if ($info = $handler->exposedInfo()) {
97 $form['#info']["$type-$id"] = $info;
106 $form['actions']['submit'] = [
107 // Prevent from showing up in \Drupal::request()->query.
110 '#value' => $this->t('Apply'),
111 '#id' => Html::getUniqueId('edit-submit-' . $view->storage->id()),
114 $form['#action'] = $view->hasUrl() ? $view->getUrl()->toString() : Url::fromRoute('<current>')->toString();
115 $form['#theme'] = $view->buildThemeFunctions('views_exposed_form');
116 $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id']);
118 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
119 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
120 $exposed_form_plugin->exposedFormAlter($form, $form_state);
123 $this->exposedFormCache->setForm($view->storage->id(), $view->current_display, $form);
131 public function validateForm(array &$form, FormStateInterface $form_state) {
132 $view = $form_state->get('view');
134 foreach (['field', 'filter'] as $type) {
135 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
136 $handlers = &$view->$type;
137 foreach ($handlers as $key => $handler) {
138 $handlers[$key]->validateExposed($form, $form_state);
141 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
142 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
143 $exposed_form_plugin->exposedFormValidate($form, $form_state);
149 public function submitForm(array &$form, FormStateInterface $form_state) {
150 // Form input keys that will not be included in $view->exposed_raw_data.
151 $exclude = ['submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'];
152 $values = $form_state->getValues();
153 foreach (['field', 'filter'] as $type) {
154 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
155 $handlers = &$form_state->get('view')->$type;
156 foreach ($handlers as $key => $info) {
157 if ($handlers[$key]->acceptExposedInput($values)) {
158 $handlers[$key]->submitExposed($form, $form_state);
161 // The input from the form did not validate, exclude it from the
168 $view = $form_state->get('view');
169 $view->exposed_data = $values;
170 $view->exposed_raw_input = [];
172 $exclude = ['submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'];
173 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
174 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
175 $exposed_form_plugin->exposedFormSubmit($form, $form_state, $exclude);
176 foreach ($values as $key => $value) {
177 if (!empty($key) && !in_array($key, $exclude)) {
178 if (is_array($value)) {
179 // Handle checkboxes, we only want to include the checked options.
180 // @todo: revisit the need for this when
181 // https://www.drupal.org/node/342316 is resolved.
182 $checked = Checkboxes::getCheckedCheckboxes($value);
183 foreach ($checked as $option_id) {
184 $view->exposed_raw_input[$option_id] = $value[$option_id];
188 $view->exposed_raw_input[$key] = $value;