Follow-up to #2704871: Replace usages of deprecated method drupal_render()

Problem/Motivation

In #2729597: [meta] Replace \Drupal with injected services where appropriate in core we generally decided to convert usages of \Drupal methods to inversion-of-control injection, by method.

This issue converts usages of \Drupal::service('renderer') to injection.

In some places in core, this can lead to circular dependencies, such as in #2529438: Inject renderer service into ThemeManager, disuse drupal_render()

Proposed resolution

Convert usages of \Drupal::service('renderer') to use IoC injection where applicable.

Exclude theme systems, which are handled in #2529438: Inject renderer service into ThemeManager, disuse drupal_render()

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork drupal-2834982

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

Mile23 created an issue. See original summary.

mile23’s picture

This might be a little postponed on #2704871: Replace usages of deprecated method drupal_render(), but anyone who isn't afraid of a reroll can start here.

mile23’s picture

Status: Active » Needs review
StatusFileSize
new7.58 KB

A patch to start with...

Non-test usages replaced, except for these:

Drupal\Core\Ajax\CommandWithAttachedAssetsTrait uses renderer without injection. See: #2733703: [plan] Service traits should require IoC injection

Drupal\Core\Entity\EntityDisplayBase uses \Drupal to get the renderer. Since it's a plugin, we should be able to use ContainerFactoryPluginInterface, to inject, but since it's also an entity we can't, because entities use create().

Drupal\Core\Render\Element\Actions uses \Drupal to get the renderer in preRenderActionsDropbutton(), which is static. You'd think that the caller would be responsible for doing the rendering, but it is not. It contains a todo: // @todo Move this to #pre_render. Needs issue.

Drupal\field_ui\Element\FieldUiTable: also called from static.

Status: Needs review » Needs work

The last submitted patch, 3: 2834982_3.patch, failed testing.

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.

aviral2809’s picture

Assigned: Unassigned » aviral2809
martin107’s picture

Issue tags: +Needs reroll

This patch no longer applies...

@aviral2809 If you like I will help you with the reroll?

This issue injected the renderer --- it may be good as a pattern to copy.

#2869903: ContextualController: remove ContainerAwareTrait

If you want help just let me know....

martin107’s picture

Assigned: aviral2809 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new7.57 KB

Here is just a straight reroll

The only conflict I had to manually resolve was a conversion of array() to []

martin107’s picture

StatusFileSize
new7.57 KB
new716 bytes

my mistake

The last submitted patch, 8: array-2834982-8.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 9: this-2834982-9.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new7.63 KB
new3.53 KB

After looking at the current patch, I can see the problem

ViewFormBase breaks an established Drupal pattern for Base classes.

it is subtle but by convention Base classes do not provide _construct() and create() methods.

ViewFromBase is extended 10 times! .. so all patches touching those dependencies would have to adjust 10 constructors.

This argument makes me queasy but the 10 descendants to ViewFormBase get an extra method
$this->renderer() a single place which contains the antipattern.

\Drupal::service('renderer')

--- the argument being it concentrates the pain points, the thing to be refactored before Drupal9 to a single location.

Status: Needs review » Needs work

The last submitted patch, 12: allAboutThatBase-2834982-12.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new6.93 KB
new1.95 KB

For similar reasons to #12

EntityForm::_construct() is not called/ not useful

I don't like a Form having a dependency on the renderer service.

it seems overcomplicated. the solution is to recognise that the dependency is actually fake!

In this instance we are using the renderer service as a helper function to make a series of CacheableMetadata helper calls
in short render is a "pass through" service and we should just remove the dependency.

martin107’s picture

I am spinning off a side issue to untangle the renderer for other services.

we need to deprecate some RenderInterface:methods() so the problem won't be resolved until Drupal9 :(

mile23’s picture

Status: Needs review » Needs work

In this instance we are using the renderer service as a helper function to make a series of CacheableMetadata helper calls
in short render is a "pass through" service and we should just remove the dependency.

So if there is a need for cacheable metadata to be managed by EntityForm, it seems like that change would be to remove the dependency on Renderer, and then add code to manage the metadata the way Renderer::addCacheableDependency() does. Or add a @todo with a followup issue to do that if it's out of scope here.

+++ b/core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php
@@ -39,6 +39,26 @@
+  /**
+   * Gets the renderer service.
+   *
+   * @return \Drupal\Core\Render\RendererInterface
+   *   The renderer service.
+   */
+  protected function renderer() {
+    if (!$this->renderer) {
+      $this->renderer = \Drupal::service('renderer');
+    }
+    return $this->renderer;
+  }

