Problem/Motivation
render() is no longer called very often, in fact Drupal core only calls it 4 times. The problem with that function is that in certain circumstances, i.e. when using Drush or DrupalConsole, there is no render context and that throws exceptions.
I was hit by that the second time today where a contrib developer unintentionally used that function which was perfectly right but with Drush I got errors all over the place.
A better replacement would be \Drupal::service('renderer')->renderPlain($renderArray); or maybe render could be rewritten to call exactly that?
My concern is that contrib developers not being aware of the problem could introduce more usages of that function causing trouble some time down the road.
Proposed resolution
Mark render() as deprecated in Drupal 9.3.x for removal before 10.0.0 release.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|
Issue fork drupal-2794261
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 2794261-deprecate-render-function
changes, plain diff MR !950
Comments
Comment #2
mile23We'd need to mark it deprecated in 8.3.x because 8.2.x is in beta.
The process for this to mark it as deprecated, and then follow-up to remove usages and finally remove the function.
Comment #3
pguillard commentedI guess this is it ?
Comment #4
chi commentedCould you please elaborate on this? Does "renderer" service itself work fine without this context?
Could be a bit longer under 80 characters limit.
Comment #7
pguillard commentedApplied suggestion at #4
Comment #8
borisson_/s/8.3.0/8.5.0/
This should also use a @trigger_error in the code and the renderplain suggestion in the IS.
Comment #10
andypostComment #11
oleksiyComment #12
andypostComment #13
oleksiyСhange record https://www.drupal.org/node/2939099
Comment #15
oleksiyrenderPlain() has been replaced with new method renderElement() added on the Renderer service.
Comment #17
mile23Should be 8.7.x.
The replacement is \Drupal::service('renderer')->renderElement(), just like in the function we're deprecating.
Comment #18
sorlov commentedApplied fixes at #17
Comment #19
sorlov commentedComment #20
andypostNeeds work for deprecation tests and workaround for
show()both should be replaced, we can't use functional code in classes (at least `show()` needs protected method wrapper
btw there was issue about show/hide refactor as well
they used in one method so \Drupal::service('renderer') should be called once using local variable
Comment #21
mile23Not sure if there's an issue to deprecate show/hide, so adding Needs followup.
show() and hide() are out of scope here, so reverting those changes.
Added a test. And a good thing, too. See if you can figure out why.
Comment #22
andypost@Mile23 you commited composer changes to patch
Comment #23
mile23Oops...
Here it is minus changes to composer.lock.
Comment #24
berdirI'm not convinced about adding this new method, we already have several methods on this service whose differences aren't obvious and renderElement() doesn't actually explain what makes it special.
The history of this function as well as the related show/hide is php template files, they were introduced in 7.x as the predecessor of the twig "|without('foo')" thingy, whatever that is called exactly. The extra checks that are included that are checking for it not being an array and so on, because we didn't want to put that burden on theme/frontend developers.
Considering that this use case doesn't exist anymore (in core anyway), I'm not sure we need an 1:1 replacement for it.
What happens if we replace the few calls we have in core with a regular render()? does anything actually fail?
If not, we could consider deprecating it without replacement, together with the related show()/hide(). show() is used 2x in core, once inside render() and once inside a file field preprocess function that is doing a lot of trickery anyway, unsetting #printed ourself wouldn't hurt there. Same for hide(), that's used in only 2 different places as well.
And considering that, it might actually not be stupid to deal with those 3 functions at once, especially if the solution is to just deprecate them without replacement as we just need to update the usages, @trigger_error() and test for that.
At least we should have a comment from Wim here as well as he mostly designed/wrote the Renderer service.
Comment #25
wim leersEXCITING to see these old bits getting deprecated at last! :) 🤘 🎉 Thank you! 🙏
+1
We should make it simpler or at least have a smaller public API surface.
I hardly designed it, I just helped move it out of an
.incfile into a service.The issue summary says:
… but then we end up not reusing that existing API and instead are expanding the public API that
Renderer(Interface)provides. The reason for this is not yet explained in the issue summary. It's also not explained in the change record: https://www.drupal.org/node/2939099.It looks like #15 introduced this without any explanation?
Comment #31
andypostThere's no new usage added so just re-roll for 9.3
Comment #32
andypostFix CS
Comment #33
andypostComment #34
andypostComment #35
longwaveRemoving "Needs followup" as #2258355: Deprecate hide() and show() exists, and updated issue summary a bit.
Comment #37
andypost++ to approach - much cleaner
Comment #39
longwaveFixed test, converted to merge request.
Comment #40
longwaveUpdated change record to match new approach.
Comment #41
paulocsMR 950looks good to me.The code looks clean and there is test for the method deprecated.
I updated the change record from
The render() function is deprecated.toThe render() function in common.inc is deprecated.The only thing that I see that could be changed but I think its a nit-pick is that the test module could be renamed for
render_deprecation_testto keep consistency as all the other test modules have the word "test" in their name.As I guess its a nit-pick, I'm moving it to RTBC.
Comment #43
catchCommitted e1ab113 and pushed to 9.3.x. Thanks!
Comment #45
catchPublished the CR.