Updated: Comment #0

Problem/Motivation

#1862202: Objectify the language system obsoleted all the language_* functions in bootstrap.inc. The most commonly used were not replaced/removed to limit the size of the patch.

Proposed resolution

Replace all the deprecated function calls with their OO counterpart. Inject the language manager service where needed.

function language($type) {
  return \Drupal::languageManager()->getCurrentLanguage($type);
}
function language_list($flags = Language::STATE_CONFIGURABLE) {
  return \Drupal::languageManager()->getLanguages($flags);
}
function language_load($langcode) {
  return \Drupal::languageManager()->getLanguage($langcode);
}
function language_default() {
  return \Drupal::languageManager()->getDefaultLanguage();
}

This issue only applies to OO code. Tests and procedural code are being handled by #2166915: Remove uses of deprecated language functions in tests and procedural code

Remaining tasks

  1. Write a patch
  2. Fix failing tests
  3. Reviews

User interface changes

None

API changes

None

CommentFileSizeAuthor
#77 remove_remaining_uses-2225427-77.patch42.35 KBlomo
#77 remove_remaining_uses-2225427-76.patch42.35 KBlomo
#72 interdiff.txt808 bytesjeroent
#72 remove_remaining_uses-2225427-72.patch41.06 KBjeroent
#68 interdiff.txt2.34 KBjeroent
#68 remove_remaining_uses-2225427-68.patch41.01 KBjeroent
#66 interdiff.txt4.87 KBjeroent
#66 remove_remaining_uses-2225427-66.patch40.99 KBjeroent
#63 remove_remaining_uses-2225427-63.patch35.66 KBjeroent
#56 2225427-56-remove-language-oo.patch59.76 KBmartin107
#56 interdiff-54-56.txt958 bytesmartin107
#54 interdiff-52-54.txt867 bytesmartin107
#54 2225427-54-remove-language-oo.patch60.69 KBmartin107
#52 2225427-52-remove-language-oo.patch60.7 KBmartin107
#50 2225427-50-remove-language-oo.patch61.8 KBmartin107
#50 interdiff.txt1.16 KBmartin107
#47 2225427-47-remove-language-oo.patch61.65 KBmartin107
#45 2225427-45-remove-language-oo.patch65.69 KBmartin107
#45 interdiff.txt1.16 KBmartin107
#43 2225427-43-remove-language-oo.patch65.65 KBmartin107
#40 interdiff-37-40.txt2.08 KBmartin107
#40 2225427-40-remove-language-oo.patch65.66 KBmartin107
#37 2225427-37-remove-language-oo.patch65.66 KBmartin107
#33 diffdiff-24-33.txt12.52 KBianthomas_uk
#33 2225427-33-remove-language-oo.patch65.53 KBianthomas_uk
#31 2225427-31-remove-language-oo.patch64.34 KBianthomas_uk
#29 2225427-29-remove-language-oo.patch64.06 KBianthomas_uk
#27 2225427-27-remove-language-oo.patch63.94 KBianthomas_uk
#24 2225427-24-remove-language-oo.patch64.3 KBianthomas_uk
#24 interdiff-22-24.txt2.65 KBianthomas_uk
#22 interdiff-20-22.txt4.14 KBianthomas_uk
#22 2225427-22-remove-language-oo.patch61.65 KBianthomas_uk
#20 2225427-20-remove-language-oo.patch57.51 KBianthomas_uk
#20 interdiff-18-20.txt11.91 KBianthomas_uk
#18 interdiff-16-18.txt2.58 KBianthomas_uk
#18 2225427-18-remove-language-oo.patch46.26 KBianthomas_uk
#16 2225427-16-remove-language-oo.patch43.68 KBianthomas_uk
#14 2225427-15-remove-language-oo.patch43.36 KBianthomas_uk
#14 interdiff-13-15.txt19.98 KBianthomas_uk
#13 2225427-13-remove-language-oo.patch23.91 KBianthomas_uk
#13 interdiff-11-13.txt4.92 KBianthomas_uk
#11 interdiff-9-11.txt2.48 KBmartin107
#11 2225427-11-remove-language-oo.patch25.3 KBmartin107
#9 2225427-9-remove-language-oo.patch25.39 KBianthomas_uk
#9 interdiff-7-9.txt3.48 KBianthomas_uk
#7 2225427-7-remove-language-oo.patch25.43 KBianthomas_uk
#4 2225427-4-remove-language-oo.patch35.47 KBianthomas_uk
#4 interdiff-1-4.txt19.78 KBianthomas_uk
#2 2225427-remove-language-oo.patch24.12 KBianthomas_uk

