Problem

drupal_container() is deprecated, and all calls in the core modules need to be replaced with Drupal::service().

One exception is where the module_handler service is requested:
drupal_container()->get('module_handler');

which needs to be replaced with Drupal::moduleHandler() (#1957154: Replace calls to drupal_container()->get('module_handler') service with Drupal::moduleHandler())

Proposed resolution

Create patches for each core module.

Remaining tasks

To do

#2001202: Replace drupal_container() with Drupal::service() in the aggregator module
#2001044: Replace drupal_container() with Drupal::service() in the user module
#2003058: Replace drupal_container() with Drupal::service() in the block module
#2003260: Replace drupal_container() with Drupal::service() in the breakpoint module
#2003430: Convert CKEditor tests to use $this->container instead of drupal_container()
#2003498: Replace drupal_container() with Drupal::service() in the comment module
#2003576: Replace drupal_container() with Drupal::service() in the config module
#2003596: Replace drupal_container() with Drupal::service() in the contact module
#2003616: Replace drupal_container() with Drupal::service() in the editor module
#2003630: Replace drupal_container() with Drupal::service() in the entity_reference module
#2011082: Replace drupal_container() with Drupal::service() in the field module
#2011084: Replace drupal_container() with Drupal::service() in the file module
#2011090: Replace drupal_container() with Drupal::service() in the language module
#2011094: Replace drupal_container() with Drupal::service() in the layout module
#2011102: Replace drupal_container() with Drupal::service() in the locale module
#2011104: Replace drupal_container() with Drupal::service() in the menu & menu_link modules
#2011108: Replace drupal_container() with Drupal::service() in the overlay module
#2011116: Replace drupal_container() with Drupal::service() in the path module
#2011122: Replace drupal_container() with injected services in the rest module
#2011126: Replace drupal_container() with Drupal::service() in the search module
#2014009: Replace drupal_container() with Drupal::service() in the serialization module
#2014011: Replace drupal_container() with Drupal::service() in the shortcut module
#2014015: Replace drupal_container() with Drupal::service() in the simpletest module
#2014013: Replace drupal_container() with Drupal::service() in the statistics module
#2014017: Replace drupal_container() with Drupal::service() in the system module
#2014025: Replace drupal_container() with Drupal::service() in the taxonomy module
#2014031: Replace drupal_container() with Drupal::service() in the tour module
#2014033: Replace drupal_container() with Drupal::service() in the content_translation module
#2014037: Replace drupal_container() with Drupal::keyValue() in the update module
#2014043: Replace drupal_container() with Drupal::service() in the views module
#2014049: Replace drupal_container() with Drupal::service() in the views_ui module
...

#2006024: drupal_container() is deprecated, don't use it in bootstrap.php

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ebeyrent’s picture

Issue summary: View changes

Added issue 2003058

ebeyrent’s picture

Issue summary: View changes

Updated issue summary.

ebeyrent’s picture

Issue summary: View changes

Added issue 2003430

ebeyrent’s picture

Issue summary: View changes

Updated issue summary.

ebeyrent’s picture

Issue summary: View changes

Updated issue summary.

ebeyrent’s picture

Issue summary: View changes

Updated issue summary.

ebeyrent’s picture

Issue summary: View changes

Updated issue summary.

ddrozdik’s picture

we will try to check and create needed patches during Code Sprint UA.

ddrozdik’s picture

Status: Active » Needs work

ebeyrent, I see this is not all list of issues, because other modules like field, file, layout ... uses drupal_container function. Do you need help with these modules ?

ddrozdik’s picture

Issue summary: View changes

Updated issue summary.

ddrozdik’s picture

Issue summary: View changes

Added new tasks

ddrozdik’s picture

Issue tags: +Novice, +CodeSprintUA
ebeyrent’s picture

Absolutely - please feel free to help out as much as you can!

ebeyrent’s picture

Issue summary: View changes

add task for rest module

ddrozdik’s picture

Issue summary: View changes

add new tasks to list

ddrozdik’s picture

Title: [META] Replace drupal_container() with Drupal::service() in core modules » [META] Replace drupal_container() with Drupal::service()
ddrozdik’s picture

Issue summary: View changes

add task to list

ddrozdik’s picture

Issue summary: View changes

add new tasks to list

ddrozdik’s picture

Issue summary: View changes

remove duplicated task

Crell’s picture

The issues listed are all done, but it looks like someone has added a bunch more drupal_container() calls. :-( NetBeans finds 34 instances. Let's just fix them here and be done with it.

Do people not read change notices, or do committers not notice when someone is using a deprecated function? *sigh*

Ashleigh Thevenet’s picture

Issue summary: View changes
Status: Needs work » Fixed

The last issue on this meta is now committed, so I'm closing this.

Crell’s picture

Title: [META] Replace drupal_container() with Drupal::service() » Replace drupal_container() with Drupal::service()
Priority: Normal » Major
Status: Fixed » Needs work
Issue tags: +beta blocker

Unfortunately I just checked the source code and not only do we still need to remove the function itself but there's 28 NEW instances of drupal_container(). Someone didn't get the memo. :-(

Let's turn this back into a non-meta to just finish it off. Also tagging beta blocker as there's no good reason for us to ship with the old function.

Crell’s picture

Status: Needs work » Needs review
FileSize
13.93 KB

Let's try this.

Status: Needs review » Needs work
Crell’s picture

Status: Needs work » Needs review
FileSize
13.94 KB
Crell’s picture

Status: Needs work » Needs review

I said needs review you damned buggy issue queue...

tstoeckler’s picture

Issue tags: -beta blocker

Sorry, but this is in no way a beta blocker. I'm not a fan of deprecated functions, but nothing at all is or can be broken by the function or its usage. It's not nice and removing it is a nice to have, but to block anything on it is just wrong. Even if we were to ship Drupal 8.0 with the function no one should lose sleep over it.

ebeyrent’s picture

If we mark the function as deprecated, we're letting the world know that the function is going to be removed at some point. If, however, we're still using it in core, what's the point of it being @deprecated? Core should be the shining example of how to use the API, and to release a version of core that uses deprecated functions is sloppy and lazy.

We should just do the work now and remove the function, which should have been done instead of deprecating the function.

Crell’s picture

Issue tags: +beta blocker

Actually the fact that drupal_container() can't be autoloaded means that it *can* hurt things. It's been on the chopping block for months, if not a year. Kill it.

If you disagree that it's a beta blocker, review and RTBC the patch sooner rather than later so that it becomes a moot point.

Testbot, do you job, would you??

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/lib/Drupal.php
@@ -106,8 +106,8 @@ public static function setContainer(ContainerInterface $container) {
+   * @deprecated This method is only useful for the testing environment. It
+   * should not be used otherwise.

Wrong indentation.

Also: This comment has never been accurate. As can be seen in current_path() but also in many cases in core, there are legit cases for using getContainer(). It always pisses me off when PhpStorm complains at me about using deprecated functions, when there's no alternative...

I still think this issue is a *huge* misuse of the "beta-blocker" tag and a great example for some of the inequalities in the core queue, but it's also very simple and I can't argue it's not RTBC. So marking as such. progress++

Crell’s picture

Come on, testbot, do your job...

herom’s picture

Status: Reviewed & tested by the community » Needs review
herom’s picture

herom’s picture

Status: Needs review » Reviewed & tested by the community

and back to rtbc, waiting for test results.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: 2001206-remove-drupal-container.patch, failed testing.

The last submitted patch, 19: 2001206-remove-drupal-container.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
FileSize
13.94 KB

reroll

herom’s picture

Status: Needs work » Needs review
FileSize
13.94 KB

Status: Needs review » Needs work

The last submitted patch, 24: 2001206-container-to-service.patch, failed testing.

penyaskito’s picture

Fixed typo.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
1.17 KB
14.02 KB

Really weird that this other patch passes.

\Drupal::getContainer() should return the same instance every time is called.

Status: Needs review » Needs work

The last submitted patch, 27: 2001206-container-to-service-27.patch, failed testing.

penyaskito’s picture

Status: Needs work » Needs review
FileSize
13.94 KB

Reuploading #26 for showing the difference.

Status: Needs review » Needs work

The last submitted patch, 29: 2001206-container-to-service-26.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 29: 2001206-container-to-service-26.patch, failed testing.

rfay’s picture

It looks to me like the fail of #27 is a result of some difference in behavior of testbot 953. Would love if somebody took a look. Doing a full reconfirmation on it, but my bet is that these are poor tests exposed by a slightly different install procedure.

herom’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 29: 2001206-container-to-service-26.patch, failed testing.

The last submitted patch, 26: 2001206-container-to-service-26.patch, failed testing.

herom’s picture

herom’s picture

patch #27 is passing.

penyaskito’s picture

Why is the difference in test results between #26 and #27?
This answer must be answered before RTBCing.

herom’s picture

well, to be clear, there is nothing wrong with this patch or the current behaviour.
the interdiff in #27 is necessary, because ModuleHandler::install/uninstall creates a new Container, and the old one becomes invalid.
here is the code path that creates the new Container:

ModuleHandler::install/uninstall() ->
\Drupal::service('kernel')->updateModules() ->
$this->boot() ->
$this->initializeContainer() ->
\Drupal::setContainer($this->container);
penyaskito’s picture

Ok, so if this is the desired (and known) behavior, #27 is ready to RTBC.
Thanks @herom for the explanation. While I did the patch myself, I had not a clear vision of the difference.

Crell’s picture

#27 is RTBC. Quick, before someone else uses drupal_container()! :-)

Crell’s picture

Status: Needs review » Reviewed & tested by the community

#27 is RTBC. Quick, before someone else uses drupal_container()! :-)

herom’s picture

reroll.

Crell’s picture

#43 is RTBC. Quick, before someone else uses drupal_container()! :-)

catch’s picture

For that reason (patches like this conflcting with new usages, head breaking etc.), I've been asking people to split the actual removal into a separate issue, so the conversions don't get lumped in. Sending for re-test now.

catch’s picture

Dries’s picture

In one part of the patch, we use:

-$password_hasher = drupal_container()->get('password');
+$password_hasher = \Drupal::service('password');

In another part of the patch, we use:

-$password_hasher = drupal_container()->get('password');
+$password_hasher = \Drupal::getContainer('password');

It looks like further clean-up is required?

catch’s picture

Status: Reviewed & tested by the community » Needs work
Crell’s picture

Re #45: Normally I agree, but this issue is mostly just last stragglers and usages that it looks like were added after this issue was setup. That's why we're trying to just close the deal once and for all.

I'll reroll for #47 this weekend if someone doesn't beat me to it.

andypost’s picture

FileSize
525 bytes
13.41 KB

Re-roll and fix for #47

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 50: 2001206-container-to-service-50.patch, failed testing.

catch’s picture

@Crell I also think we should make an exception in this case, function just will not die.

andypost’s picture

Status: Needs work » Needs review
FileSize
13.41 KB
andypost’s picture

FileSize
1.83 KB
1.83 KB

And a bit of clean-up of unused variables

Status: Needs review » Needs work

The last submitted patch, 54: 2001206-container-to-service-54.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
13.45 KB
Crell’s picture

Status: Needs review » Reviewed & tested by the community

Well, someone beat me to it.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

ianthomas_uk’s picture

Another call was added to core after this patch was committed. I've opened a followup: #2167517: system_custom_theme calls removed function drupal_container

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.