+1. The comments in #12 and #14 are similar to the reasoning behind #2733703: [plan] Service traits should require IoC injection and #2471663: Undeprecate EntityHandlerBase

Anyway, the goal here is to minimize dependency on \Drupal, acknowledging that we can't completely remove it, especially for traits and base classes, so this AnythingBase::renderer() pattern is appropriate.

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.

martin107’s picture

Status: Needs work » Postponed

Lots, but not all of the patch here - has had to be duplicated as part of the solution to

#2876274: Renderer: deprecate mergeBubbleableMetadata() and addCacheableDependency()

I have move that issue to a state where I think it is complete.

Given the conflicts with this patch ... I think we should pause here and reroll when the other is committed.

I would still love to get this in before Drupal9

martin107’s picture

Status: Postponed » Needs work

Mile23 , sorry I did not mean to derail your issue.

#2876274 blew up into a 60Kb patch.. now that is near complete I am going to start working on this again.

but just limiting changes to

\Drupal::service('renderer')->renderRoot()
\Drupal::service('renderer')->render()

[ the other patch will deal with all the rest ]

the final patch here should also be reasonably large..

martin107’s picture

This is not a candidate for best use of grep in 2019 but

git grep --name-only "\Drupal::service('renderer')->render" | grep -v 'Test\.php' | grep -v '\.module' | grep -v '\.inc' | grep -v '\.api\.php' | grep -v '.install'

27 things to replace

core/lib/Drupal/Core/Ajax/CommandWithAttachedAssetsTrait.php
core/lib/Drupal/Core/Render/Element/Actions.php
core/lib/Drupal/Core/Render/Element/Tableselect.php
core/lib/Drupal/Core/Theme/ThemeManager.php
core/lib/Drupal/Core/Utility/TableSort.php
core/modules/book/src/Form/BookAdminEditForm.php
core/modules/comment/src/Plugin/views/field/EntityLink.php
core/modules/comment/src/Plugin/views/field/StatisticsLastCommentName.php
core/modules/contextual/src/Plugin/views/field/ContextualLinks.php
core/modules/field_ui/src/Element/FieldUiTable.php
core/modules/file/src/Element/ManagedFile.php
core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
core/modules/filter/src/Plugin/Filter/FilterCaption.php
core/modules/filter/src/Plugin/Filter/FilterHtml.php
core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php
core/modules/image/src/Form/ImageStyleEditForm.php
core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php
core/modules/node/src/NodeListBuilder.php
core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php
core/modules/rdf/tests/src/Kernel/Field/FieldRdfaTestBase.php
core/modules/search/tests/modules/search_extra_type/src/Plugin/Search/SearchExtraTypeSearch.php
core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php
core/modules/update/src/Form/UpdateManagerUpdate.php
core/modules/views/src/Analyzer.php
core/modules/views/src/Plugin/views/area/Result.php
core/modules/views/src/Plugin/views/field/PrerenderList.php
core/modules/views_ui/src/Form/BreakLockForm.php

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.

thalles’s picture

Hi @martin107, you create a new parameter in Drupal\file\Plugin\Field\FieldWidget\FileWidget

But this parameter was no injected in Drupal\image\Plugin\Field\FieldWidget\ImageWidget, I think this can be a problem

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.

s_bhandari’s picture

StatusFileSize
new2.09 KB

Found one in core Views module and adding a patch for the same.

jungle’s picture

Title: Replace usages of \Drupal::service('renderer') with IoC injection » Replace non-test usages of \Drupal::service('renderer') with IoC injection

We do this for non-test code under this issue.

Per the parent issue, adjust the scope to do it for non-test code.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new16.27 KB

Please review!

jungle’s picture

Status: Needs review » Needs work

Thanks @Suresh Prabhu Parkala, tests did not pass.

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.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new16.57 KB

Patch for 9.2.x, please review.

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.

narendra.rajwar27’s picture

StatusFileSize
new14.02 KB

Adding re-rolled patch for 9.5.x

This patch does not include changes in file core/modules/book/src/Form/BookAdminEditForm.php from patch #32, because the changes of this file are already covered here: https://www.drupal.org/project/drupal/issues/1169576#comment-14692162

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.

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

spokje’s picture

Assigned: Unassigned » spokje
Status: Needs review » Needs work
spokje’s picture

Assigned: spokje » Unassigned

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.