View source
<?php
namespace Drupal\views\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ViewsForm implements FormInterface, ContainerInjectionInterface {
use DependencySerializationTrait;
protected $classResolver;
protected $requestStack;
protected $urlGenerator;
protected $viewId;
protected $viewDisplayId;
protected $viewArguments;
public function __construct(ClassResolverInterface $class_resolver, UrlGeneratorInterface $url_generator, RequestStack $requestStack, $view_id, $view_display_id, array $view_args) {
$this->classResolver = $class_resolver;
$this->urlGenerator = $url_generator;
$this->requestStack = $requestStack;
$this->viewId = $view_id;
$this->viewDisplayId = $view_display_id;
$this->viewArguments = $view_args;
}
public static function create(ContainerInterface $container, $view_id = NULL, $view_display_id = NULL, array $view_args = NULL) {
return new static($container
->get('class_resolver'), $container
->get('url_generator'), $container
->get('request_stack'), $view_id, $view_display_id, $view_args);
}
public function getBaseFormId() {
$parts = [
'views_form',
$this->viewId,
$this->viewDisplayId,
];
return implode('_', $parts);
}
public function getFormId() {
$parts = [
$this
->getBaseFormId(),
];
if (!empty($this->viewArguments)) {
$parts = array_merge($parts, $this->viewArguments);
}
return implode('_', $parts);
}
public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) {
if (!($step = $form_state
->get('step'))) {
$step = 'views_form_views_form';
$form_state
->set('step', $step);
}
$form_state
->set([
'step_controller',
'views_form_views_form',
], 'Drupal\\views\\Form\\ViewsFormMainForm');
$form_state
->addBuildInfo('base_form_id', $this
->getBaseFormId());
$form = [];
$query = $this->requestStack
->getCurrentRequest()->query
->all();
$query = UrlHelper::filterQueryParameters($query, [], '');
$options = [
'query' => $query,
];
$form['#action'] = $view
->hasUrl() ? $view
->getUrl()
->setOptions($options)
->toString() : Url::fromRoute('<current>')
->setOptions($options)
->toString();
$form['show_view_elements'] = [
'#type' => 'value',
'#value' => $step == 'views_form_views_form' ? TRUE : FALSE,
];
$form_object = $this
->getFormObject($form_state);
$form += $form_object
->buildForm($form, $form_state, $view, $output);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$form_object = $this
->getFormObject($form_state);
$form_object
->validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_object = $this
->getFormObject($form_state);
$form_object
->submitForm($form, $form_state);
}
protected function getFormObject(FormStateInterface $form_state) {
$form_step_class = $form_state
->get([
'step_controller',
$form_state
->get('step'),
]) ?: 'Drupal\\views\\Form\\ViewsFormMainForm';
return $this->classResolver
->getInstanceFromDefinition($form_step_class);
}
}