Closed (fixed)
Project:
Drupal core
Version:
8.8.x-dev
Component:
base system
Priority:
Normal
Category:
Task
Assigned:
Issue tags:
Reporter:
Created:
12 Apr 2019 at 20:23 UTC
Updated:
7 Aug 2019 at 15:49 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
nlisgo commentedThis is a first pass. Some of these instances will have to be replaced with DI specific to the class rather than just drawing on the service containers. Also there will be some instances where we should be using service containers instead of invoking
\Drupal\Core\Link::fromTextAndUrl.Feeback required. Happy to continue on this issue.
Comment #4
berdirThe patch seems to contain a lot of unrelated changes, diff/rebased against wrong/old branch?
Comment #5
mikelutzComment #6
pguillard commentedI tried to extract the best parts from #2, plus some reroll as code has changed already.
Comment #7
pguillard commentedComment #8
eduardo morales albertiRerroll patch to apply and change Link::fromTextAndUrl('text', $url) to \Drupal::service('link_generator')->generate('text', $url).
Comment #9
eduardo morales albertiRerroll again because last patch was created against wrong commit.
Comment #10
eduardo morales albertiRerroll patch because replacement of deprecated function was wrong.
Comment #12
eduardo morales albertiFix service link generator call.
Comment #13
eduardo morales albertiRerrol patch to pass tests.
Not allways is posible to use \Drupal::service('link_generator')->generate
Comment #14
eduardo morales albertiRerroll to fix some additional errors.
Comment #15
eduardo morales albertiFix coding standards Unused use statement Link class.
Comment #16
eduardo morales albertiChange issue status.
Comment #17
berdir> Rerroll patch to apply and change Link::fromTextAndUrl('text', $url) to \Drupal::service('link_generator')->generate('text', $url).
Why? Link::fromTextAndUrl() is not deprecated, that is the recommended replacement?
Comment #18
sathish.redcrackle commentedTested on Drupal 8.8.x and there is no l() on core files except on test files.
Comment #19
eduardo morales albertiIt is because seeing the class Link https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Link.php/... The method to string calls directly to the link generator. So I do not see the advantage of using the Link::fromTextAndUrl() in the most cases.
Comment #20
berdirThe Link class is the designed replacement of \Drupal::l(). at least for all the places that can't (easily) be injected. We also very recently removed all usages of the deprecated LinkGeneratorTrait, which I think you rely on in a lot of places, that won't work anymore now.
Comment #21
berdirComment #20 sounded a bit harsh, wasn't meant like that sorry. I still think we should stick with Link, that's what we did elsewhere, but lets get a second opinion on that.
Comment #22
mikelutzThis is tricky, because Link::fromTextAndUrl() returns a Link object, while \Drupal::l() returns a GeneratedLink object, so trying to get all the right ->toStrings() and ->toRenderables() is tough. Here's a first attempt, that will need iteration. No interdiff because I started from scratch using Link instead of the link generator.
Comment #23
mikelutz:-/
Comment #24
mikelutzComment #26
mikelutzSo it seems I was a little confused. I had thought that Link::toString() was returning a, you know, string. I didn't realize that that was a generated link, so in most cases here, ->toString() is correct. Lets see if this gets us closer.
Comment #28
mikelutzWell, that went the wrong direction. :-/
Comment #30
berdirafaik there should be an empty line between @deprecated and @see. And @see is usually last in the docblock. Not sure about the code example here. I think we could just explicitly say Use ...Link::fromTextAndUrl() in the message and drop that, it's really not that much longer.
I think the recommended way would be Url::fromRoute(), not sure if we want to touch that here. Looks like there are quite a few examples like that.
Comment #31
mikelutzAddressing feedback, fixing last test.
Comment #32
berdirSome comments/notes, only 1-2 are actionable.
Hm, not sure if you went through multiple changes here, but wouldn't it be easier to keep the (string) cast? Drupal:l() and toString() actually have the exact same return value, so doesn't seem necessary to change that here?
We're changing the form structure here, could theoretically go for a smaller change but it does seem nicer like this.
As a non native speaker, all uppercase here seems a bit strange? Below we we just say Test \Drupal\Core\Url, so we could just use the Link class name here too?
On the other side, later on we use "link generator" again, then lowercase though.
is there a specific reason for using the service here and not everywhere else? Above we also say link generator in the comment and use Link::..
this used getGeneratedLink() before, I guess that means it works due to \Drupal\KernelTests\KernelTestBase::assertEquals(), both of these changes were committed within a week, so I suspect that was done before the string cast was added.
I guess that is there to stay and not deprecated, so seems fine.
Comment #33
mikelutz1) I did go through a few iterations, no objections to going back to the typecase. fixed.
2) In the documentation, ->toRenderable() is recommended over ->toString() when possible, so I did switch over to it in the cases where the link was directly used in a renderable array.
3) I lowercased link generator. The reason I say it tests the link generator and not Link is because as far as I can tell the filtering is done in the Link->toString() method by way of the link generator service, that's what the assertion message says, and what it looks like from the code.
4) Again, I say link generator in the comment because the processing that is being tested is done in the link generator ->generate() through the Link->toString() method. It said "Test the link generator" before, despite accessing it through the Drupal::l method, and it's still testing the generator, even if it's accessing it through Link::toString() now.
I use the link_generator service directly when the text is a renderable array. The documentation for Link:: methods require $text to be a string. (I do use Link::fromTextAndUrl with TranslateableMarkup and FormatableMarkup objects despite the documentation, but at least those are castable to string. I couldn't bring myself to pass in an actual array.
5) yeah, weak typing on the assertion means the getGeneratedLink() isn't needed. I didn't see a reason to add it if there was weak typing in the assertion.
Comment #34
berdir4. Yeah, fair enough, I would say that the current description on Link::fromTextAndUrl() is a documentation bug, there's no reason that it should support fewer things than LinkGenerator::generate(). I could almost see you working on that test and pondering just far you think you can go against the documentation ;) Should we create a follow-up for that?
5. It's not just weak typing, though, if you comment out our own explicit string type cast in KernelTestBase, it actually fails like this:
So assertEquals() behaves somewhat unexpected here, as long as one of the arguments is a string, it is compared as a string if the other can be cast to a string. But if both are objects, they are compared as objects and they are different objects that can both be cast to the same string. Which actually makes sense, why should it bother to cast to string of it receives two objects. But that's fine, I didn't expect a change here, just wanted to write down my findings on why we used to do that and why it is no longer necessary.
Comment #35
alexpottI'm not 100% sure about all the render array changes. On one hand the change is clearly inline with the intentions of how
toRenderable()is to be used and is correct. On the other it's kinda unexpected. That said render arrays are considered internal API as per https://www.drupal.org/core/d8-bc-policy - so I'm tentatively +1. I think a release manager should make this call.Comment #36
berdir> I'm not 100% sure about all the render array changes.
"all the" sounds like more than it is I think, if I saw that correctly, there are 3 non-test replacements, field_help() which I can't imagine anyone would try to alter, then the field storage list builder, that.. maybe? and the locale status in the language list, also an unlikely case for someone to customize?
But yeah, if a release manager feels unsure about that, it's a small thing to change it back.
Comment #37
catchThe render array changes are fine with me for a minor release, so untagging. Didn't yet review the whole patch so leaving RTBC.
Comment #38
alexpottCommitted ae248f2 and pushed to 8.8.x. Thanks!