Needs work
Project:
Drupal core
Version:
main
Component:
render system
Priority:
Normal
Category:
Task
Assigned:
Reporter:
Created:
15 Sep 2015 at 12:29 UTC
Updated:
30 Jan 2023 at 21:03 UTC
Jump to comment: Most recent, Most recent file
Let's be honest, we didn't managed to move everything into templates properly, so there are many examples, like in #2568609: Replace remaining !placeholder for Non-URL HTML outputs only in theme functions in system.admin.inc
where \Drupal::service('renderer') is the way to go, at least for now.
\Drupal::renderer()| Comment | File | Size | Author |
|---|---|---|---|
| #31 | 2568797-nr-bot.txt | 151 bytes | needs-review-queue-bot |
| #2 | add_drupal_renderer-2568797-2.patch | 501 bytes | lauriii |
Comments
Comment #2
lauriiiI was thinking of why we don't have this in \Drupal even though this is something that is (sadly) needed quite often.
Comment #3
dawehnerWim should have a look at it. We certainly should make it clear that its not what you should use but in places like hook_help() it is certainly better to do so.
Comment #6
wim leersDevil's advocate:
Comment #8
wim leersComment #9
markhalliwellBecause this doesn't return the proper class/interface for allowing IDEs for proper auto completion.
IMO, this is more of a "helper/wrapper" method than anything so it links the class.
Consider the following:
Opposed to just doing:
This is more about DX IMO and reducing LOC and readability for when this actually is needed.
Comment #10
catchI just needed \Drupal::renderer()->addCacheableDependency($build, $foo); and this would have been handy for that. Unless there's another way to do that without the service, but couldn't see one.
Comment #11
markhalliwellAlso, there are plenty of places where this can be useful, especially in themes btw, where we don't have the ability to simply inject the renderer, let alone the container. To just say "don't use it" isn't right.
Instead, it should be documented for it to only be used in places where there the render service hasn't been injected, not able to be injected nor has immediate access to the container.
So, setting back to CNW for documentation purposes.
Comment #12
dawehnerGiven that the theme system and the render system are somehow similar, it might be worth to provide the renderer directly, maybe, as part of preprocess for example.
$variables['renderer']or something like that.Comment #13
markhalliwellThis would technically allow twig templates access to this as well. Considering that this is a class and not a string/render array, how would twig handle this if it's used. No, we already have the
|renderfilter for twig. The variables array is meant for the consumption of things in twig templates, not to provide classes like this. I'd rather just have the helper method\Drupal::rendererfor when it's needed in PHP.Comment #14
dawehnerWell, I was primarily thinking of preprocess functions. For every other usecase though, like in modules, you are doing something wrong, IMHO, when you use
\Drupalitself.Comment #15
markhalliwellI understand, but polluting the
$variablesarray with a class like this is a no go. Variables are meant for consumption, not utilities. All themes can do is call\Drupalhelper methods and I'm fine with that, do it all the time.Yes, I agree. However there are use cases in modules too: alter hooks (which is what @catch is referring to above).
That's why I set this back to CNW because there needs to be better documentation around "when" it's appropriate to use this.
Comment #16
dawehnerYeah, sure, I'm just brainstorming here.
For the usecase of tests/controllers for example we could provide a
$this->renderer()helper method.Comment #24
sam152 commentedI agree this would be a useful addition.
I also think it's potentially safer than not including it. By returning something type hinted, IDEs make it really obvious the renderer actually has a bunch of different methods, which I think encourages digging a bit deeper to understand the nuances between them and perhaps problems with rendering early in general.
Out of interest sake, I scanned a recent check-out of all D8 contrib projects and found that 'renderer' was the most frequently accessed service using the
\Drupal::servicemethod.The top 5 were:
Comment #31
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.