diff --git a/core/modules/views/src/Form/ViewsForm.php b/core/modules/views/src/Form/ViewsForm.php index 575b92313f..cee1368640 100644 --- a/core/modules/views/src/Form/ViewsForm.php +++ b/core/modules/views/src/Form/ViewsForm.php @@ -2,12 +2,13 @@ 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\Path\CurrentPathStack; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Url; use Drupal\views\ViewExecutable; @@ -46,6 +47,20 @@ class ViewsForm implements FormInterface, ContainerInjectionInterface { */ protected $urlGenerator; + /** + * The current route match. + * + * @var \Drupal\Core\Routing\CurrentRouteMatch + */ + protected $currentRouteMatch; + + /** + * The current path stack. + * + * @var \Drupal\Core\Path\CurrentPathStack + */ + protected $currentPathStack; + /** * The ID of the view. * @@ -83,10 +98,12 @@ class ViewsForm implements FormInterface, ContainerInjectionInterface { * @param string[] $view_args * The arguments passed to the active view. */ - public function __construct(ClassResolverInterface $class_resolver, UrlGeneratorInterface $url_generator, RequestStack $requestStack, $view_id, $view_display_id, array $view_args) { + public function __construct(ClassResolverInterface $class_resolver, UrlGeneratorInterface $url_generator, RequestStack $requestStack, CurrentRouteMatch $currentRouteMatch, CurrentPathStack $currentPathStack, $view_id, $view_display_id, array $view_args) { $this->classResolver = $class_resolver; $this->urlGenerator = $url_generator; $this->requestStack = $requestStack; + $this->currentRouteMatch = $currentRouteMatch; + $this->currentPathStack = $currentPathStack; $this->viewId = $view_id; $this->viewDisplayId = $view_display_id; $this->viewArguments = $view_args; @@ -100,6 +117,8 @@ public static function create(ContainerInterface $container, $view_id = NULL, $v $container->get('class_resolver'), $container->get('url_generator'), $container->get('request_stack'), + $container->get('current_route_match'), + $container->get('path.current'), $view_id, $view_display_id, $view_args @@ -153,11 +172,21 @@ public function buildForm(array $form, FormStateInterface $form_state, ViewExecu $form = []; - $query = $this->requestStack->getCurrentRequest()->query->all(); - $query = UrlHelper::filterQueryParameters($query, [], ''); + if (!$view->hasUrl()) { + // On any non views.ajax route, use the current route for the form action. + if ($this->currentRouteMatch->getCurrentRouteMatch()->getRouteName() !== 'views.ajax') { + $form_action = Url::fromRoute('')->toString(); + } + else { + // On the views.ajax route, set the action to the page we were on. + $form_action = Url::fromUserInput($this->currentPathStack->getPath())->toString(); + } + } + else { + $form_action = $view->getUrl()->toString(); + } - $options = ['query' => $query]; - $form['#action'] = $view->hasUrl() ? $view->getUrl()->setOptions($options)->toString() : Url::fromRoute('')->setOptions($options)->toString(); + $form['#action'] = $form_action; // Tell the preprocessor whether it should hide the header, footer, pager, // etc. $form['show_view_elements'] = [