Problem/Motivation
Follow up from #2073811-146: Add a url generator twig extension
Linking to a file itself is not trivial in Drupal, as you can swap out the logic behind it, see file_create_url()
Proposed resolution
Expose file_create_url() as file_url() in Twig because it's shorter, easier to remember and maps to the alter hook HOOK_file_url_alter(). Reason we shouldn't put both file_url() and file_create_url() into twig is because it would split the function usage and make consistency hard to accomplish as some would use the old version and some would use the shorter new one.
Remaining tasks
User interface changes
API changes
New twig function "file_url()" which works exactly like the PHP function file_create_url().
Beta phase evaluation
Issue category | Task, because this functionality would be useful for themers, and kinda required for sites using other ways to store their files. |
---|---|
Issue priority | Normal ... even I consider it as major, given that it both helps on peformance of real sites and blocks a critical from being finished. |
Disruption | No disruption at all, as it just adds an API |
Comment | File | Size | Author |
---|---|---|---|
#46 | interdiff.txt | 1.8 KB | joelpittet |
#46 | 2308187-file_url.patch | 4.55 KB | joelpittet |
#38 | twig-2308187-38.patch | 4.11 KB | lauriii |
#34 | twig-2308187-34.patch | 23.11 KB | lauriii |
#31 | interdiff.txt | 3.62 KB | lauriii |
Comments
Comment #1
dawehnerComment #2
Wim LeersPerhaps just
file_url()
?i.e.:
Comment #3
dawehnerLet's postpone this on the other issue.
Comment #4
star-szrComment #5
dawehner@Wim Leers
I sounds like a good idea to use a shorter name, thought for consistency I would argue that both would be a good idea.
Comment #6
joelpittetTypo /file_rl/file_url/
And I like the short one too, and would favour ditching the longer one to keep the namespace clean, though love to hear others take on that? Discussed a bit here already #2168231-7: Twig Functions needed in templates
Comment #7
er.pushpinderrana CreditAttribution: er.pushpinderrana commentedFixed TYPO.
Comment #8
dawehnerAs always we need some proper test.
Comment #9
star-szrComment #10
joelpittet@dawehner do we need tests to confirm that the
file_create_url()
works?We have a number of tests that seem to do this like
Drupal\file\Tests\DownloadTest::testPublicFileTransfer
checks that it works correctly.Twig is just passing the function to the template which it has tests for itself and we have tests for how it works, so I'm inclined to think we don't need tests.
Not sure we should go with two, and we need a comma at the end of the second line regardless. Can we settle on
file_url
?Comment #11
iMiksuComment #12
iMiksuWe also do tests with link generation so it's good to have tests for this.
It would make sense to have single function rather than having aliases (would be confusing?). In the other hand we could consider renaming whole
file_create_url()
function in the first place which would be then yet another topic.Comment #15
iMiksuComment #16
iMiksuComment #19
star-szrI apologize for the drive-by Dreditor…
Thanks @iMiksu! Please add a newline at EOF for this test Twig template. Otherwise looks good at a glance and is failing/passing appropriately.
Comment #20
lauriiiFixed some documentation in the tests. Saw that earlier and wanted to s/twig/Twig. Also fixed the missing newline.
Comment #21
joelpittet@iMiksu maybe we just leave the name the same (less docs to change;) ). I'd rather have one function than two because it will just split the people who are using it in contrib and core and just confuse.
I'd rather have it consistent yet annoyingly named and longer than necessary than have two.
We can't make those names changes easily in beta, so I'd rather look at that for a 8.1 or more likely 9.x change, but we can open that issue now and postpone;)
Comment #22
davidhernandezI agree with using one function. My initial opinion was to use file_create_url as it maps directly, making it much easier to track down. We also have precedence using that function name. I do like that file_url is shorter and friendlier, which is a plus in Twig templates.
If using the shorter name is a change that may not be possible in beta (Though I'm not sure why. Wouldn't this technically be an api addition?) then take the path of least resistance and use the longer one.
What docs need to be changed? Is it just the theming guide docs, or do we need to make a bunch of comment/api.drupal.org changes?
Comment #23
lauriiiChanged the latest patch use only file_create_url
Comment #25
iMiksuAccidental change in
core/modules/taxonomy/src/Tests/ThemeTest.php
file?Comment #26
lauriiiDont know how that ended up there :P
Comment #28
lauriiiWtf I messed up the patch and even RTBC'd by a mistake. Im such newbie!
Comment #29
joelpittet@lauriii and @iMiksu want to give @davidhernandez's suggestion a try and aim for file_url and fallback to file_create_url if there is too much strife? It would be an API addition if we just do the twig function change.
Comment #30
dawehnerDoes that basically means, that we provide both file_url() and file_create_url() as function in twig? We can still do that, but this issue is one of the child issues of a critical, so I'm curious how we can make progress here.
This issue
Comment #31
lauriiiWe discussed this on yesterdays Twig call and agreed that we want to have the shorter one aka
file_url
. We havehook_file_url_alter
so its aligned with that. It's also a lot more easier to remember and makes more sense.We don't want to have both to make it as clear as possible. If we would use both we would end up having both versions in contribs and core and people might get confused of whats the difference. Its also not good in terms of consistency.
We don't have verbs in other filters neither so it would be also inconsistent with that.
Comment #33
joelpittet@lauriii thanks for rolling that up.
Here is the reference we were talking about @see https://api.drupal.org/api/drupal/modules!system!system.api.php/function...
Is it just missing this file_url.html.twig?
Comment #34
lauriiiOmg! Im such newbie
Comment #36
davidhernandezYou are mixing your patches with other patches. You have the breadcrumb stuff in here.
Comment #37
joelpittetNo worries man, that is the most common thing on re-rolls to miss new files it seems, we all do it. For the mixing of patches, if you don't already, create a branch per issue. That usually mitigates that for me, before I used to just use
git diff --staged
but that was making too many mixing issues;) Thought branch per issue doesn't solve that issue always... I still do that when I get lazy and forget to create a new branch for a "quick fix"Comment #38
lauriiiSorry for messing around a bit :D
Comment #41
joelpittetGreat let's get this in play. Thank you!
Comment #42
joelpittetChange recorded added: https://www.drupal.org/node/2396439
Comment #43
alexpottThis is a bad test file. How about creating a file in the test. Then the test has no hidden dependencies?
Comment #44
davidhernandezAre you saying to dynamically create a file, or add/keep one in the test's directory to use for the test?
Comment #45
alexpott#44 either is fine by me. Just linking to an asset that might change is a bad idea.
Comment #46
joelpittetWill this do the trick?
Comment #47
dawehnerThank you for adressing the feedback.
Is there a reason why we add
file_url
but notfile_create_url
?Maybe just comment it in the IS.
Comment #48
joelpittet@dawehner added this to the issue summary:
That work?
Comment #49
joelpittetAlso it would be confusing for a while, why there are two functions doing the same thing... specially if someone decided to use the alternate on a project.
Comment #50
dawehnerAlright this is fair, but we also have the split in the php vs. twig naming.
Comment #51
davidhernandezCan we rename the php one? :)
Comment #52
dawehnerWell, that would be an api change
Comment #53
joelpittetYeah we are playing with what we have here... don't want an API change, but want a better name. The name has a side benefit of matching better to the hook that acts on it. So we have that improvement... Would be nice to do that PHP function name change for D9.
Comment #54
davidhernandezCouldn't we essentially alias it and mark the longer one deprecated to be removed later? ...but I digress.
Comment #57
star-szrComment #58
Wim LeersNitpicks that can be fixed on commit:
s/file url/file_url/
s/file_create_url/file_url/
Comment #59
alexpottCommitted 6df284e and pushed to 8.0.x. Thanks!
Thanks for adding the beta evaluation for to the issue summary.
Comment #61
Wim Leers#58 was not fixed on commit. Of course that's no disaster, but if those who worked on this patch would roll a patch to fix it in a follow-up issue, ping me for review!
Comment #62
davidhernandezPosted follow up. #2405823: Comment fix in Twig tests for file_url