Needs work
Project:
Drupal core
Version:
main
Component:
base system
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
29 Mar 2020 at 04:12 UTC
Updated:
21 Jul 2020 at 11:22 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #3
codersukanta commentedComment #4
codersukanta commentedChanged
\Drupal::currentUser()with the current_user service.I was trying to inject the

AccountProxyInterfacein__constructorfunction of some class but got some error like this.It would be very helpful if anyone can suggest me the right approach to resolve it.
Comment #5
hardik_patel_12 commented@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.
Comment #6
msutharsComment #7
msuthars@Hardik_Patel_12 I reviewed the patch and found some changes:
1. In
BookAdminEditFormno need to inject the service you can simply use$this->currentUser()method that is already defined in Parent classFormBase.2.
ContentTranslationHandleris pending for Ioc injection. You have to simply use$this->currentUser->id()ingetDefaultOwnerId()method.3.
FormTestVerticalTabsFormis also pending for Ioc injection, simply use$this->currentUser()->hasPermission('access vertical_tab_test tabs')inbuildForm()method.Comment #8
msutharsComment #9
hardik_patel_12 commentedPoints 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.
Comment #10
msutharsComment #11
msuthars@Hardik_Patel_12 thanks to clarifying. The patch #9 LGTM. Tested on local and it is working as expected, moving to RTBC.
Comment #12
alexpottFor 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.
Comment #13
hardik_patel_12 commentedAdding change record and deprecation error also. Kindly review a new patch.
Comment #14
kim.pepper@Hardik_Patel_12 change records cannot be published until the issue is committed.
Comment #15
hardik_patel_12 commented@kim.pepper , i am un-publishing change record thanks for your suggestion.
Comment #16
volegerAttaching related issue.
Comment #17
kim.pepperI 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.phpComment #18
longwaveRe #17 I think some of these are going to be tricky.
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?
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?
This is also doable but would need a BC layer for the constructor.
This would also need a BC layer.
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?
Comment #19
junglePer the parent issue, rescoping this to do it for non-test code. Sorry for the change in the middle!