Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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  * @internal
17  */
18 class ViewsExposedForm extends FormBase {
19
20   /**
21    * The exposed form cache.
22    *
23    * @var \Drupal\views\ExposedFormCache
24    */
25   protected $exposedFormCache;
26
27   /**
28    * Constructs a new ViewsExposedForm
29    *
30    * @param \Drupal\views\ExposedFormCache $exposed_form_cache
31    *   The exposed form cache.
32    */
33   public function __construct(ExposedFormCache $exposed_form_cache) {
34     $this->exposedFormCache = $exposed_form_cache;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static($container->get('views.exposed_form_cache'));
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getFormId() {
48     return 'views_exposed_form';
49   }
50
51   /**
52    * {@inheritdoc}
53    */
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'])) {
57       return [
58         // Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
59         '#theme' => '',
60       ];
61     }
62
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');
69
70     $form_state->setUserInput($view->getExposedInput());
71
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)) {
76       return $cache;
77     }
78
79     $form['#info'] = [];
80
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'];
94           }
95           else {
96             $handler->buildExposedForm($form, $form_state);
97           }
98           if ($info = $handler->exposedInfo()) {
99             $form['#info']["$type-$id"] = $info;
100           }
101         }
102       }
103     }
104
105     $form['actions'] = [
106       '#type' => 'actions',
107     ];
108     $form['actions']['submit'] = [
109       // Prevent from showing up in \Drupal::request()->query.
110       '#name' => '',
111       '#type' => 'submit',
112       '#value' => $this->t('Apply'),
113       '#id' => Html::getUniqueId('edit-submit-' . $view->storage->id()),
114     ];
115
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']);
119
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);
123
124     // Save the form.
125     $this->exposedFormCache->setForm($view->storage->id(), $view->current_display, $form);
126
127     return $form;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function validateForm(array &$form, FormStateInterface $form_state) {
134     $view = $form_state->get('view');
135
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);
141       }
142     }
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);
146   }
147
148   /**
149    * {@inheritdoc}
150    */
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);
161         }
162         else {
163           // The input from the form did not validate, exclude it from the
164           // stored raw data.
165           $exclude[] = $key;
166         }
167       }
168     }
169
170     $view = $form_state->get('view');
171     $view->exposed_data = $values;
172     $view->exposed_raw_input = [];
173
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[$key][] = $value[$option_id];
187           }
188         }
189         else {
190           $view->exposed_raw_input[$key] = $value;
191         }
192       }
193     }
194   }
195
196 }