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.
18 class ViewsExposedForm extends FormBase {
21 * The exposed form cache.
23 * @var \Drupal\views\ExposedFormCache
25 protected $exposedFormCache;
28 * Constructs a new ViewsExposedForm
30 * @param \Drupal\views\ExposedFormCache $exposed_form_cache
31 * The exposed form cache.
33 public function __construct(ExposedFormCache $exposed_form_cache) {
34 $this->exposedFormCache = $exposed_form_cache;
40 public static function create(ContainerInterface $container) {
41 return new static($container->get('views.exposed_form_cache'));
47 public function getFormId() {
48 return 'views_exposed_form';
54 public function buildForm(array $form, FormStateInterface $form_state) {
55 // Don't show the form when batch operations are in progress.
56 if ($batch = batch_get() && isset($batch['current_set'])) {
58 // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
63 // Make sure that we validate because this form might be submitted
64 // multiple times per page.
65 $form_state->setValidationEnforced();
66 /** @var \Drupal\views\ViewExecutable $view */
67 $view = $form_state->get('view');
68 $display = &$form_state->get('display');
70 $form_state->setUserInput($view->getExposedInput());
72 // Let form plugins know this is for exposed widgets.
73 $form_state->set('exposed', TRUE);
74 // Check if the form was already created
75 if ($cache = $this->exposedFormCache->getForm($view->storage->id(), $view->current_display)) {
81 // Go through each handler and let it generate its exposed widget.
82 foreach ($view->display_handler->handlers as $type => $value) {
83 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
84 foreach ($view->$type as $id => $handler) {
85 if ($handler->canExpose() && $handler->isExposed()) {
86 // Grouped exposed filters have their own forms.
87 // Instead of render the standard exposed form, a new Select or
88 // Radio form field is rendered with the available groups.
89 // When an user choose an option the selected value is split
90 // into the operator and value that the item represents.
91 if ($handler->isAGroup()) {
92 $handler->groupForm($form, $form_state);
93 $id = $handler->options['group_info']['identifier'];
96 $handler->buildExposedForm($form, $form_state);
98 if ($info = $handler->exposedInfo()) {
99 $form['#info']["$type-$id"] = $info;
108 $form['actions']['submit'] = [
109 // Prevent from showing up in \Drupal::request()->query.
112 '#value' => $this->t('Apply'),
113 '#id' => Html::getUniqueId('edit-submit-' . $view->storage->id()),
116 $form['#action'] = $view->hasUrl() ? $view->getUrl()->toString() : Url::fromRoute('<current>')->toString();
117 $form['#theme'] = $view->buildThemeFunctions('views_exposed_form');
118 $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id']);
120 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
121 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
122 $exposed_form_plugin->exposedFormAlter($form, $form_state);
125 $this->exposedFormCache->setForm($view->storage->id(), $view->current_display, $form);
133 public function validateForm(array &$form, FormStateInterface $form_state) {
134 $view = $form_state->get('view');
136 foreach (['field', 'filter'] as $type) {
137 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
138 $handlers = &$view->$type;
139 foreach ($handlers as $key => $handler) {
140 $handlers[$key]->validateExposed($form, $form_state);
143 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
144 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
145 $exposed_form_plugin->exposedFormValidate($form, $form_state);
151 public function submitForm(array &$form, FormStateInterface $form_state) {
152 // Form input keys that will not be included in $view->exposed_raw_data.
153 $exclude = ['submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'];
154 $values = $form_state->getValues();
155 foreach (['field', 'filter'] as $type) {
156 /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
157 $handlers = &$form_state->get('view')->$type;
158 foreach ($handlers as $key => $info) {
159 if ($handlers[$key]->acceptExposedInput($values)) {
160 $handlers[$key]->submitExposed($form, $form_state);
163 // The input from the form did not validate, exclude it from the
170 $view = $form_state->get('view');
171 $view->exposed_data = $values;
172 $view->exposed_raw_input = [];
174 $exclude = ['submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset'];
175 /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
176 $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
177 $exposed_form_plugin->exposedFormSubmit($form, $form_state, $exclude);
178 foreach ($values as $key => $value) {
179 if (!empty($key) && !in_array($key, $exclude)) {
180 if (is_array($value)) {
181 // Handle checkboxes, we only want to include the checked options.
182 // @todo: revisit the need for this when
183 // https://www.drupal.org/node/342316 is resolved.
184 $checked = Checkboxes::getCheckedCheckboxes($value);
185 foreach ($checked as $option_id) {
186 $view->exposed_raw_input[$option_id] = $value[$option_id];
190 $view->exposed_raw_input[$key] = $value;