Problem/Motivation

Part of effort #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() that concerns removal of drupal_static() and drupal_static_reset() from locale_translation_get_projects() and locale_translation_clear_cache_projects().

The static cache of locale_translation_get_projects() looks almost the same as \Drupal\locale\LocaleProjectStorage::$cache. However, there are 3 differences:

  1. The items returned by the first are objects (\stdClass) while the latter are arrays.
  2. locale_translation_get_projects() performs an additional task to ensure at least the core project.
  3. locale_translation_get_projects() accepts a list of projects to filter on.

Proposed resolution

  • Create a new method LocaleProjectStorageInterface::getProjects providing the functionality of locale_translation_get_projects().
  • Deprecate locale_translation_get_projects() & locale_translation_clear_cache_projects().

Remaining tasks

None.

User interface changes

None.

API changes

  • New method LocaleProjectStorageInterface::getProjects().
  • locale_translation_get_projects() & locale_translation_clear_cache_projects() are deprecated.

.

Data model changes

None.

Release notes snippet

N/A

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Title: Deprecate locale_translation_get_projects() move it in locale.project service » Deprecate locale_translation_get_projects() and locale_translation_clear_cache_projects()
Issue summary: View changes
Status: Active » Needs review
Issue tags: +@deprecated
StatusFileSize
new67.59 KB

Patch

andypost’s picture

Please remove composer changes from patch

claudiu.cristea’s picture

StatusFileSize
new17.48 KB

Ouch! Thank you @andypost :)

The last submitted patch, 2: 3036982-2.patch, failed testing. View results

andypost’s picture

