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

CommentFileSizeAuthor
#2 3123211-2.patch588 bytesjungle

Comments

jungle created an issue. See original summary.

jungle’s picture

Status: Active » Needs review
StatusFileSize
new588 bytes

Did not touch usages in tests.

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.

kristen pol’s picture

Assigned: Unassigned » kristen pol

Assigning to myself for review.

kristen pol’s picture

Assigned: kristen pol » Unassigned
Status: Needs review » Reviewed & tested by the community

Thanks for the patch.

1) Changes looks fine.

2) Patch applies cleanly to 8.9, 9.0, and 9.1.

[mac:kristen:drupal-8.9.x-dev]$ patch -p1 < 3123211-2.patch 
patching file core/modules/menu_ui/src/MenuForm.php
[mac:kristen:drupal-9.0.x-dev]$ patch -p1 < 3123211-2.patch 
patching file core/modules/menu_ui/src/MenuForm.php
[mac:kristen:drupal-9.1.x-dev]$ patch -p1 < 3123211-2.patch 
patching file core/modules/menu_ui/src/MenuForm.php

3) Searching for \Drupal::menuTree():

./core/tests/Drupal/Tests/Core/DrupalTest.php:    $this->assertNotNull(\Drupal::menuTree());
./core/tests/Drupal/KernelTests/Core/Menu/MenuLinkDefaultIntegrationTest.php:    $tree = \Drupal::menuTree()->load('test', new MenuTreeParameters());
./core/tests/Drupal/KernelTests/Core/Menu/MenuLinkDefaultIntegrationTest.php:    $tree = \Drupal::menuTree()->load('test', new MenuTreeParameters());
./core/lib/Drupal/Core/Menu/menu.api.php: * $menu_tree = \Drupal::menuTree();
./core/modules/views_ui/tests/src/Functional/DisplayPathTest.php:    $result = \Drupal::menuTree()->load('admin', $parameters);
./core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeriverTest.php:    $menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeriverTest.php:    $menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeriverTest.php:    $menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentDeriverTest.php:    $menu_tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php:    $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
./core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php:    $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
./core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php:    $this->assertCount(0, \Drupal::menuTree()->load('menu_test', $menu_tree_condition));
./core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php:    $this->assertCount(1, \Drupal::menuTree()->load('menu_test', $menu_tree_condition_collection));
./core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php:    $menu_tree = \Drupal::menuTree()->load('menu_test', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/MenuLinkContentCacheabilityBubblingTest.php:    $menu_tree = \Drupal::menuTree();
./core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php:    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php:    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php:    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
./core/modules/system/tests/src/Functional/System/AdminTest.php:    $menu_tree = \Drupal::menuTree();
./core/modules/system/system.module:  $menu_tree = \Drupal::menuTree();
./core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php:    $tree = \Drupal::menuTree()->load('admin', new MenuTreeParameters());

I went through all of these to check if any could be changed to dependency injection but didn't see any.

4) Tests pass.

5) Marking RTBC.

  • xjm committed a71b641 on 9.1.x
    Issue #3123211 by jungle, Kristen Pol: Replace usages of \Drupal::...

  • xjm committed 65ada1d on 9.0.x
    Issue #3123211 by jungle, Kristen Pol: Replace usages of \Drupal::...

  • xjm committed 2723840 on 8.9.x
    Issue #3123211 by jungle, Kristen Pol: Replace usages of \Drupal::...
xjm’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: +Needs followup
  1. I confirmed that the menu tree service is already available in MenuForm and that we're calling it correctly:

    public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator, MenuLinkContentStorageInterface $menu_link_content_storage) {
      $this->menuLinkManager = $menu_link_manager;
      $this->menuTree = $menu_tree;
      $this->linkGenerator = $link_generator;
      $this->menuLinkContentStorage = $menu_link_content_storage;
    }
    
  2. I also confirmed that we still prefer \Drupal::whateverService() in tests to injecting the services, because of container rebuilds etc. So that means:

    [ibnsina:drupal | Sat 13:04:08] $ grep -r "Drupal::menuTree" * | grep -v "tests"
    core/lib/Drupal/Core/Menu/menu.api.php: * $menu_tree = \Drupal::menuTree();
    core/modules/system/system.module:  $menu_tree = \Drupal::menuTree();
  3. Since we don't have to inject the service anywhere new, this is a transparent change and can be backported during beta. (If we'd had to inject the service anywhere new, it'd be minor-only with a beta deadline.)
     

  4. One could make the case that menu.api.php should give an example of using the service properly rather than assuming the example is for procedural or test code, but rewriting docs is a separate issue scope. Tagging "needs followup" for that.

Committed to 9.1.x, and cherry-picked to 9.0.x and 8.9.x. Thanks!

jungle’s picture

Thanks @Kristen Pol for reviewing, and Thanks @xjm for committing!

Follow-up filed, hoping that I understood #9.4 correctly.

Status: Fixed » Closed (fixed)

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