Problem/Motivation

Follow up #2729597: [meta] Replace \Drupal with injected services where appropriate in core

Proposed resolution

Replace all of them with IoC injection where possible

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

jungle created an issue. See original summary.

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.

codersukanta’s picture

Assigned: Unassigned » codersukanta
codersukanta’s picture

Assigned: codersukanta » Unassigned
Status: Active » Needs review
StatusFileSize
new208.8 KB
new109.72 KB

Changed \Drupal::currentUser() with the current_user service.

I was trying to inject the AccountProxyInterface in __constructor function of some class but got some error like this.
Testcase Result

It would be very helpful if anyone can suggest me the right approach to resolve it.

hardik_patel_12’s picture

StatusFileSize
new4.67 KB

@codersukanta, Thanks for working on this.

issue is not just replacing \Drupal::currentUser() with \Drupal::service('current_user') , but

It's to replace \Drupal::currentUser() and \Drupal::service('current_user') with IoC injection where possible.

Kindly see the Parent issue for more info.

Kindly review a patch.

msuthars’s picture

Assigned: Unassigned » msuthars
msuthars’s picture

@Hardik_Patel_12 I reviewed the patch and found some changes:

1. In BookAdminEditForm no need to inject the service you can simply use $this->currentUser() method that is already defined in Parent class FormBase.
2. ContentTranslationHandler is pending for Ioc injection. You have to simply use $this->currentUser->id() in getDefaultOwnerId() method.
3. FormTestVerticalTabsForm is also pending for Ioc injection, simply use $this->currentUser()->hasPermission('access vertical_tab_test tabs') in buildForm() method.

msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs review » Needs work
hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new4 KB
new2.64 KB

Points covered as suggested in #7 are as below.Following points are covered as suggested in #7

1. In BookAdminEditForm using $this->currentUser() method that is already defined in Parent class FormBase.
2. No need to replace \Drupal::currentUser() call with $this->currentUser() in getDefaultOwnerId() method because its a static method.
3. In FormTestVerticalTabsForm using $this->currentUser() in buildForm() method.

Kindly review a new patch.

msuthars’s picture

Assigned: Unassigned » msuthars
msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs review » Reviewed & tested by the community

@Hardik_Patel_12 thanks to clarifying. The patch #9 LGTM. Tested on local and it is working as expected, moving to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
+++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
@@ -66,12 +74,15 @@ public function usesGroupBy() {
+   * @param \Drupal\Core\Session\AccountInterface $current_user
+   *   The current user.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user) {
...
+    $this->currentUser = $current_user;

For BC in case someone has extended this in contrib we need to do the deprecation dance - i.e. allow the current user to be null and then use \Drupal service if it is and do @trigger_error(message, E_USER_DEPRECATED) - which means we need a change record too.

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new4.3 KB
new1.43 KB

Adding change record and deprecation error also. Kindly review a new patch.

kim.pepper’s picture

@Hardik_Patel_12 change records cannot be published until the issue is committed.

hardik_patel_12’s picture

@kim.pepper , i am un-publishing change record thanks for your suggestion.

voleger’s picture

kim.pepper’s picture

Status: Needs review » Needs work

I think there are a few more instances that can be injected:

grep -nri '\Drupal::currentUser()' core | grep -v .module | grep -vi test | grep -v .api.php | grep -v authorize.php

core//lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php:116:        ->getSettableOptions(\Drupal::currentUser());
core//lib/Drupal/Core/Form/FormBuilder.php:1415:      $this->currentUser = \Drupal::currentUser();
core//lib/Drupal/Core/TempStore/SharedTempStoreFactory.php:79:      $owner = \Drupal::currentUser()->id() ?: session_id();
core//modules/jsonapi/src/Context/FieldResolver.php:731:    $filter_access_results = $this->moduleHandler->invokeAll('jsonapi_entity_field_filter_access', [$field_definition, \Drupal::currentUser()]);
core//modules/locale/src/LocaleLookup.php:115:      $user = \Drupal::currentUser();
longwave’s picture

Re #17 I think some of these are going to be tricky.

core//lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php:116:        ->getSettableOptions(\Drupal::currentUser());

This is an abstract base class that doesn't yet implement create(), I think this is doable but with a careful BC layer? There are already concrete implementations that do have services injected with create(), so they would need BC too?

core//lib/Drupal/Core/Form/FormBuilder.php:1415:      $this->currentUser = \Drupal::currentUser();

This has a safeguard first in case the service doesn't exist, not sure why it was done this way - forms are used in the installer, is this too early for the current user service?

core//lib/Drupal/Core/TempStore/SharedTempStoreFactory.php:79:      $owner = \Drupal::currentUser()->id() ?: session_id();

This is also doable but would need a BC layer for the constructor.

core//modules/jsonapi/src/Context/FieldResolver.php:731:    $filter_access_results = $this->moduleHandler->invokeAll('jsonapi_entity_field_filter_access', [$field_definition, \Drupal::currentUser()]);

This would also need a BC layer.

core//modules/locale/src/LocaleLookup.php:115:      $user = \Drupal::currentUser();

This class is instantiated directly rather than being a service or using a factory, so I don't know if or how we can handle the case of BC for existing callers. I guess we can just use a similar null check in the constructor?

jungle’s picture

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

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

Per the parent issue, rescoping this to do it for non-test code. Sorry for the change in the middle!

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.

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.