Status: Needs review » Needs work
+++ b/core/modules/locale/tests/src/Functional/LocaleUpdateTest.php
@@ -328,7 +328,7 @@ public function testEnableUninstallModule() {
+    $projects = $this->container->get('locale.project')->getProjects();

+++ b/core/modules/locale/tests/src/Kernel/LocaleTranslationProjectsTest.php
@@ -42,22 +42,22 @@ protected function setUp() {
+    $this->assertIdentical($expected, $this->container->get('locale.project')->getProjects());
...
+    $this->assertEqual($expected, $this->container->get('locale.project')->getProjects());
...
+    $this->assertEqual($expected, $this->container->get('locale.project')->getProjects());

please use \Drupal::service(), as there's no conclusion on this yet, this is recommended way to prevent issues on container rebuild in tests

andypost’s picture

claudiu.cristea’s picture

Status: Needs work » Needs review
StatusFileSize
new17.46 KB
new2.18 KB

Fixed.

claudiu.cristea’s picture

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.

andypost’s picture

  1. +++ b/core/includes/install.core.inc
    @@ -1792,11 +1792,13 @@ function _install_prepare_import($langcodes, $server_pattern) {
    -          \Drupal::service('locale.project')->set($data['name'], $data);
    +          /** @var \Drupal\locale\LocaleProjectStorageInterface $locale_project_storage */
    +          $locale_project_storage = \Drupal::service('locale.project');
    +          $locale_project_storage->set($data['name'], $data);
               module_load_include('compare.inc', 'locale');
               // Reset project information static cache so that it uses the data
               // set above.
    -          locale_translation_clear_cache_projects();
    +          $locale_project_storage->resetCache();
    
    +++ b/core/modules/locale/locale.compare.inc
    @@ -80,10 +83,10 @@ function locale_translation_build_projects() {
    -    \Drupal::service('locale.project')->set($project->name, $data);
    +    $locale_project_storage->set($project->name, $data);
     
         // Invalidate the cache of translatable projects.
    -    locale_translation_clear_cache_projects();
    +    $locale_project_storage->resetCache();
    
    +++ b/core/modules/locale/tests/src/Kernel/LocaleTranslationProjectsTest.php
    @@ -42,22 +42,22 @@ protected function setUp() {
         $this->projectStorage->set('bar', []);
    -    locale_translation_clear_cache_projects();
    +    \Drupal::service('locale.project')->resetCache();
    

    btw reset cache only used when some data is set, so probably this method could be no-op and reset cache inside set()

  2. +++ b/core/modules/locale/locale.translation.inc
    @@ -50,40 +50,27 @@
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
    ...
    +  @trigger_error('locale_translation_get_projects() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\locale\LocaleProjectStorageInterface::getProjects() instead. See https://www.drupal.org/node/3036986.', E_USER_DEPRECATED);
    ...
    + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
    ...
    +  @trigger_error('locale_translation_clear_cache_projects() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\locale\LocaleProjectStorageInterface::resetCache() instead. See https://www.drupal.org/node/3036986.', E_USER_DEPRECATED);
    
    +++ b/core/modules/locale/tests/src/Unit/LocaleProjectStorageTest.php
    @@ -0,0 +1,46 @@
    +   * @expectedDeprecation locale_translation_get_projects() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\locale\LocaleProjectStorageInterface::getProjects() instead. See https://www.drupal.org/node/3036986.
    ...
    +   * @expectedDeprecation locale_translation_clear_cache_projects() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\locale\LocaleProjectStorageInterface::resetCache() instead. See https://www.drupal.org/node/3036986.
    

    should be 8.8.0 now

claudiu.cristea’s picture

StatusFileSize
new19.3 KB
new7.69 KB

Good point. However, we need to keep also resetCache() as it is, a 3rd party module might have used this drupal_static_reset('locale_translation_get_projects').

andypost’s picture

+++ b/core/modules/locale/src/LocaleProjectStorage.php
@@ -166,4 +176,32 @@ public function getAll() {
+  public function getProjects(array $limit_to_projects = NULL) {
...
+      if ($row_count == 0) {
+        module_load_include('compare.inc', 'locale');
+        // At least the core project should be in the database, so we build the
+        // data if none are found.
+        locale_translation_build_projects();

Let's move it to private|protected method cos this will be refactored soon and makes this class unit testable

claudiu.cristea’s picture

Let's move it to private|protected method cos this will be refactored soon and makes this class unit testable

Move what, the whole method?

andypost’s picture

Move part which using to load inc file for constant and function

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.

hardik_patel_12’s picture

Issue tags: +Bug Smash Initiative
StatusFileSize
new19.32 KB
new10.29 KB

"deprecated in Drupal 8.8.0 " should be incremented to 9.1.0" and also changed to "and will be removed from Drupal 10". Kindly review a new patch.

Status: Needs review » Needs work

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

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new19.32 KB
new546 bytes

Solving test case.

andypost’s picture

Probably this is great place for memory cache

hardik_patel_12’s picture

Issue tags: -Bug Smash Initiative

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.

sutharsan’s picture

Status: Needs review » Needs work

The conversion to service methods is a good step. But in my opinion this patch makes it too complicated by mixing non-essential refactoring into it.

  1. +++ b/core/includes/bootstrap.inc
    @@ -614,6 +614,12 @@ function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
    +      @trigger_error("Using drupal_static_reset() with 'locale_translation_get_projects' as parameter is deprecated in Drupal 9.1.0 and will be removed in Drupal 10.0.0. See https://www.drupal.org/node/3036986.", E_USER_DEPRECATED);
    

    For this and other deprecation messages, Drupal version should be updated. Also the message pattern should follow https://www.drupal.org/project/coding_standards/issues/3024461.

  2. +++ b/core/includes/install.core.inc
    @@ -1781,11 +1781,10 @@ function _install_prepare_import($langcodes, $server_pattern) {
    -          \Drupal::service('locale.project')->set($data['name'], $data);
    +          /** @var \Drupal\locale\LocaleProjectStorageInterface $locale_project_storage */
    +          $locale_project_storage = \Drupal::service('locale.project');
    +          $locale_project_storage->set($data['name'], $data);
    

    This refactoring is personal preference. Keep this refactoring out of this issue, it makes the patch harder to review.

  3. +++ b/core/modules/locale/locale.compare.inc
    @@ -46,8 +46,11 @@ function locale_translation_build_projects() {
    +  /** @var \Drupal\locale\LocaleProjectStorageInterface $locale_project_storage */
    +  $locale_project_storage = \Drupal::service('locale.project');
    +
       // Mark all previous projects as disabled and store new project data.
    -  \Drupal::service('locale.project')->disableAll();
    +  $locale_project_storage->disableAll();
     
    

    This refactoring is personal preference. Keep it out of this issue, it makes review harder.

  4. +++ b/core/modules/locale/locale.compare.inc
    @@ -80,10 +83,7 @@ function locale_translation_build_projects() {
    -    \Drupal::service('locale.project')->set($project->name, $data);
    

    Ditto

  5. +++ b/core/modules/locale/src/LocaleProjectStorage.php
    @@ -98,6 +105,7 @@ public function setMultiple(array $data) {
    +    $this->cacheAsObjects = [];
    

    I'd rather see this replaced by a ::clearObjectCache() method. That will tell _why_ this line is there.

  6. +++ b/core/modules/locale/src/LocaleProjectStorage.php
    @@ -166,4 +176,32 @@ public function getAll() {
    +      $this->cacheAsObjects = array_map(function ($project) {
    +        return (object) $project;
    +      }, $this->getAll());
    +    }
    

    This issue is not the place for this refactoring. It makes the issue more difficult to refactor.

  7. +++ b/core/modules/locale/src/LocaleProjectStorage.php
    @@ -166,4 +176,32 @@ public function getAll() {
    +    // Limit to passed list, if case.
    +    if ($limit_to_projects) {
    +      return array_intersect_key($this->cacheAsObjects, array_flip($limit_to_projects));
    

    The comment and the variable name have changed. Do not refactor in this issue, it makes it harder to review.

  8. +++ b/core/modules/locale/src/LocaleProjectStorageInterface.php
    @@ -99,4 +99,17 @@ public function resetCache();
    +   *
    +   * @param array|null $limit_to_projects
    +   *   (optional) If passed, the result will be filtered on this list.
    +   *
    +   * @return array
    +   *   Array of project data for translation update.
    +   *
    +   * @see locale_translation_build_projects()
    +   */
    +  public function getProjects(array $limit_to_projects = NULL);
    +
    

    A part of the comment got removed, the param description was changed, function signature (default value) was changed. This makes the patch harder to review.

ravi.shankar’s picture

StatusFileSize
new19.31 KB

Added reroll of patch #20 for Drupal 9.3.x.

ravi.shankar’s picture

Need to work on custom commands fails also.

ravi.shankar’s picture

StatusFileSize
new18.35 KB
new3.88 KB

Here I have tried to address the custom command fails of patch #26.

Also tried to address #25.1, 2, 3, and 4 points of comment #25.

Still needs work for remaining points.

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.

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.

nicxvan’s picture

Title: Deprecate locale_translation_get_projects() and locale_translation_clear_cache_projects() » [pp-1] Deprecate locale_translation_get_projects() and locale_translation_clear_cache_projects()
Status: Needs work » Postponed
Related issues: +#3569328: Modernize locale.translations.inc

I think this should be closed as a duplicate in favor of the newer one.

Usually we'd update the older one with more history, but I think there is a need to do the full file in one go since there are circular dependencies.

I'll just postpone this for now.

nicxvan’s picture

Status: Postponed » Closed (duplicate)

Thank you everyone for working on this. I'm going to close this as a duplicate since I think the other one will likely get in first.

All closed issues now grant credit and I've taken care of adding credit for contributions here thanks!

Typically we would use the older issue but somehow I missed this one when I searched and the other is nearly RTBC

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.