Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Last updated comment #11
So. Um. What?
D7
$items['create_account'] = l(t('Create new account'), 'user/register', array(
'attributes' => array(
'title' => t('Create a new user account.'),
'class' => array('create-account-link'),
),
));
D8
$items['create_account'] = Drupal::linkGenerator()->generate(t('Create new account'), 'user_register', array(), array(
'attributes' => array(
'title' => t('Create a new user account.'),
'class' => array('create-account-link'),
),
));
Replacing l()
with Drupal::linkGenerator()->generate()
? Seriously?
We need to replace this with something that doesn't expose underlying API details to normal developers. Suggestion: Drupal::link(). Otherwise, expect developers to go back to hard-coding <a>
tags.
Related Issues
#2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes
#2073811: Add a url generator twig extension
#2073813: Add a UrlGenerator helper to FormBase and ControllerBase
Comments
Comment #1
tim.plunkettThere is an issue to add a #route_name for #type => link
Comment #2
pwolanin CreditAttribution: pwolanin commented@tim.plunkett - #route_name is done now in the link generator patch.
re: DX see: #2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes - I might consider this duplicate?
basically we just need to bikeshed Drupal::l() or Drupal::link()
Comment #3
tim.plunkettGreat, then
And yes, I'd consider this a dupe, but leaving that to @webchick.
Comment #4
pwolanin CreditAttribution: pwolanin commentedAlso for themers:
#2073811: Add a url generator twig extension
Comment #5
pwolanin CreditAttribution: pwolanin commentedAlso, I would be totally fine if we tweaked l() and url() to take a path OR an array of route name and parameters, though many would barf...
Comment #6
msonnabaum CreditAttribution: msonnabaum commentedI very much agree with tim's approach. We should take a hard look at where these functions are used, because I dont get what the use case is for having these is outside the theme layer.
Comment #7
webchickSorry, I didn't see #2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes before I posted this. But now there is more discussion here, so I'm unsure what to do. I think maybe closing the other one as a dupe and moving the patches over here (but I don't want to do that myself because Dreditor will then mis-attribute them to me). Else, we can close this one and re-write the issue summary of the other and escalate to critical (I don't have time to do this right now, but can sometime over the weekend if no one beats me to it).
Comment #8
thedavidmeister CreditAttribution: thedavidmeister commentedThere will be a perf hit using render arrays for all links instead of sending them directly to l()/generator as there are extra function calls and processing involved in drupal_render() and drupal_pre_render_link().
Comment #9
msonnabaum CreditAttribution: msonnabaum commentedThat is not a performance hit worth considering.
Comment #10
pwolanin CreditAttribution: pwolanin commentedThe biggest use outside the theme layer is for translated text and help text where we frequently call url() now.
Other than that, I agree they should rarely need to be called directly.
@webchick - the only thing to decide really is the method name - I think we should leave the other issue open for discussion of the patch to add Drupal::l(), and we should either close this one or make it a meta.
Comment #11
pwolanin CreditAttribution: pwolanin commentedchange title
Comment #11.0
pwolanin CreditAttribution: pwolanin commented...
Comment #12
tim.plunkettThis was left open until the issue summary of #2078285: Add short-cut methods to the \Drupal class for generating URLs and links from routes was rewritten, but that already got committed.
Comment #13
thedavidmeister CreditAttribution: thedavidmeister commentedIf you want to discuss moving link generation over to render arrays, #2046881: [meta] Avoid "early rendered" strings where beneficial to do so, build structured data to pass to drupal_render() once instead
Comment #13.0
thedavidmeister CreditAttribution: thedavidmeister commentedUpdated issue summary.
Comment #14
dawehnerThese days this would look like the following:
Comment #15
dawehnerBut this could be certainly moved to