Problem/Motivation

This issue has a non-test scope, which means don't convert test classes.

Classes which use t() should convert their usage to an injected translation service.

Proposed resolution

t() usages inside static class methods can't be injected, so leave those.

Other usages should ensure that the class uses Drupal\Core\StringTranslation\StringTranslationTrait, or otherwise has an implementation of $this->t().

Calls to t() in non-static methods should be changed to $this->t().

Remaining tasks

Eventually perform IoC for service traits such as StringTranslationTrait: #2733703: [plan] Service traits should require IoC injection

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kunal.kursija created an issue. See original summary.

kunalkursija’s picture

Status: Active » Needs review
FileSize
1.28 KB

Adding Patch.

minakshiPh’s picture

Assigned: Unassigned » minakshiPh

Status: Needs review » Needs work

The last submitted patch, 2: outside_in_module-2806983-2.patch, failed testing.

aditya.n’s picture

Assigned: minakshiPh » Unassigned
aditya.n’s picture

Adding the patch for the issue

Status: Needs review » Needs work

The last submitted patch, 6: drupal-core-replace-t-with-this-t-2806983-6-8.2.x.patch, failed testing.

kunalkursija’s picture

@androiditya

Static functions can be used without instantiation, Hence you cannot use "$this" inside static functions.

naveenvalecha’s picture

naveenvalecha’s picture

Category: Bug report » Task
Issue tags: +Novice
+++ b/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php
@@ -34,9 +38,12 @@ class SystemBrandingOffCanvasForm extends PluginFormBase implements ContainerInj
+  public function __construct(ConfigFactoryInterface $config_factory, TranslationInterface $string_translation) {
     $this->configFactory = $config_factory;
+    $this->stringTranslation = $string_translation;

This is a BC break, but as the module is in alpha then who cares ;)

amit.drupal’s picture

Mile23’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/outside_in/outside_in.module
    @@ -17,11 +19,11 @@
     function outside_in_help($route_name, RouteMatchInterface $route_match) {
    ...
    +      $output = '<h3>' . $this->t('About') . '</h3>';
    +      $output .= '<p>' . $this->t('The Settings Tray module provides an \'edit mode\' in which clicking on a block opens a slide-out tray which allows configuration to be altered without leaving the page.For more information, see the <a href=":outside-in-documentation">online documentation for the Settings Tray module</a>.', [':outside-in-documentation' => 'https://www.drupal.org/documentation/modules/outside_in']) . '</p>';
    +      $output .= '<h3>' . $this->t('Uses') . '</h3>';
    ...
    +      $output .= '<dt>' . $this->t('Editing blocks on the same page in the slide-out tray') . '</dt>';
    

    Unfortunately, hooks don't have a $this, much less $this->t(). So either leave the global t() or use $translation = \Drupal::translation() to get the service, and then call $translation->t().

  2. +++ b/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php
    @@ -15,6 +17,8 @@
    +  use StringTranslationTrait;
    
    @@ -34,9 +38,12 @@ class SystemBrandingOffCanvasForm extends PluginFormBase implements ContainerInj
    +    $this->stringTranslation = $string_translation;
    

    StringTranslationTrait has a setStringTranslation() method we should use during the constructor.

amit.drupal’s picture

Status: Needs work » Needs review
amit.drupal’s picture

Status: Needs review » Needs work
amit.drupal’s picture

@Mile23 Thanks for review code.

The last submitted patch, 11: 2806983-11.patch, failed testing.

amit.drupal’s picture

@naveenvalecha Please Suggest me issues in 2806983-11.patch

naveenvalecha’s picture

#11, Please do test on local before posting, Did you even test the patch on d.o.

+++ b/core/modules/outside_in/outside_in.module
@@ -17,11 +19,11 @@
-      $output = '<h3>' . t('About') . '</h3>';
-      $output .= '<p>' . t('The Settings Tray module provides an \'edit mode\' in which clicking on a block opens a slide-out tray which allows configuration to be altered without leaving the page.For more information, see the <a href=":outside-in-documentation">online documentation for the Settings Tray module</a>.', [':outside-in-documentation' => 'https://www.drupal.org/documentation/modules/outside_in']) . '</p>';
-      $output .= '<h3>' . t('Uses') . '</h3>';
+      $output = '<h3>' . $this->t('About') . '</h3>';
+      $output .= '<p>' . $this->t('The Settings Tray module provides an \'edit mode\' in which clicking on a block opens a slide-out tray which allows configuration to be altered without leaving the page.For more information, see the <a href=":outside-in-documentation">online documentation for the Settings Tray module</a>.', [':outside-in-documentation' => 'https://www.drupal.org/documentation/modules/outside_in']) . '</p>';
+      $output .= '<h3>' . $this->t('Uses') . '</h3>';
       $output .= '<dl>';
-      $output .= '<dt>' . t('Editing blocks on the same page in the slide-out tray') . '</dt>';
+      $output .= '<dt>' . $this->t('Editing blocks on the same page in the slide-out tray') . '</dt>';
       $output .= '</dl>';

Unreleated changes.

#12.2
yup nice spot :)

