Problem/Motivation

This is a follow-up issue to #2729597: [meta] Replace \Drupal with injected services where appropriate in core.

Proposed resolution

Replace all non-static, non-test references to \Drupal::time() with IoC injection where possible.

Remaining tasks

  1. Make the requested changes to non-static references to \Drupal::time().
  2. Adjust existing tests to compensate for the newly added deprecation notices.
  3. Obtain code review from a framework manager (since this touches multiple subsystems).
  4. Address any change requests from the framework manager.
  5. Commit.

API changes

Some additional optional parameters. After committing this issue, not passing the parameters will result in a deprecation notice (but existing APIs should still work as intended).

Release notes snippet

TBD; maybe not necessary?

Issue fork drupal-3123216

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.

nitesh624’s picture

Assigned: Unassigned » nitesh624
nitesh624’s picture

StatusFileSize
new15 KB
nitesh624’s picture

Assigned: nitesh624 » Unassigned
Status: Active » Needs review
jungle’s picture

Status: Needs review » Needs work

Thanks for working on this. Tests did not pass.

nitesh624’s picture

Assigned: Unassigned » nitesh624
nitesh624’s picture

StatusFileSize
new6.44 KB
new12.02 KB
nitesh624’s picture

StatusFileSize
new13.07 KB
new14.4 KB
nitesh624’s picture

Assigned: nitesh624 » Unassigned
Status: Needs work » Needs review
jungle’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
sanjayk’s picture

Assigned: Unassigned » sanjayk
sanjayk’s picture

StatusFileSize
new11.31 KB

Reroll the patch

sanjayk’s picture

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

Thanks for working on this.

It's not just replacing \Drupal::time() with \Drupal::service('datetime.time') , but

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

Kindly see the Parent issue for more info.

jungle’s picture

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

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

Per the parent issue, rescoping this to do it for non-test code.

So i have to set this back to NW, sorry!

clayfreeman’s picture

If \Drupal::time() is not being removed, then why replace it with \Drupal::service('datetime.time')? Aren't these two expressions functionally equivalent?

I'd assume that the issue is referring to adding the service via dependency injection where possible.

jungle’s picture

@clayfreeman, please ignore the patch/what have done here. The patch #13 is meaningless ATM to me, I do not think @sanjayk understood the scope here, partially, it's my bad, the issue summary is not very clear, sorry! See a fixed sibling issue for what we have done for simliar ones, for instance: #3123210: Replace non-test usages of \Drupal::theme() with IoC injection or visit the parent issue for more if you are interested in.

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new6.69 KB

@jungle According to me I have found only two places where I have done with the changes(Service via dependency injection), Please have a look and advise.

jungle’s picture

Status: Needs review » Needs work

Needs fixing the tests.

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new6.78 KB
new2.38 KB

Fixed the test, Please review.

hardik_patel_12’s picture

StatusFileSize
new6.94 KB
new2.71 KB

Adding changed record and deprecation error message.

The last submitted patch, 21: 3123216_21.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 22: 3123216-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

clayfreeman’s picture

Please note that Drupal\Core\Queue\DatabaseQueue and Drupal\Core\Queue\MemoryQueue are currently using time(), but will be switched to \Drupal::time() after #3116478: Add a way to silently keep an item locked when processing a queue via cron is committed.

The reason that we cannot use dependency injection right away is explained in #54.

clayfreeman’s picture

clayfreeman’s picture

The related issue in my last comment has been committed (see 0f10d21).

The scope of this issue will need to be adjusted accordingly to accommodate the changes therein.

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.

guilhermevp’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new6.85 KB

Re-rolled patch, updated summary.

clayfreeman’s picture

clayfreeman’s picture

Issue summary: View changes
Issue tags: +Needs framework manager review

MR !910 simply adds IoC injection to all non-static, non-test references to \Drupal::time() without consideration for whether it makes sense or not; I figure code review can be used to determine where we consider this change to actually be useful.

Two areas worth highlighting:

  1. _batch_queue($batch_set)

    In order to use IoC for the queue subsystem, we're going to have to solidify some potentially-shoddy API surrounding the queue subsystem's usage in the Batch API. I went ahead and made this change, but I fully expect that we'll either want to improve the Batch API's invocation of the queue subsystem, or ignore this change for now.

  2. \Drupal\update\ProjectSecurityRequirement

    Using IoC for this class felt a bit silly while I was implementing it, but I went ahead with it anyway for completeness. I figure this will be another area that will require a bit of discussion to line out.

Tagging with "Needs framework manager review" since it doesn't seem practical to set the component for each subsystem. Affected subsystems (as best as I can tell):

  • batch system
  • entity system
  • cron system (queue)
  • comment.module
  • system.module
  • update.module

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

Hiding the patches as this appears to be worked on in merge requests.

Can we please get the MR updated to point to 10.1.x

andypost’s picture

Issue tags: +Needs reroll