Comments

ianthomas_uk’s picture

ianthomas_uk’s picture

This is the work from #2166915: Remove uses of deprecated language functions in tests and procedural code. It uses \Drupal but is a good base to start from.

ianthomas_uk’s picture

Status: Needs work » Needs review

Might as well test that

ianthomas_uk’s picture

StatusFileSize
new19.78 KB
new35.47 KB

Here are a few classes switched to dependency injection. Uploading partly for testing, but I'd also appreciate reviews of the interdiff to say if I'm doing the right thing.

Status: Needs review » Needs work

The last submitted patch, 4: 2225427-4-remove-language-oo.patch, failed testing.

martin107’s picture

Issue tags: +Needs reroll
ianthomas_uk’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new25.43 KB

This is a plain reroll of #4, so will likely fail tests

Status: Needs review » Needs work

The last submitted patch, 7: 2225427-7-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new3.48 KB
new25.39 KB

This should fix the most common failure

Status: Needs review » Needs work

The last submitted patch, 9: 2225427-9-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new25.3 KB
new2.48 KB

Fixed constructors to get LanguageListTest to pass.... these errors seem common to many other failings

Status: Needs review » Needs work

The last submitted patch, 11: 2225427-11-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new4.92 KB
new23.91 KB

buildForm() is actually called statically (even though it's not marked static - gotta love php), so should use the \Drupal wrapper.

ianthomas_uk’s picture

Title: Remove uses of deprecated language functions in object oriented code » Remove remaining uses of deprecated language functions (mostly in object oriented code)
StatusFileSize
new19.98 KB
new43.36 KB

Tweaked the title to capture any calls that have slipped back in after the earlier patches were committed, and here's a new patch with some more replacements (just a few left now that needed more thought).

Status: Needs review » Needs work

The last submitted patch, 14: 2225427-15-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new43.68 KB

Fix missing use statement in Drupal\node\Plugin\Search

Status: Needs review » Needs work

The last submitted patch, 16: 2225427-16-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new46.26 KB
new2.58 KB

This fixes the most common failure

Status: Needs review » Needs work

The last submitted patch, 18: 2225427-18-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new11.91 KB
new57.51 KB

This should replace all the remaining calls and fixes the error in #18. If it's green it will need reviews.

Status: Needs review » Needs work

The last submitted patch, 20: 2225427-20-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new61.65 KB
new4.14 KB

A couple of child classes needed updating

Status: Needs review » Needs work

The last submitted patch, 22: 2225427-22-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new2.65 KB
new64.3 KB

A couple of tests needed updating to mock the new dependencies

martin107’s picture

I have worked on this issue and am quite happy to work on it again... so just let me know if I can help out...

But for the moment putting my reviewers hat on and thinking.

There are several examples of procedural function calls, within classes .. the trend is to move to direct dependency injection unless in an .module of .install file etc

Here is example in the class \Drupal\Core\Session\UserSession

function getPreferredLangcode($default = NULL) {
    * {@inheritdoc}
    */
   function getPreferredAdminLangcode($default = NULL) {
-    $language_list = language_list();
+    $language_list = \Drupal::languageManager()->getLanguages();
     if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {

Where the languageManager service could be injected.

Also the config_translation.access.form service definition arguments could be updated to directly inject the languageManager into the class \Drupal\config_translation\Access\ConfigTranslationFormAccess.

ianthomas_uk’s picture

I've tried to inject the language manager in any functions that use the create(ContainerInterface) factory pattern, although it's quite possible I've missed some, I'll have a closer look later. We should also be able to easily add parameters to service definitions.

To inject it for other classes would need interfaces, which I didn't really want to do on an issue like this - I think that would be better done in issues that replace all calls to \Drupal() in a module at the same time, with a single change to the interface. I also found some methods that are being called statically, e.g. buildForm().

ianthomas_uk’s picture

StatusFileSize
new63.94 KB

I've had another look through and can't find any places where we could use injected objects without changing an API, so I think we should just go with the patch as it is for the purposes of this issue.

It did need a reroll however (something about a beta blocker getting committed), so here's an updated patch

Status: Needs review » Needs work

The last submitted patch, 27: 2225427-27-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new64.06 KB

Parameter order wasn't consistent between parent and child classes in #27

Status: Needs review » Needs work

The last submitted patch, 29: 2225427-29-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new64.34 KB

This should be the proper reroll of #24

Status: Needs review » Needs work

The last submitted patch, 31: 2225427-31-remove-language-oo.patch, failed testing.

ianthomas_uk’s picture

Status: Needs work » Needs review
StatusFileSize
new65.53 KB
new12.52 KB

try again. I can't generate an interdiff anymore, but the diffdiff looks sensible and the previously failing test now passes locally.

martin107’s picture

I have taken a slow, careful scan of this patch, it looks good to me

Nothing out of scope, all changes appropriate, a good cleanup.

I have worked on this so won't move to RTBC but +1 from me.

ParisLiakos’s picture

+++ b/core/lib/Drupal/Core/Session/UserSession.php
@@ -183,12 +183,12 @@ public function isAnonymous() {
+    $language_list = \Drupal::languageManager()->getLanguages();
...
+      return $default ? $default : \Drupal::languageManager()->getDefaultLanguage()->id;

@@ -196,12 +196,12 @@ function getPreferredLangcode($default = NULL) {
+    $language_list = \Drupal::languageManager()->getLanguages();
...
+      return $default ? $default : \Drupal::languageManager()->getDefaultLanguage()->id;

+++ b/core/modules/content_translation/src/Access/ContentTranslationManageAccessCheck.php
@@ -65,12 +65,12 @@ public function access(Route $route, Request $request, AccountInterface $account
+      $languages = \Drupal::languageManager()->getLanguages();
...
+          $source = \Drupal::languageManager()->getLanguage($source) ?: $entity->language();
+          $target = \Drupal::languageManager()->getLanguage($target) ?: \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);

@@ -80,7 +80,7 @@ public function access(Route $route, Request $request, AccountInterface $account
+          $language = \Drupal::languageManager()->getLanguage($language) ?: \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);

+++ b/core/modules/content_translation/src/ContentTranslationHandler.php
@@ -99,7 +99,7 @@ public function entityFormAlter(array &$form, array &$form_state, EntityInterfac
+    $languages = \Drupal::languageManager()->getLanguages();

@@ -132,7 +132,7 @@ public function entityFormAlter(array &$form, array &$form_state, EntityInterfac
+      foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE) as $language) {

@@ -145,7 +145,7 @@ public function entityFormAlter(array &$form, array &$form_state, EntityInterfac
+      foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE) as $language) {

+++ b/core/modules/content_translation/src/Controller/ContentTranslationController.php
@@ -29,8 +29,8 @@ public function overview(Request $request) {
+    $source = \Drupal::languageManager()->getLanguage($source);
+    $target = \Drupal::languageManager()->getLanguage($target);

+++ b/core/modules/user/src/Entity/User.php
@@ -367,13 +367,13 @@ public function getTimeZone() {
+    $language_list = \Drupal::languageManager()->getLanguages();
...
+      return $default ? $default : \Drupal::languageManager()->getDefaultLanguage()->id;

@@ -381,13 +381,13 @@ function getPreferredLangcode($default = NULL) {
+    $language_list = \Drupal::languageManager()->getLanguages();
...
+      return $default ? $default : \Drupal::languageManager()->getDefaultLanguage()->id;

lets store the language manager to a variable to avoid some extra \Drupal:: calls

penyaskito’s picture

Status: Needs review » Needs work

Needs work per #35.

martin107’s picture

Assigned: ianthomas_uk » martin107
StatusFileSize
new65.66 KB

Reroll before doing #35.

Lots of simple touches needed, but the sheer number of them, makes me think I will have to come back to this.

martin107’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 37: 2225427-37-remove-language-oo.patch, failed testing.

martin107’s picture

StatusFileSize
new65.66 KB
new2.08 KB

While merging with recent changes the order of the final parameters to ContentEntityDatabaseStorage::_construct(), cache and languageManager
has have been reversed. So all changes are of the form

-      ->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->languageManager, $this->cache))
+      ->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache, $this->languageManager))

This will not fix all errors. Just ContentEntityDatabaseStorageTest.php

martin107’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 40: 2225427-40-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new65.65 KB

Rats, #40 waited 11.5 hours in the queue.... in the mean time #2285083: Rename contact category to contact form was committed and caused a reroll.

in the end no drama, a single line fix to ConfigTranslationUiTest,

Status: Needs review » Needs work

The last submitted patch, 43: 2225427-43-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB
new65.69 KB

Fixed a couple of side issues, just posting what I have so far..I have not fixed the failing error yet!

a) This issue introduces a new test which contains the flaw that #2322889: Various setUp() and tearDown() methods are not protected fixed.

b) The name of the content entity type is now menu_link_content :-

-    $menu_items = \Drupal::entityManager()->getStorage('menu_link')->loadByProperties(array('route_name' => 'menu_test.context'));
+    $menu_items = \Drupal::entityManager()->getStorage('menu_link_content')->loadByProperties(array('route_name' => 'menu_test.context'));

This seems to be the correct thing to do, but now as I run this test through the debugger by the time the program reaches this line, the contents of the relevant table in the database are empty. So my problem has become why is the db not being populated correctly?

Status: Needs review » Needs work

The last submitted patch, 45: 2225427-45-remove-language-oo.patch, failed testing.

martin107’s picture

StatusFileSize
new61.65 KB

Patch no longer applied .. reroll.

As far as I can see all changes related to #1987882: Convert content_translation routes to a new style controller - anyway in all instances changes in head taken over changes in this patch - that was appropriate as only modern dependency injected function a calls were made in that patch.

So this patch get a little smaller.

martin107’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 47: 2225427-47-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB
new61.8 KB

I am finding stepping through MenuRouterRebuildTest a bit of a pain it takes several minutes to get to the point of interest.

I want to speed up the whole process before fixing the bug by moving from extending WebTestBase to KernelTestBase.

To pull some quotes form #2287223: Use KernelTestBase for config schema tests where possible

we can now turn some WebTestBase classes into KernelTestBase

None of these tests make web requests, they just deal with data and APIs.

extending KernelTestBase still makes MenuRouteRebuildTests an integration test, BUT I see a large speedup in test time.

The time taken to run MenuRouteRebuildTests from the command line drops from 28 seconds to 4 secs which should translate to an even bigger speedup when I start stepping through the debugger.

The question in the back of my mind is that I may have changed the setup of the test in some subtle way.
If reviewers could comment I would be grateful...

Status: Needs review » Needs work

The last submitted patch, 50: 2225427-50-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new60.7 KB

Lots has changed in the language module recently ... which is a good thing.

So this is a reroll... some things have become simplified.

Status: Needs review » Needs work

The last submitted patch, 52: 2225427-52-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new60.69 KB
new867 bytes

Ah rats, In the last comment lots of tests pasts... but I did not run a lint check of all files modified :)

now I have and there should be more more errors of this type

Status: Needs review » Needs work

The last submitted patch, 54: 2225427-54-remove-language-oo.patch, failed testing.

martin107’s picture

Status: Needs work » Needs review
StatusFileSize
new958 bytes
new59.76 KB

Reverted a line in DisplayPluginBase.php, which is now an unnecessary change.

Seems to be responsible for most of the exceptions.

Status: Needs review » Needs work

The last submitted patch, 56: 2225427-56-remove-language-oo.patch, failed testing.

martin107’s picture

Assigned: martin107 » Unassigned

Hmm back to the same stage as #50.

Not sure how to proceed, I think it maybe something trivial.

ianthomas_uk’s picture

Status: Needs work » Postponed
Related issues: +#2328293: Remove usage of language_list()

#2328293: Remove usage of language_list() has a much more up to date patch that covers language_list() and some injection. Let's get that reviewed and committed, then come back here to do the other functions.

gábor hojtsy’s picture

#2328293: Remove usage of language_list() is now RTBC. Let's get that in :)

gábor hojtsy’s picture

I mean and THEN get back here to work on the rest.

ianthomas_uk’s picture

Status: Postponed » Needs work
jeroent’s picture

Status: Needs work » Needs review
StatusFileSize
new35.66 KB

New attempt.

Function language and language_list are already removed so language_load and language_default are the only remaining.

Status: Needs review » Needs work

The last submitted patch, 63: remove_remaining_uses-2225427-63.patch, failed testing.

jeroent’s picture

Assigned: Unassigned » jeroent
jeroent’s picture

Status: Needs work » Needs review
StatusFileSize
new40.99 KB
new4.87 KB

Fixed the failing tests.

Status: Needs review » Needs work

The last submitted patch, 66: remove_remaining_uses-2225427-66.patch, failed testing.

jeroent’s picture

Assigned: jeroent » Unassigned
Status: Needs work » Needs review
StatusFileSize
new41.01 KB
new2.34 KB

There are still 4 failing tests in LanguageUILanguageNegotiationTest.

I will investigate this further tomorrow.

Status: Needs review » Needs work

The last submitted patch, 68: remove_remaining_uses-2225427-68.patch, failed testing.

gábor hojtsy’s picture

Issue tags: +sprint
jeroent’s picture

Assigned: Unassigned » jeroent
jeroent’s picture

Assigned: jeroent » Unassigned
Status: Needs work » Needs review
StatusFileSize
new41.06 KB
new808 bytes

I think I was half asleep last night.

gábor hojtsy’s picture

Issue tags: +SprintWeekend2015Queue

The key to review I think with the patch is that all the places you took the language manager from \Drupal, none of them had a language manager injected locally?

ianthomas_uk’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php
    @@ -132,15 +141,18 @@ static public function create(ContainerInterface $container, array $configuratio
    +  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, AccountInterface $account = NULL) {
         $this->database = $database;
         $this->entityManager = $entity_manager;
         $this->moduleHandler = $module_handler;
         $this->searchSettings = $search_settings;
         $this->account = $account;
    +    $this->languageManager = $language_manager;
    

    The ordering of the assignments doesn't match the ordering of the constructor parameters

  2. +++ b/core/modules/user/src/Entity/User.php
    @@ -378,7 +378,7 @@ function getPreferredLangcode($fallback_to_default = TRUE) {
    -      return $fallback_to_default ? language_default()->getId() : '';
    +      return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
    

    This could use $this->languageManager()

  3. +++ b/core/modules/user/src/Entity/User.php
    @@ -392,7 +392,7 @@ function getPreferredAdminLangcode($fallback_to_default = TRUE) {
    -      return $fallback_to_default ? language_default()->getId() : '';
    +      return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : '';
    

    This could use $this->languageManager()

Looks fine other than that - I couldn't see a way to do any more injection than we already are

gábor hojtsy’s picture

Issue tags: +SprintWeekend2015
jeroent’s picture

Status: Needs work » Needs review
StatusFileSize
new41.76 KB
new2.1 KB
lomo’s picture

StatusFileSize
new42.35 KB
new42.35 KB

Found one more call to \Drupal::languageManager() where we could use the injected service, instead. (Damn... the patch-76 should have been removed.)

develcuy’s picture

Issue tags: -SprintWeekend2015Queue
gábor hojtsy’s picture

@DevelCuy: the issue is still a perfect one to review on sprint weekend. :)

ianthomas_uk’s picture

Status: Needs review » Reviewed & tested by the community

Comments have been addressed, patch #77 looks good now

develcuy’s picture

Issue tags: +SprintWeekend2015Queue

Removed SprintWeekend2015Queue by mistake.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue is a prioritized change (deprecated functions) as per https://www.drupal.org/core/beta-changes and it's benefits outweigh any disruption. Committed c34c3df and pushed to 8.0.x. Thanks!

diff --git a/core/modules/config/src/Tests/ConfigLanguageOverrideTest.php b/core/modules/config/src/Tests/ConfigLanguageOverrideTest.php
index 9dac7f5..4970681 100644
--- a/core/modules/config/src/Tests/ConfigLanguageOverrideTest.php
+++ b/core/modules/config/src/Tests/ConfigLanguageOverrideTest.php
@@ -94,7 +94,7 @@ function testConfigLanguageOverride() {
     $this->assertIdentical($config->get('value'), array('key' => 'override'));
 
     // Ensure renaming the config will rename the override.
-    \Drupal::languageManager()->setConfigOverrideLanguage(language_load('en'));
+    \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('en'));
     \Drupal::configFactory()->rename('config_test.foo', 'config_test.bar');
     $config = \Drupal::config('config_test.bar');
     $this->assertEqual($config->get('value'), array('key' => 'original'));

Fixed on commit.

  • alexpott committed c34c3df on 8.0.x
    Issue #2225427 by ianthomas_uk, martin107, JeroenT, LoMo: Remove...
gábor hojtsy’s picture

Issue tags: -sprint

Fantastic, thanks! Settings good examples++

Status: Fixed » Closed (fixed)

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