Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\views\Form\ViewsExposedForm.
*/
namespace Drupal\views\Form;
use Drupal\Component\Utility\Html;
Angie Byron
committed
use Drupal\Core\Form\FormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
Angie Byron
committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use Drupal\views\ExposedFormCache;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the views exposed form.
*/
class ViewsExposedForm extends FormBase {
/**
* The exposed form cache.
*
* @var \Drupal\views\ExposedFormCache
*/
protected $exposedFormCache;
/**
* Constructs a new ViewsExposedForm
*
* @param \Drupal\views\ExposedFormCache $exposed_form_cache
* The exposed form cache.
*/
public function __construct(ExposedFormCache $exposed_form_cache) {
$this->exposedFormCache = $exposed_form_cache;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('views.exposed_form_cache'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'views_exposed_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Angie Byron
committed
// Don't show the form when batch operations are in progress.
if ($batch = batch_get() && isset($batch['current_set'])) {
return array(
// Set the theme callback to be nothing to avoid errors in template_preprocess_views_exposed_form().
'#theme' => '',
);
}
// Make sure that we validate because this form might be submitted
// multiple times per page.
Angie Byron
committed
$form_state->setValidationEnforced();
Angie Byron
committed
/** @var \Drupal\views\ViewExecutable $view */
Angie Byron
committed
$view = $form_state->get('view');
$display = &$form_state->get('display');
Angie Byron
committed
Angie Byron
committed
$form_state->setUserInput($view->getExposedInput());
Angie Byron
committed
// Let form plugins know this is for exposed widgets.
Angie Byron
committed
$form_state->set('exposed', TRUE);
Angie Byron
committed
// Check if the form was already created
if ($cache = $this->exposedFormCache->getForm($view->storage->id(), $view->current_display)) {
return $cache;
}
$form['#info'] = array();
// Go through each handler and let it generate its exposed widget.
foreach ($view->display_handler->handlers as $type => $value) {
Alex Pott
committed
/** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
Angie Byron
committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
foreach ($view->$type as $id => $handler) {
if ($handler->canExpose() && $handler->isExposed()) {
// Grouped exposed filters have their own forms.
// Instead of render the standard exposed form, a new Select or
// Radio form field is rendered with the available groups.
// When an user choose an option the selected value is split
// into the operator and value that the item represents.
if ($handler->isAGroup()) {
$handler->groupForm($form, $form_state);
$id = $handler->options['group_info']['identifier'];
}
else {
$handler->buildExposedForm($form, $form_state);
}
if ($info = $handler->exposedInfo()) {
$form['#info']["$type-$id"] = $info;
}
}
}
}
$form['actions'] = array(
'#type' => 'actions'
);
$form['actions']['submit'] = array(
// Prevent from showing up in \Drupal::request()->query.
'#name' => '',
'#type' => 'submit',
'#value' => $this->t('Apply'),
'#id' => Html::getUniqueId('edit-submit-' . $view->storage->id()),
Angie Byron
committed
);
$form['#action'] = $view->hasUrl() ? $view->getUrl()->toString() : Url::fromRoute('<current>')->toString();
Angie Byron
committed
$form['#theme'] = $view->buildThemeFunctions('views_exposed_form');
$form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id']);
Angie Byron
committed
/** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
Alex Pott
committed
$exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
Angie Byron
committed
$exposed_form_plugin->exposedFormAlter($form, $form_state);
// Save the form.
$this->exposedFormCache->setForm($view->storage->id(), $view->current_display, $form);
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function validateForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
$view = $form_state->get('view');
Angie Byron
committed
foreach (array('field', 'filter') as $type) {
Alex Pott
committed
/** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
Alex Pott
committed
$handlers = &$view->$type;
Angie Byron
committed
foreach ($handlers as $key => $handler) {
$handlers[$key]->validateExposed($form, $form_state);
}
}
/** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
Alex Pott
committed
$exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
Angie Byron
committed
$exposed_form_plugin->exposedFormValidate($form, $form_state);
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
foreach (array('field', 'filter') as $type) {
Alex Pott
committed
/** @var \Drupal\views\Plugin\views\ViewsHandlerInterface[] $handlers */
Angie Byron
committed
$handlers = &$form_state->get('view')->$type;
Angie Byron
committed
foreach ($handlers as $key => $info) {
$handlers[$key]->submitExposed($form, $form_state);
}
}
Alex Pott
committed
Angie Byron
committed
$view = $form_state->get('view');
$view->exposed_data = $form_state->getValues();
$view->exposed_raw_input = [];
Angie Byron
committed
Alex Pott
committed
$exclude = array('submit', 'form_build_id', 'form_id', 'form_token', 'exposed_form_plugin', 'reset');
Angie Byron
committed
/** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase $exposed_form_plugin */
Alex Pott
committed
$exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
Angie Byron
committed
$exposed_form_plugin->exposedFormSubmit($form, $form_state, $exclude);
Alex Pott
committed
foreach ($form_state->getValues() as $key => $value) {
Angie Byron
committed
if (!in_array($key, $exclude)) {
Angie Byron
committed
$view->exposed_raw_input[$key] = $value;