Currently there is no way to get the path or route name from Url object in twig and it makes some things a bit complicated.
For example I want to put webfont icons next to user menu links in the menu template but I have no way of knowing there the meni item link is pointing to since it holds only the Url object and I cannot retrieve any data out of it.
Therefore I think we should have functions in twig so we can work wirh Url objects.
This is the original issue I had: http://drupal.stackexchange.com/questions/177900/how-to-get-uri-from-url...
Comments
Comment #1
Anonymous (not verified) commentedivanjaros created an issue. See original summary.
Comment #2
dawehnerI'm pretty sure that
{{ url.routeName }}should actually work. Do you mind trying it out?Comment #3
Anonymous (not verified) commentedI tried that, and it is not working. I even tried Twig's attribute() but there is just no way to get the info.
Comment #4
dawehnerWhich examples do we talk about, so like which template ...
Comment #5
dawehnerOh right I guess the url object here might be a generatedUrl object
Comment #6
star-szrCan this be done in preprocess? To me it feels like an uncommon use case and therefore not sure we should add it to core.
Comment #7
joelpittetI don't think it's that un-common to want to drill into url objects and passing GeneratedUrl to the template seems a bit silly (because the bubblableMetataObject is weird and less useful to me). Having an actual Url object passed to the template would be ideal and @dawehner's original assertion would have been correct and problem solved.
Just throwing this out there yet way to late of an idea, instead of bubbling, just fire events containing the metadata needed. It seems a bit more straight forward. Or maybe just create a traight for this and add it to Url object may be less drastic change.
Anyways... rant over. @ivanjaros which template are you referring to?
Comment #8
dawehnerI'm a bit confused about the GeneratedUrl object in itself ... how does it even work, it has neither toString/__toString nor implements any of the other common interfaces?
Comment #9
Anonymous (not verified) commentedjoel: if you look at the link in the story description, you can see that I was trying to customize the menu template.
Comment #10
joelpittet@dawehner same boat, no idea. @ivanjaros can you confirm it's the GeneratedUrl you are getting?
by 'traight' i meant trait... reads the same:P
Comment #11
Anonymous (not verified) commentedjoel: it was Url. It might have changed now with the latest RC. I would have to update(im on RC2 now) and see, but I'm sure it was Url. I haven't heard of GeneratedUrl yet :D
Comment #12
dawehnerSo if you want to get the URL as string, you can use
{{ url.tostring }}Comment #13
joelpittetComment #29
smustgrave commentedThank you for sharing your idea for improving Drupal.
We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #30
smustgrave commentedThink this can be closed out after years of inactivity. There's also twig_tweak that may address this feature request. If still valid we can always re-open
Thanks all