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.
Problem/Motivation
drupal_render()
was marked as deprecated, though its still called in quite some places.
Proposed resolution
All the child issues are closed as duplicates of this issue. Create a single patch for this issue instead of many sub-issues.
- Avoid rendering manually by letting the template who is printing the variable render it.
- Inject the renderer service into service, which uses drupal_render()
- Use \Drupal::service('renderer')->render() for old prodecural code.
- Replace drupal_render_root() with $renderer->renderRoot().
Remaining tasks
In 8.1.x, begin with a single patch to replace drupal_render()
with use of the renderer service. Rest TBD.
User interface changes
None
API changes
None
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedI'm getting a lot of hits on
dupal_render()
(393 in code, 538 total). Shouldn't we make this a meta issue?Also, the comment above the function reads:
Should the version of this issue be updated?
Comment #2
joelpittet@pjonckiere I'm not sure a meta as I hope we can do it all here, but maybe... Tagged with Twig so it shows up on our http://drupaltwig.org/
Also the first step is to avoid calling it all together preventing early rendering, then inject, then procedural service call.
I've updated the IS and added a script to help find them all.
@pjonckiere is this similar to what you used?
List:
Count
Comment #3
joelpittetA few will be cleaned up by means of #2348381: [META-0 theme functions left] Convert/refactor core theme functions, and lots are tests.
It may be worth injecting the renderer onto WebTestBase for all the tests to call $this->render();
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedI let PhpStorm do the searching for me, but it comes down to the same as your regex command.
I did search on the whole project, and not just core, so there are slightly less hits then what I mentioned. But it still might be somewhat early to start working on this.
Comment #5
star-szr@joelpittet in #3: The render thingy exists today as a trait, see #2257519: Move content assertion methods into a trait, so DUTB can consume it, too (\Drupal\simpletest\AssertContentTrait), available in WebTestBase and KernelTestBase.
If we try to evaluate these and remove early rendering, this is probably going to stall out. If we want to replace the drupal_render() calls we should make it scriptable. See also "how to meta your metas".
Comment #6
cilefen CreditAttribution: cilefen commentedWe could split this up the way we did in #1811638: [meta] Clean-up Test members - ensure property definition and use of camelCase naming convention.
Comment #7
cilefen CreditAttribution: cilefen commentedI think that the need to inject the service in some cases but not in others automatically makes this a meta because it isn't scriptable. A human has to review each replacement.
Comment #8
cilefen CreditAttribution: cilefen commentedComment #9
heddnComment #10
heddnComment #11
cilefen CreditAttribution: cilefen commentedComment #12
heddnComment #13
cilefen CreditAttribution: cilefen commentedComment #14
cilefen CreditAttribution: cilefen commentedComment #15
nghai CreditAttribution: nghai commentedComment #16
Noe_ CreditAttribution: Noe_ at Devhouse Spindle commentedComment #17
Mile23#2471913: Replace drupal_render calls in core/modules/simpletest
Comment #18
heddnComment #19
edysmpComment #20
Ada Hernandez CreditAttribution: Ada Hernandez commentedComment #21
cilefen CreditAttribution: cilefen commentedPlease make sure new child issues are set to "Active" unless there is a patch posted.
Comment #22
keopxComment #23
keopxComment #24
keopxComment #25
Mile23Should we be replacing
drupal_render_root()
as well?Comment #26
Mile23Comment #27
mitrpaka CreditAttribution: mitrpaka commentedComment #28
Mile23#2473393: Replace drupal_render calls in core/includes
Comment #29
Fabianx CreditAttribution: Fabianx for Acquia commented#25: drupal_render_root() is replaced with $renderer->renderRoot().
Comment #30
Wim LeersHow was I not yet following this issue?!?
Comment #31
Crell CreditAttribution: Crell at Palantir.net commentedWim: The same reason I wasn't. Welcome to my life. :-) (subscribe!)
Comment #32
willzyx CreditAttribution: willzyx commentedComment #33
willzyx CreditAttribution: willzyx commentedComment #34
willzyx CreditAttribution: willzyx commentedComment #35
willzyx CreditAttribution: willzyx commentedComment #36
willzyx CreditAttribution: willzyx commentedComment #37
mitrpaka CreditAttribution: mitrpaka commentedComment #38
willzyx CreditAttribution: willzyx commentedComment #39
xjmThanks everyone for your work here.
drupal_render()
was not deprecated before the beta, and is marked for removal by 9.0.0. Per the allowed beta changes policy, only the removal of functions deprecated for removal 8.0.0 is a prioritized change. So the normal task of removing uses ofdrupal_render()
isn't in scope during this phase of the release cycle, since we have to keepdrupal_render()
around until 9.x anyway.It would be okay to resume simple conversions that preserve BC after 8.0.0, so postponing this to 8.1.x.
It would be great to have everyone's help instead in #2348381: [META-0 theme functions left] Convert/refactor core theme functions and #2205673: [META] Remove all @deprecated functions marked "remove before 8.0". Also see the other open Twig issues:
https://www.drupal.org/project/issues/search/drupal?project_issue_follow...
Comment #40
star-szrAgreed, thanks @xjm.
Comment #41
xjmAlso, discussed with @alexpott and @webchick. When we do convert these, we should do it in a single patch rather than many small issues since it's actually a single conceptual change. So let's not file any additional child issues, and re-scope this to not be a meta.
Comment #42
Wim LeersTagging DX because it'd be better for DX if zero calls to
drupal_render()
remained: then contrib/custom code will be less tempted to calldrupal_render()
, thus encouraging better code, thus leading to a more consistent overall (core + contrib + custom) D8 code base, thus a better DX.Comment #43
Fabianx CreditAttribution: Fabianx for Acquia commentedWe can still justify some of those conversions in D8 with performance however.
- If something is called on every page and uses drupal_render() within an injectable service => convert it
- If something is not called on every page but just on some obscure admin page or within some legacy function => leave it
So we should split at least one performance issue for conversions that have a performance impact.
(and yes the indirection of drupal_render -> \Drupal::service('renderer') -> $container->get('renderer') -> $container->getRenderer() -> render() does matter.)
Comment #44
xjmRemoving the novice tags as it's not good to have these on issues that are targeted for 8.1.x or that do not have consensus in one way or another.
If there is an actual non-negligible performance gain, let's demonstrate that where it happens and file related issues. Thanks!
Edit: Agreed that it is better DX for sure; just that alone isn't enough to justify a change at this point.
Comment #45
hass CreditAttribution: hass commentedAre you guys aware that render - both service and old render function is completly broken? #2474865: drupal_render() and service no longer attach
Comment #46
Mile23Updating issue summary.
Comment #47
Mile23Some items already underway:
#2529438: Inject renderer service into ThemeManager, disuse drupal_render()
#2017549: Remove usage of drupal_render and theme functions in Views/Views UI
#2428925: Create a trait to add in the renderer service
Comment #48
Mile23Oops missed these.
Comment #50
Mile23#2704871: Replace usages of deprecated method drupal_render()
Comment #57
Berdirdrupal_render() happened in #2704871: Replace usages of deprecated method drupal_render(), drupal_render_root() will happen in #2818677: Replace usages of deprecated method drupal_render_root()