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

Issue fork drupal-2794261

Command icon 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:

Comments

jurgenhaas created an issue. See original summary.

mile23’s picture

Title: render() function in common.inc should be deprecated » Deprecate render() function in common.inc
Version: 8.1.x-dev » 8.3.x-dev
Issue summary: View changes
Issue tags: +@deprecated, +Kill includes

We'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.

pguillard’s picture

Status: Active » Needs review
StatusFileSize
new511 bytes

I guess this is it ?

chi’s picture

Status: Needs review » Needs work

there is no render context and that throws exceptions

Could you please elaborate on this? Does "renderer" service itself work fine without this context?

+++ b/core/includes/common.inc
@@ -918,6 +918,9 @@ function drupal_render_children(&$element, $children_keys = NULL) {
+ * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use

Could be a bit longer under 80 characters limit.

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.

pguillard’s picture

Status: Needs work » Needs review
StatusFileSize
new511 bytes
new473 bytes

Applied suggestion at #4

borisson_’s picture

Status: Needs review » Needs work
+++ b/core/includes/common.inc
@@ -919,8 +919,8 @@
+ * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use the

/s/8.3.0/8.5.0/

This should also use a @trigger_error in the code and the renderplain suggestion in the IS.

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.

andypost’s picture

Issue tags: +Needs change record
oleksiy’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new2.9 KB
andypost’s picture

Issue tags: +Needs change record
oleksiy’s picture

Issue tags: -Needs change record
StatusFileSize
new2.96 KB

Status: Needs review » Needs work

The last submitted patch, 11: deprecate_render_function-2794261-11.patch, failed testing. View results

oleksiy’s picture

Status: Needs work » Needs review
StatusFileSize
new5.48 KB
new5.87 KB

renderPlain() has been replaced with new method renderElement() added on the Renderer service.

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.

mile23’s picture

Status: Needs review » Needs work
  1. +++ b/core/includes/common.inc
    @@ -929,26 +929,15 @@ function drupal_render_children(&$element, $children_keys = NULL) {
    + * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use
    

    Should be 8.7.x.

  2. +++ b/core/includes/common.inc
    @@ -929,26 +929,15 @@ function drupal_render_children(&$element, $children_keys = NULL) {
    + * \Drupal\Core\Render\Renderer::renderElement() instead.
    ...
    +  @trigger_error('render() is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\Renderer::renderElement() instead. See https://www.drupal.org/node/2939099.', E_USER_DEPRECATED);
    

    The replacement is \Drupal::service('renderer')->renderElement(), just like in the function we're deprecating.

sorlov’s picture

Applied fixes at #17

sorlov’s picture

Status: Needs work » Needs review
andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Needs work for deprecation tests and workaround for show()

  1. +++ b/core/includes/common.inc
    @@ -929,26 +929,15 @@ function drupal_render_children(&$element, $children_keys = NULL) {
    - * @see show()
    - * @see hide()
    
    +++ b/core/lib/Drupal/Core/Render/Renderer.php
    @@ -176,6 +176,28 @@ public function renderPlaceholder($placeholder, array $elements) {
    +      show($element);
    
    +++ b/core/lib/Drupal/Core/Render/RendererInterface.php
    @@ -79,6 +79,25 @@ public function renderPlain(&$elements);
    +   * @see show()
    +   * @see hide()
    

    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

  2. +++ b/core/modules/file/file.field.inc
    @@ -78,12 +78,12 @@ function template_preprocess_file_widget_multiple(&$variables) {
    +        'data' => \Drupal::service('renderer')->renderElement($widget['display']),
    ...
    +    $weight = \Drupal::service('renderer')->renderElement($widget['_weight']);
    

    they used in one method so \Drupal::service('renderer') should be called once using local variable

mile23’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +Needs followup
StatusFileSize
new59.77 KB
new55.47 KB

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

andypost’s picture

@Mile23 you commited composer changes to patch

mile23’s picture

StatusFileSize
new9.69 KB

Oops...

Here it is minus changes to composer.lock.

berdir’s picture

+++ b/core/lib/Drupal/Core/Render/RendererInterface.php
@@ -79,6 +79,25 @@ public function renderPlain(&$elements);
+  /**
+   * Renders an element.
+   *
+   * This method renders an element. The top level element is shown with show()
+   * before rendering, so it will always be rendered even if hide() had been
+   * previously used on it.
+   *
+   * @param $element
+   *   The element to be rendered.
+   *
+   * @return mixed
+   *   The rendered element.
+   *
+   * @see \Drupal\Core\Render\RendererInterface
+   * @see show()
+   * @see hide()
+   */
+  public function renderElement($element);

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

wim leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

EXCITING to see these old bits getting deprecated at last! :) 🤘 🎉 Thank you! 🙏


If not, we could consider deprecating it without replacement, together with the related show()/hide().

+1

We should make it simpler or at least have a smaller public API surface.

At least we should have a comment from Wim here as well as he mostly designed/wrote the Renderer service.

I hardly designed it, I just helped move it out of an .inc file into a service.


The issue summary says:

render() is no longer called very often
[…]
A better replacement would be \Drupal::service('renderer')->renderPlain($renderArray); or maybe render could be rewritten to call exactly that?

… 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?

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.

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.

andypost’s picture

StatusFileSize
new3.38 KB
new9.74 KB

There's no new usage added so just re-roll for 9.3

andypost’s picture

StatusFileSize
new1.85 KB
new9.75 KB

Fix CS

andypost’s picture

andypost’s picture

longwave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs followup, -Needs issue summary update
StatusFileSize
new8.01 KB
new7.95 KB
  • Removed renderElement() and put render() back to how it was
  • Refactored template_preprocess_file_widget_multiple() to not need render()
  • Refactored shortcut_preprocess_page_title() to do what it says and render the same way template_preprocess_html() does
  • Converted EntityField to just use RenderInterface::render()

Removing "Needs followup" as #2258355: Deprecate hide() and show() exists, and updated issue summary a bit.

Status: Needs review » Needs work

The last submitted patch, 35: 2794261-35.patch, failed testing. View results

andypost’s picture

++ to approach - much cleaner

longwave’s picture

Status: Needs work » Needs review

Fixed test, converted to merge request.

longwave’s picture

Updated change record to match new approach.

paulocs’s picture

Status: Needs review » Reviewed & tested by the community

MR 950 looks 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. to The 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_test to 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.

voleger made their first commit to this issue’s fork.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed e1ab113 and pushed to 9.3.x. Thanks!

  • catch committed e1ab113 on 9.3.x
    Issue #2794261 by longwave, andypost, Oleksiy, Mile23, pguillard, sorlov...
catch’s picture

Published the CR.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.