amit.drupal’s picture

@naveenvalecha Thanks
Next time improve my coding.

minakshiPh’s picture

Assigned: Unassigned » minakshiPh
keshavv’s picture

Here is the patch please review

minakshiPh’s picture

Assigned: minakshiPh » Unassigned
Status: Needs work » Needs review
FileSize
3.31 KB
3.12 KB

corrected the patch as mentioned in #12

Kindly review.
Thanks!

Status: Needs review » Needs work

The last submitted patch, 22: fix-issue-2806983-22.patch, failed testing.

Mile23’s picture

Thanks. :-)

+++ b/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php
@@ -34,9 +38,12 @@ class SystemBrandingOffCanvasForm extends PluginFormBase implements ContainerInj
+    setStringTranslation($string_translation);

The trait adds setStringTranslation() to $this, so it would be $this->setStringTranslation($string_translation);

minakshiPh’s picture

Assigned: Unassigned » minakshiPh
minakshiPh’s picture

Assigned: minakshiPh » Unassigned
Status: Needs work » Needs review
FileSize
3.31 KB
586 bytes

Hi @Mile23,

Thanks for reviewing my patch.

Have corrected the minor change as mentioned in #24.

Kindly review.
Thanks!

naveenvalecha’s picture

+++ b/core/modules/outside_in/src/Cache/Context/OutsideInCacheContext.php
@@ -34,7 +34,7 @@ public function __construct(OutsideInManagerInterface $outside_in_manager) {
+    return $this->t('Settings Tray');

This will make the test fail b/c the $this->t() function will not be available here.

tedbow’s picture

Status: Needs review » Needs work

@minakshiPh Settings needs work because of #27

minakshiPh’s picture

Assigned: Unassigned » minakshiPh
minakshiPh’s picture

Assigned: minakshiPh » Unassigned
Status: Needs work » Needs review
FileSize
2.75 KB
327 bytes

Hi @tedbow,

Change mentioned in #27 is done and attached the new patch.

Kindly review.
Thanks!

pmchristensen’s picture

Status: Needs review » Reviewed & tested by the community

Looking really good - reviewed and tested. It should perhaps be considered to do something about the skipping tests?

Starting test 'Drupal\Tests\outside_in\FunctionalJavascript\OffCanvasTest::testOffCanvasLinks'.
S
Starting test 'Drupal\Tests\outside_in\FunctionalJavascript\OutsideInBlockFormTest::testBlocks'.
S

The last submitted patch, 21: 2806983-21.patch, failed testing.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Thank you for your work on cleaning up Drupal core's use of deprecated APIs!

In order to deprecate APIs in a maintainable way, converting deprecated uses should be replaced across all of core for a given kind of usage, rather than in individual modules or files. Such issues should also always be part of an overall plan to ensure all usages are removed, rather than standalone patches. Such a plan may already exist for t() so this issue should probably be closed in favor of that.

For background information on why we usually will not commit cleanups that aren't scoped in that way, see the core issue scope guidelines. See #2575081: [policy, no patch] Use E_USER_DEPRECATED in Drupal 8 minor releases for more information on how we plan to manage deprecations in the future.

Contributing to the overall plan above will help ensure that your cleanups for core's deprecated code improve core in a maintainable and minimally disruptive way.

For this issue, we should start by replacing non-test t() usages in all classes where the translation trait or service is already available. This can be done with a script. We would then have a followup to inject the service in classes where it is not already available and convert usages there. Finally, we should have a separate issue to discuss the use of t() in tests. We probably should close this issue as a duplicate, but setting to needs work for now so that contributors see this information. Thanks!

You might want to help contribute to this issue instead: #2549805: Remove all usage of FormattableMarkup in tests apart from explicit tests of that API

ZeiP’s picture

Issue tags: -Novice
tedbow’s picture

Status: Needs work » Closed (won't fix)

Closing as per @xjm comments in #33

Such a plan may already exist for t() so this issue should probably be closed in favor of that.

If anybody knows of such as issue please link to it from here.

Mile23’s picture

Title: outside_in module : Replace usage of t() with $this->t() » Replace non-test usage of t() with injected service
Version: 8.2.x-dev » 8.3.x-dev
Component: outside_in.module » other
Issue summary: View changes
Status: Closed (won't fix) » Needs work

@xjm: For this issue, we should start by replacing non-test t() usages in all classes where the translation trait or service is already available. This can be done with a script.

Can't be done with a script. Someone is welcome to prove me wrong, however.

I don't think the larger-scope issue exists since I couldn't find it. I'm re-opening and re-scoping here, since we already have a patch here.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

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.

manishsaharan’s picture

Status: Needs work » Needs review
FileSize
3.16 KB

Fixed this with 9.2.x

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 » Closed (duplicate)