View source
<?php
namespace Drupal\views\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Render\Element\Checkboxes;
use Drupal\Core\Url;
use Drupal\views\ExposedFormCache;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewsExposedForm extends FormBase {
protected $exposedFormCache;
protected $currentPathStack;
public function __construct(ExposedFormCache $exposed_form_cache, CurrentPathStack $current_path_stack = NULL) {
$this->exposedFormCache = $exposed_form_cache;
if ($current_path_stack === NULL) {
@trigger_error('The path.current service must be passed to ViewsExposedForm::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3066604', E_USER_DEPRECATED);
$current_path_stack = \Drupal::service('path.current');
}
$this->currentPathStack = $current_path_stack;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('views.exposed_form_cache'), $container
->get('path.current'));
}
public function getFormId() {
return 'views_exposed_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($batch = batch_get() && isset($batch['current_set'])) {
return [
'#theme' => '',
];
}
$form_state
->setValidationEnforced();
$view = $form_state
->get('view');
$display =& $form_state
->get('display');
$form_state
->setUserInput($view
->getExposedInput());
$form_state
->set('exposed', TRUE);
if ($cache = $this->exposedFormCache
->getForm($view->storage
->id(), $view->current_display)) {
return $cache;
}
$form['#info'] = [];
foreach ($view->display_handler->handlers as $type => $value) {
foreach ($view->{$type} as $id => $handler) {
if ($handler
->canExpose() && $handler
->isExposed()) {
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'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#name' => '',
'#type' => 'submit',
'#value' => $this
->t('Apply'),
'#id' => Html::getUniqueId('edit-submit-' . $view->storage
->id()),
];
if (!$view
->hasUrl()) {
if ($this
->getRouteMatch()
->getRouteName() !== 'views.ajax') {
$form_action = Url::fromRoute('<current>')
->toString();
}
else {
$form_action = Url::fromUserInput($this->currentPathStack
->getPath())
->toString();
}
}
else {
$form_action = $view
->getUrl()
->toString();
}
$form['#action'] = $form_action;
$form['#theme'] = $view
->buildThemeFunctions('views_exposed_form');
$form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage
->id() . '-' . $display['id']);
$exposed_form_plugin = $view->display_handler
->getPlugin('exposed_form');
$exposed_form_plugin
->exposedFormAlter($form, $form_state);
$this->exposedFormCache
->setForm($view->storage
->id(), $view->current_display, $form);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$view = $form_state
->get('view');
foreach ([
'field',
'filter',
] as $type) {
$handlers =& $view->{$type};
foreach ($handlers as $key => $handler) {
$handlers[$key]
->validateExposed($form, $form_state);
}
}
$exposed_form_plugin = $view->display_handler
->getPlugin('exposed_form');
$exposed_form_plugin
->exposedFormValidate($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$exclude = [
'submit',
'form_build_id',
'form_id',
'form_token',
'exposed_form_plugin',
'reset',
];
$values = $form_state
->getValues();
foreach ([
'field',
'filter',
] as $type) {
$handlers =& $form_state
->get('view')->{$type};
foreach ($handlers as $key => $info) {
if ($handlers[$key]
->acceptExposedInput($values)) {
$handlers[$key]
->submitExposed($form, $form_state);
}
else {
$exclude[] = $key;
}
}
}
$view = $form_state
->get('view');
$view->exposed_data = $values;
$view->exposed_raw_input = [];
$exclude = [
'submit',
'form_build_id',
'form_id',
'form_token',
'exposed_form_plugin',
'reset',
];
$exposed_form_plugin = $view->display_handler
->getPlugin('exposed_form');
$exposed_form_plugin
->exposedFormSubmit($form, $form_state, $exclude);
foreach ($values as $key => $value) {
if (!empty($key) && !in_array($key, $exclude)) {
if (is_array($value)) {
$checked = Checkboxes::getCheckedCheckboxes($value);
foreach ($checked as $option_id) {
$view->exposed_raw_input[$key][$option_id] = $value[$option_id];
}
}
else {
$view->exposed_raw_input[$key] = $value;
}
}
}
}
}