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

Issue fork drupal-3123224

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

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.

s_bhandari’s picture

Assigned: Unassigned » s_bhandari
s_bhandari’s picture

Assigned: s_bhandari » Unassigned
hardik_patel_12’s picture

Status: Active » Needs review
StatusFileSize
new685 bytes

Kindly review a patch.

msuthars’s picture

Assigned: Unassigned » msuthars
msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs review » Needs work

@Hardik_Patel_12 MailManager is pending of Ioc service injection. You can inject the request_stack service in it.
example:
$this->request = $request_stack->getCurrentRequest();

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new6.58 KB
new5.61 KB

Injected \Drupal::request() call in MailManager class , kindly review a new patch.

Tasks left:

  1. Check if there are more to do in non-test code.
  2. Add change record(s)
jungle’s picture

Title: Replace usages of \Drupal::request() with IoC injection » Replace non-test usages of \Drupal::request() with IoC injection
Status: Needs review » Needs work

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

Thanks @Hardik_Patel_12 for the patch, per the parent issue, rescoping this to do it for non-test code.

So I have to set back to NW for removing changes in tests, sorry, commenting to other sibling issues I filed.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new4.57 KB
new2.01 KB

Here I have made changes as suggested in comment #9, please review.

Status: Needs review » Needs work

The last submitted patch, 10: 3123224-10.patch, failed testing. View results

jungle’s picture

  1. Testing did not pass
  2. $ git grep -nri '\Drupal::request()' core | grep -v .module | grep -iv test.php | grep -iv .inc | grep -iv trait.php| grep -iv .api.php | grep -v authorize.php | grep -iv testbase.php | grep -v '//' | grep -v '*' | grep -v .theme | grep -v '/tests/'
    core/lib/Drupal/Core/Database/Query/TableSortExtender.php:37:    $context = TableSort::getContextFromRequest($header, \Drupal::request());
    core/lib/Drupal/Core/Entity/Query/QueryBase.php:333:    $order = TableSort::getOrder($headers, \Drupal::request());
    core/lib/Drupal/Core/Entity/Query/QueryBase.php:334:    $direction = TableSort::getSort($headers, \Drupal::request());
    core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php:94:    $request = \Drupal::request();
    core/lib/Drupal/Core/Render/Element/RenderElement.php:349:        $settings['options']['query'] += \Drupal::request()->query->all();
    

    Looks like there are a few more to do.

nikitagupta’s picture

Status: Needs work » Needs review
StatusFileSize
new7.58 KB
new2.51 KB
jungle’s picture

Status: Needs review » Needs work

Patch failed to apply.

nikitagupta’s picture

StatusFileSize
new7.58 KB
new2.51 KB
hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new5.8 KB
new1.1 KB

@nikitagupta thanks for working on this issues , but we need to inject Request(\Drupal::request()) service before replacements as we have done in core/lib/Drupal/Core/Mail/MailManager.php file.

Solving failed test cases of #10 , if we want do changes for only non-test file then this test cases will always gone be fail.

core/lib/Drupal/Core/Render/Element/RenderElement.php:349:        $settings['options']['query'] += \Drupal::request()->query->all();

FOr this file \Drupal::request() call is under static function so this looks ok to me.

core/lib/Drupal/Core/Database/Query/TableSortExtender.php:37:    $context = TableSort::getContextFromRequest($header, \Drupal::request());
core/lib/Drupal/Core/Entity/Query/QueryBase.php:333:    $order = TableSort::getOrder($headers, \Drupal::request());
core/lib/Drupal/Core/Entity/Query/QueryBase.php:334:    $direction = TableSort::getSort($headers, \Drupal::request());
core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php:94:    $request = \Drupal::request();

For this files can anyone suggest good way to inject \Drupal::request() call.

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

At this time we will need a D10 version of the patch.

Also D10 should be checked to see if there are additional instances of \Drupal::request() that need to be addressed.

Thanks

nitin shrivastava’s picture

StatusFileSize
new5.84 KB
new5.84 KB

reroll for drupal 10

smustgrave’s picture

Still seem to be instances

ViewExecutable
DbUpdateController

Not sure if entities or traits count.

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.

hardik_patel_12’s picture

StatusFileSize
new5.83 KB
new1.12 KB

Updating deprecation message for drupal:11.0.0.
The invocation of \Drupal::request() is located within a static function in DbUpdateController, making it uncertain whether it should be modified.

samit.310@gmail.com made their first commit to this issue’s fork.

samit.310@gmail.com changed the visibility of the branch 3123224-replace-non-test-usages to hidden.

samitk’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Should use constructor promotion now.

samitk’s picture

Status: Needs work » Needs review

Hi @smustgrave,

Please review.

Thanks
Samit K.

smustgrave’s picture

Status: Needs review » Needs work

Left a comment but not sure we can fully remove parameters and add new ones without doing a BC step.

Also what was the search that was ran to find these, imagine there are more.

dcam’s picture

core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php also has a call to \Drupal::request() as noted in #3373074: Call to a member function has() on null in Drupal\Core\Mail\Plugin\Mail\PhpMail->mail().

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.