diff --git a/core/modules/views/src/Form/ViewsForm.php b/core/modules/views/src/Form/ViewsForm.php index 575b92313f..ae901a1432 100644 --- a/core/modules/views/src/Form/ViewsForm.php +++ b/core/modules/views/src/Form/ViewsForm.php @@ -8,6 +8,8 @@ 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 +48,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 +99,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 +118,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 @@ -157,7 +177,21 @@ public function buildForm(array $form, FormStateInterface $form_state, ViewExecu $query = UrlHelper::filterQueryParameters($query, [], ''); $options = ['query' => $query]; - $form['#action'] = $view->hasUrl() ? $view->getUrl()->setOptions($options)->toString() : Url::fromRoute('')->setOptions($options)->toString(); + if (!$view->hasUrl()) { + // On any non views.ajax route, use the current route for the form action. + if ($this->currentRouteMatch->getRouteName() !== 'views.ajax') { + $form_action = Url::fromRoute('')->setOptions($options)->toString(); + } + else { + // On the views.ajax route, set the action to the page we were on. + $form_action = Url::fromUserInput($this->currentPathStack->getPath())->setOptions($options)->toString(); + } + } + else { + $form_action = $view->getUrl()->setOptions($options)->toString(); + } + + $form['#action'] = $form_action; // Tell the preprocessor whether it should hide the header, footer, pager, // etc. $form['show_view_elements'] = [