Problem/Motivation

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.

Proposed resolution

  • Add \Drupal::renderer()
  • Make it clear in its docs, that you better not use it directly, unless you know this is the better choice for now.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

dawehner created an issue. See original summary.

lauriii’s picture

Status: Active » Needs review
StatusFileSize
new501 bytes

I was thinking of why we don't have this in \Drupal even though this is something that is (sadly) needed quite often.

dawehner’s picture

Assigned: Unassigned » wim leers

Wim 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.

Status: Needs review » Needs work

The last submitted patch, 2: add_drupal_renderer-2568797-2.patch, failed testing.

Status: Needs work » Needs review
wim leers’s picture

Devil's advocate: Why is \Drupal::service('renderer') not acceptable?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Status: Needs review » Closed (outdated)
markhalliwell’s picture

Status: Closed (outdated) » Needs review

Devil's advocate: Why is \Drupal::service('renderer') not acceptable?

Because 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:

/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$output = $renderer->render($build);

Opposed to just doing:

$output = \Drupal::renderer()->render($build);

This is more about DX IMO and reducing LOC and readability for when this actually is needed.

catch’s picture

I 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.

markhalliwell’s picture

Title: Add \Drupal::renderer() but document to better not use it. » Add \Drupal::renderer() but document how and when it should be used
Status: Needs review » Needs work

Also, 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.

dawehner’s picture

Also, 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.

Given 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.

markhalliwell’s picture

$variables['renderer']

This 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 |render filter 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::renderer for when it's needed in PHP.

dawehner’s picture

Well, I was primarily thinking of preprocess functions. For every other usecase though, like in modules, you are doing something wrong, IMHO, when you use \Drupal itself.

markhalliwell’s picture

Version: 8.1.x-dev » 8.2.x-dev

Well, I was primarily thinking of preprocess functions.

I understand, but polluting the $variables array with a class like this is a no go. Variables are meant for consumption, not utilities. All themes can do is call \Drupal helper methods and I'm fine with that, do it all the time.

For every other usecase though, like in modules, you are doing something wrong, IMHO, when you use \Drupal itself.

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.

dawehner’s picture

Yeah, sure, I'm just brainstorming here.

For the usecase of tests/controllers for example we could provide a $this->renderer() helper method.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sam152’s picture

Status: Needs work » Needs review

I 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::service method.

The top 5 were:

  • 1006 renderer
  • 706 config.factory
  • 661 date.formatter
  • 304 path.current
  • 267 plugin.manager.filter

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new151 bytes

The 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.