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-3123207

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.

ankitsingh0188’s picture

Assigned: Unassigned » ankitsingh0188
ankitsingh0188’s picture

StatusFileSize
new144.55 KB
ankitsingh0188’s picture

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

Title: Replace usages of \Drupal::languageManager() with IoC injection » Replace non-test usages of \Drupal::languageManager() 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 for the change in the middle!

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new9.5 KB

List of place where we can inject injection if possible

grep -nri '\Drupal::languageManager()' core | grep -v .module | grep -vi test | grep -v .api.php | grep -v authorize.php | grep -v .inc
core/lib/Drupal/Core/TypedData/Plugin/DataType/Language.php:41:      $this->language = \Drupal::languageManager()->getLanguage($this->id);
core/lib/Drupal/Core/Datetime/DrupalDateTime.php:84:      $settings['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
core/lib/Drupal/Core/Entity/EntityBase.php:91:    return \Drupal::languageManager();
core/lib/Drupal/Core/Entity/Sql/SqlFieldableEntityTypeListenerTrait.php:200:            $entity->set($langcode_key, \Drupal::languageManager()->getDefaultLanguage()->getId());
core/lib/Drupal/Core/Render/Element/MachineName.php:129:    $language = \Drupal::languageManager()->getCurrentLanguage();
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php:91:    $this->setValue(['value' => \Drupal::languageManager()->getDefaultLanguage()->getId()], $notify);
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php:119:      $languages = array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php:129:    return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL));
core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php:136:    $languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
core/lib/Drupal/Core/Field/FormatterBase.php:87:      $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
core/lib/Drupal/Core/Routing/RouteProvider.php:134:    $this->languageManager = $language_manager ?: \Drupal::languageManager();
core/lib/Drupal/Core/Validation/DrupalTranslator.php:69:    return $this->locale ? $this->locale : \Drupal::languageManager()->getCurrentLanguage()->getId();
core/lib/Drupal/Core/Session/UserSession.php:132:    $language_list = \Drupal::languageManager()->getLanguages();
core/lib/Drupal/Core/Session/UserSession.php:137:      return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
core/lib/Drupal/Core/Session/UserSession.php:145:    $language_list = \Drupal::languageManager()->getLanguages();
core/lib/Drupal/Core/Session/UserSession.php:150:      return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';

Kindly review a patch.

jungle’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
@@ -21,6 +22,7 @@
+  use LanguageTrait;

I do not think we need a new trait here. ContainerInjectionInterface might help. Tests did not pass.

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.

saphemmy’s picture

Assigned: Unassigned » saphemmy

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.

ankitsingh0188’s picture

StatusFileSize
new17.84 KB
ankitsingh0188’s picture

StatusFileSize
new18.47 KB
ankitsingh0188’s picture

StatusFileSize
new15.05 KB
ankitsingh0188’s picture

StatusFileSize
new15.05 KB
ankitsingh0188’s picture

StatusFileSize
new15.05 KB
jungle’s picture

@ankitsingh0188 thanks for working on this.

See https://stackoverflow.com/questions/18142870/git-error-fatal-corrupt-pat... for the error, maybe you should try the git CLI to get the patch or see https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...

Also, please see #3123210: Replace non-test usages of \Drupal::theme() with IoC injection as a good example.

jungle’s picture

+++ b/core/lib/Drupal/Core/Session/UserSession.php
@@ -130,12 +130,12 @@ public function isAnonymous() {
-    $language_list = \Drupal::languageManager()->getLanguages();
+    $language_list = \Drupal::service('language_manager')->getLanguages();
...
-      return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
+      return $fallback_to_default ? \Drupal::service('language_manager')->getDefaultLanguage()->getId() : '';

Those are not "Using IoC as possible". They are almost the same. Again see fixed issue #3123210: Replace non-test usages of \Drupal::theme() with IoC injection as the example.

jungle’s picture

Assigned: saphemmy » Unassigned
_pratik_’s picture

StatusFileSize
new15.09 KB

Try this patch.
Thanks

_pratik_’s picture

Status: Needs work » Needs review
ankitsingh0188’s picture

StatusFileSize
new18.46 KB
jungle’s picture

Status: Needs review » Needs work

The patch is fine now, but the patch itself did not address this issue. Please see the one I linked in #21 as an example, or read the parent issue.

ankitsingh0188’s picture

I think the patch #25 will address the issue as mentioned in #21

jungle’s picture

jungle’s picture

>I think the patch #25 will address the issue as mentioned in #21

Nope, see #26

ankitsingh0188’s picture

StatusFileSize
new13.31 KB

@jungle I have updated the patch and using IoC wherever possible.

jungle’s picture

+++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
@@ -99,10 +106,11 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor
       @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/Field/FormatterBase.php
@@ -69,13 +79,18 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
+      @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -81,6 +81,13 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn
+  /**
+   * The language manager.
+   *
+   * @var \Drupal\Core\Language\LanguageManagerInterface
+   */
+  protected $languageManager;

@@ -98,10 +105,10 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn
-   * @param \Drupal\Core\Language\LanguageManagerInterface|null $languageManager
+   * @param \Drupal\Core\Language\LanguageManagerInterface|null $language_manager
...
-  public function __construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler, protected ?LanguageManagerInterface $languageManager = NULL) {
+  public function __construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager = NULL) {

@@ -109,10 +116,11 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor
+      @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

This is fine. it's constructor property promotion introduced in PHP8, let's adopt it in this issue. and hopefully, it gets applied to other sister issues.

+++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
@@ -99,10 +106,11 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor
       @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/Field/FormatterBase.php
@@ -69,13 +79,18 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
+      @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -109,10 +116,11 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor
+      @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/Routing/RouteProvider.php
@@ -131,7 +131,11 @@ public function __construct(Connection $connection, StateInterface $state, Curre
+      @trigger_error('Calling ' . __METHOD__ . '() without the $language_manager argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0', E_USER_DEPRECATED);

A change record link is expected. Tagging needs CR.

ankitsingh0188’s picture

Issue tags: -Needs change record
StatusFileSize
new9.83 KB
new14.29 KB

I've updated the patch with the new CR.

ankitsingh0188’s picture

StatusFileSize
new10.35 KB
ankitsingh0188’s picture

StatusFileSize
new10.67 KB
ankitsingh0188’s picture

StatusFileSize
new7.2 KB
ankitsingh0188’s picture

StatusFileSize
new7.2 KB
ankitsingh0188’s picture

StatusFileSize
new5.69 KB
jungle’s picture

Assigned: Unassigned » jungle

Hi, @ankitsingh0188. I will work on this.

jungle’s picture

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

Unassign myself, #37 may be good for review.

mukesh88’s picture

StatusFileSize
new8.01 KB
new4.78 KB
new344.14 KB

After applying the patch #37 site is down . After adding some changes in patch #40 site is working

mukesh88’s picture

StatusFileSize
new3.18 KB
new3.64 KB

Recreate patch after failed #40

mukesh88’s picture

StatusFileSize
new5.3 KB
new1.84 KB

Oops!! missing something in patch.

Status: Needs review » Needs work

The last submitted patch, 42: 3123207-42.patch, failed testing. View results

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.