it needs new MR or patch for 10.1.x

@clayfreeman please change MR branch if it's doable

rpayanm’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new33.83 KB
smustgrave’s picture

Status: Needs review » Needs work

Commit check failures
The last patch doesn't pass commit checks, could you make sure to run ./core/scripts/dev/commit-code-check.sh before uploading a patch to make sure there are no issues with code formatting. see https://www.drupal.org/docs/develop/development-tools/running-core-devel...

@rpayanm please check your patches before uploading..

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new33.87 KB
smustgrave’s picture

Status: Needs review » Needs work

Seems there were some errors

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

spokje’s picture

Assigned: Unassigned » spokje

Added new 10.1.x MR, the old one had no comments/threads, so no need to c/p them.

spokje’s picture

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

unsure if this actually needs a framework manager review.

andypost’s picture

Pushed one commit to fix own feedback

Changed messages to 10.1.0, added types and cleaned up
andypost authored 4 minutes ago

- message is for 10.1.x (formalized)
- properties should be typed to interface
- static method also needs injection
- testing could mock less
- use interfaces in mocks

also fixed CR

spokje’s picture

Thanks @andypost, got a bit too sloppy in my haste there.

I've already tried removing the always-NULL parameter Connection $connection = NULL from \Drupal\Core\Queue\Memory::construct, but that didn't end well: https://www.drupal.org/pift-ci-job/2536193

By the looks of the current TestBot run, we'll end up with the same result.

andypost’s picture

oh, missed that, thanks for pointer!

andypost’s picture

I'm thinking to file new blocker bug - properly pass arguments to queue constructor in _batch_queue

spokje’s picture

Makes sense to me, at least far more sense than keeping an always-NULL parameter in the middle of a constructor...

andypost’s picture

andypost’s picture

It's green after random failure, just not sure about form.inc changes

andypost’s picture

Probably better re-purpose #3325158 as follow-up for 11.0.x to remove the bridge code

smustgrave’s picture

Reviewing the MR looks like the deprecation is correct. The MR will have to be rebased though.

Should this be postponed until framework manager reviews.

spokje’s picture

Issue tags: +PHPStan-1

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

smustgrave’s picture

Status: Needs review » Needs work

#57 appears to be a rebase click.

Reviewing the MR I see all the instances have been addressed.

When I checked locally SqlContentEntityStorageSchema::getTemporaryTableMappingPrefix() I see one more instance I think should be replaced.

$prefix_parts[] = \Drupal::time()->getRequestTime();
andypost’s picture

Status: Needs work » Needs review

This method is static so it can't use DI for service

Maybe it could use follow-up to add argument to inject service but better not

andypost’s picture

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Confirmed the point in #58 is addressed. That was my only note. Good to mark this one.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think we can add a deprecation to allow us to remove the \Drupal::time() from \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getTemporaryTableMappingPrefix

spokje’s picture

Status: Needs work » Needs review
alexpott’s picture

Status: Needs review » Needs work

Added some review comments to the MR.

spokje’s picture

Assigned: Unassigned » spokje
spokje’s picture

Status: Needs work » Needs review

Thanks @alexpott, left one thread open since PHPCS didn't like it

spokje’s picture

Assigned: spokje » Unassigned
spokje’s picture

Thanks @mondrake for the (per usual) excellent threads in the MR. Answered them inline over there.

mondrake’s picture

Added a comment to the MR.

spokje’s picture

Thanks @mondrake, added readonly everywhere.

mondrake’s picture

Status: Needs review » Needs work

One significant point in the MR, then, alas, I think we need deprecation tests.

spokje’s picture

I'm gonna need some help with this test failure:

There was 1 error:

1) Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest::testSqlContentEntityStorageConstructorDeprecation
TypeError: Drupal\Core\Entity\Sql\SqlContentEntityStorage::getCustomTableMapping(): Argument #1 ($entity_type) must be of type Drupal\Core\Entity\ContentEntityTypeInterface, null given, called in /var/www/html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php on line 370

/var/www/html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:393
/var/www/html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:370
/var/www/html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:217
/var/www/html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:202
/var/www/html/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php:1487
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:673
/var/www/html/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:661
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:97

No clue why calling the constructor of SqlContentEntityStorage causes this?

smustgrave’s picture

Was able to fix locally by adding

  public function testSqlContentEntityStorageConstructorDeprecation(): void {
    $this->setUpEntityStorage();

Also had to update the deprecation message expected.

Didn't push the changes so I can stay in the reviewer side.

spokje’s picture

Status: Needs work » Needs review

THanks @smustgrave, that was indeed the missing piece of the puzzle.

larowlan’s picture

Left some comments, removing the tag for now, but will keep an eye on the issue/MR comments

spokje’s picture

Status: Needs review » Needs work

Thanks @larowlan for the review.

Back to NW for the open threads in the MR.
I'm going to let somebody else step in, because I've ran out of time/steam/joy working on this one.

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.