Problem/Motivation
Around line 1389 in \Drupal\views\Plugin\views\field\FieldPluginBase::renderAsLink and around line 1266 in \Drupal\views\Plugin\views\field\FieldPluginBase::renderText
uses base_path() with code comments asset that this is what the UrlGenerator puts at the start of a URL.
However, checking the code in the UrlGenerator we see:
$base_url = $this->context->getBaseUrl();
Where context is the RequestContext
So the code comments and use of base_path() are not correct.
Proposed resolution
Inject the request context and use the base url from that since it correctly matches the UrlGenerator
Add a test to show the current bug.
Remaining tasks
doit
User interface changes
none
API changes
none
Data model changes
none
Comment | File | Size | Author |
---|---|---|---|
#42 | interdiff_40-42.txt | 18.83 KB | bruno.bicudo |
#42 | 2818185-42.patch | 27.53 KB | bruno.bicudo |
#40 | interdiff_36-40.txt | 4.12 KB | sourabhjain |
#40 | 2818185-40.patch | 11 KB | sourabhjain |
#37 | interdiff_2818185_34-36.txt | 1.15 KB | ankithashetty |
Comments
Comment #2
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedComment #3
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedPartial patch, NR for bot
Comment #5
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commenteddiscussed with dawehner in IRC
making the constructor options to avoid imposing a change on all subclasses (especially contrib) + fallback to getting via global service container like it does for renderer.
Comment #6
dawehner+1
Comment #8
mpdonadioI think all of those fails are from the unit tests. I guess the question is whether to just add the mock so the tests pass using a base path of /, or use a mock with a non-/ basepath and update tests to explicitly cover this. The later is probably better.
Comment #9
jcloys CreditAttribution: jcloys at Mediacurrent commentedFirst pass at adding missing dependency in container.
Comment #10
mpdonadio#9, nice patch (even though it was left at NW, the test passed). Totally forgot this test uses the ContainerBuilder.
There is another use of base_path() in that class related to links that should really go, and not increase the scope of the issue. It also allows us to get rid of the hack at the end of the test.
So the question remains, do we want to keep this as-is, or adjust the test to explicitly check for non-/ base paths?
Comment #12
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedYes, we need to remove the other pase_path() call in the class.
We should try to expand the test a little also - we can add a method to set the request context if desired.
Comment #13
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedSome possible added test coverage - some of this is a little wonky to do in just a unit test due to all the mocking - we can't really test the use case of a link field.
Comment #15
mpdonadioLooked like fails were b/c #2790685: FieldHandlersUpdateTest has random test fails, https://www.drupal.org/pift-ci-job/506881
Comment #16
mpdonadioThink I made that last change. w/o looking at the rest of the patch. For consistency sake, which is preferred `$context` or `$requestContext`? There doesn't seem to be a clear winner in core, but it looks like the later may be used more (which is why I made the change, but missed the others in the patch).
Comment #17
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedI don't think the naming in the test really matters in terms of consistency, but as far as I could see most other uses in core are just
$this->context
Comment #18
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedOpened up this somewhat related core bug #2819197: Generated url cache context is not correct for relative urls from the UrlGenerator
Comment #20
anavarreComment #21
dawehnerAn alternative pattern is to lazy fetch it in the constructor, if it wasn't passed long. Not sure what is better to be honest.
It is super nice to get rid of this hack.
Comment #30
jibranWe need to add bc warning and bc test for this change.
Comment #31
LendudeWe usually set this in the constructor for new dependencies now, so let's follow that pattern here too and we can probably remove this method.
Comment #33
beatrizrodriguesComment #34
beatrizrodriguesI did the reroll of the patch and I addressed comment #31 too.
Comment #35
beatrizrodriguesComment #36
ankithashettyFixed the custom command error in #34, thanks!
Comment #37
ankithashettySorry, messed up the interdiff file.
Comment #39
lucienchalom CreditAttribution: lucienchalom at CI&T commentedI run the phpcs and in the test files and the core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php returned this errors in the added lines:
also the test is returning getBaseUrl() as NULL in all the assertions related.
I will try to work on this but as I am not 100% sure how to fix it, if someone wants to step in is welcome.
Comment #40
sourabhjainFixed the issues which is mentioned in #39. Please review.
Comment #42
bruno.bicudoI reviewed #40, worked on the errors and also worked on what #39 pointed out.
Kindly review it :)