Problem/Motivation
In AJAX views, Views used to set the current path with two leading slashes in some scenarios. This was fixed as a side effect of #2820347: Exposed filter reset redirects user to 404 page on AJAX view when placed as a block, but that fix didn’t include test coverage for this part of the problem.
Proposed resolution
Add test coverage.
Remaining tasks
Review and commit patch.
User interface/API/Data model changes
None.
Original bug report:
In the AJAX request, Views passes the current path (retrieved in views_views_pre_render()
) in the page request as the view_path
parameter – for example, /admin/content
. As nearly everywhere in Drupal 8, a leading slash is included.
However, in \Drupal\views\Controller\ViewAjaxController::ajaxView()
there is then the following code dealing with this parameter:
$path = $request->request->get('view_path');
// …
$this->currentPath->setPath('/' . $path, $request);
As you can see, a second slash is prepended to the method argument. When other code then tries to get, e.g., the route match for that request, the router will throw a ResourceNotFoundException
with message No routes found for "//admin/content".
.
It seems there is normally just no code that does this (or otherwise uses the current path without left-trimming it first) during Views AJAX requests, so this bug could go undetected so far. But it does cause errors in combination with some contrib modules, like Facets.
Comment | File | Size | Author |
---|---|---|---|
#19 | 3000383-17.patch | 1.77 KB | anushrikumari |
#15 | 3000383-15.patch | 1.77 KB | ravi.shankar |
#8 | 3000383-fix_views_ajax_request_current_path-8-4-interdiff.txt | 620 bytes | mbovan |
#8 | 3000383-fix_views_ajax_request_current_path-8.patch | 1.77 KB | mbovan |
#4 | 3000383-4.test-only.patch | 1.77 KB | alexpott |
Comments
Comment #2
drunken monkeyThese would be patches to fix this – one simple one, just removing the code that prepends the slash, one that it safer regarding sites that already work around this problem by stripping the leading slash from the
view_path
parameter.Comment #3
dawehnerWriting a test for that would be really hard, I think. Personally I vote for the BC safe way, you never know.
Comment #4
alexpottI think we can write a test because have \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest which already does all the mocking necessary.
Also I think the test makes me wonder about the fix. Without the fix in place \Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest::testAjaxViewViewPathNoSlash() passes and the assertions are:
So I'm not entirely sure whether we should be adding a slash - or whether a version without a slash would work as expected anyway.
Comment #7
pcambraI've found this error recently while dealing with ajax in blocks embedded with twig, patch in #2 is really effective in combination with #2866386-4: Assert the view path is set correctly after second ajax request
Comment #8
mbovan CreditAttribution: mbovan at MD Systems GmbH commentedSince #2 was already committed in #2820347: Exposed filter reset redirects user to 404 page on AJAX view when placed as a block, and #2866386: Assert the view path is set correctly after second ajax request is about tests now, I am uploading a patch that fixes code style issues in #4.
Comment #9
mbovan CreditAttribution: mbovan at MD Systems GmbH commentedUpdated the issue title.
Comment #10
BerdirJust like in other related issues, more explicit test coverage doesn't hurt. I guess considering that the fix has been committed, the questions in #4 are also no longer relevant?
Comment #12
omarlopesinoThe same solution of the safe path from #2 is already commited in core, by this issue: https://www.drupal.org/project/drupal/issues/2820347.
Then this task should be closed, isn't it?
Comment #13
mbovan CreditAttribution: mbovan at MD Systems GmbH commented@mistermoper: #8 adds additional test coverage.
Comment #15
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedAdded patch for Drupal 8.9.x
Comment #18
init90Comment #19
anushrikumari CreditAttribution: anushrikumari at OpenSense Labs commentedpatch #15 doesn't apply to 9.2.x, so I rerolled for the same
Comment #20
drunken monkeyUpdated the issue summary.
Also reviewed the patch, which looks great, and verified that it indeed fails after reverting #2820347: Exposed filter reset redirects user to 404 page on AJAX view when placed as a block. Setting to RTBC accordingly.
Comment #23
catchCommitted/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!