Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Form / ViewsExposedForm.php
1 <?php
2
3 namespace Drupal\views\Form;
4
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;
9 use Drupal\Core\Url;
10 use Drupal\views\ExposedFormCache;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides the views exposed form.
15  */
16 class ViewsExposedForm extends FormBase {
17
18   /**
19    * The exposed form cache.
20    *
21    * @var \Drupal\views\ExposedFormCache
22    */
23   protected $exposedFormCache;
24
25   /**
26    * Constructs a new ViewsExposedForm
27    *
28    * @param \Drupal\views\ExposedFormCache $exposed_form_cache
29    *   The exposed form cache.
30    */
31   public function __construct(ExposedFormCache $exposed_form_cache) {
32     $this->exposedFormCache = $exposed_form_cache;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static($container->get('views.exposed_form_cache'));
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'views_exposed_form';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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'])) {
55       return [
56         // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
57         '#theme' => '',
58       ];
59     }
60
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');
67
68     $form_state->setUserInput($view->getExposedInput());
69
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)) {
74       return $cache;
75     }
76
77     $form['#info'] = [];
78
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'];
92           }
93           else {
94             $handler->buildExposedForm($form, $form_state);
95           }
96           if ($info = $handler->exposedInfo()) {
97             $form['#info']["$type-$id"] = $info;
98           }
99         }
100       }
101     }
102
103     $form['actions'] = [
104       '#type' => 'actions'
105     ];
106     $form['actions']['submit'] = [
107       // Prevent from showing up in \Drupal::request()->query.
108       '#name' => '',
109       '#type' => 'submit',
110       '#value' => $this->t('Apply'),
111       '#id' => Html::getUniqueId('edit-submit-' . $view->storage->id()),
112     ];
113
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']);
117
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);
121
122     // Save the form.
123     $this->exposedFormCache->setForm($view->storage->id(), $view->current_display, $form);
124
125     return $form;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function validateForm(array &$form, FormStateInterface $form_state) {
132     $view = $form_state->get('view');
133
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);
139       }
140     }
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);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
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);
159         }
160         else {
161           // The input from the form did not validate, exclude it from the
162           // stored raw data.
163           $exclude[] = $key;
164         }
165       }
166     }
167
168     $view = $form_state->get('view');
169     $view->exposed_data = $values;
170     $view->exposed_raw_input = [];
171
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];
185           }
186         }
187         else {
188           $view->exposed_raw_input[$key] = $value;
189         }
190       }
191     }
192   }
193
194 }