Problem/Motivation

Follow up #2729597: [meta] Replace \Drupal with injected services where appropriate in core and see it for more details. And take this fixed issue and its commit as an example.

Proposed resolution

Inject redirect.destination service instead of using \Drupal:: destination() in non-tests where possible

See CR https://www.drupal.org/node/3343983

Command to get the target classes to work with:

$ grep -nR '\\Drupal::destination()' . | grep -v Test | grep -v "\.module" | grep -v '\.inc' | grep -v 'Trait\.php' | grep -v 'api.php' | grep -v SystemCompactLink | grep -v RedirectDestinationInterface | grep -v UserLoginBlock

Note: Usages in SystemCompactLink and UserLoginBlock are inside static methods. And usage in RedirectDestinationInterface is in the comment.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3123214

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.

nitesh624’s picture

Assigned: Unassigned » nitesh624
nitesh624’s picture

StatusFileSize
new11.74 KB
nitesh624’s picture

nitesh624’s picture

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

Status: Needs review » Needs work

Thanks for working on this.

It's not just replacing \Drupal::destination() with \Drupal::service('redirect.destination')

It's to replace \Drupal::destination() and \Drupal::service('redirect.destination') with IoC injection where possible

Please see the parent issue and closed/fixed sibling issues for reference and for more.

nitesh624’s picture

Assigned: Unassigned » nitesh624
nitesh624’s picture

StatusFileSize
new11.31 KB
new12.99 KB

Changes done as per #7

nitesh624’s picture

Status: Needs work » Needs review
nitesh624’s picture

Assigned: nitesh624 » Unassigned
nitesh624’s picture

StatusFileSize
new12.54 KB
new4.9 KB
nitesh624’s picture

Assigned: Unassigned » nitesh624
Status: Needs review » Needs work
nitesh624’s picture

StatusFileSize
new15.48 KB
new15.23 KB
nitesh624’s picture

StatusFileSize
new11.74 KB
nitesh624’s picture

Assigned: nitesh624 » Unassigned
Status: Needs work » Needs review
nitesh624’s picture

Assigned: Unassigned » nitesh624
Status: Needs review » Needs work
nitesh624’s picture

StatusFileSize
new15.05 KB
nitesh624’s picture

StatusFileSize
new13.89 KB
nitesh624’s picture

StatusFileSize
new11.74 KB

Reuploading patch from #15 as IOC injection is resulting error

nitesh624’s picture

Assigned: nitesh624 » Unassigned
Status: Needs work » Needs review
jungle’s picture

Title: Replace usages of \Drupal::destination() with IoC injection » Replace non-test usages of \Drupal::destination() 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!

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new10.4 KB

Patch did not applied so re-rolled and also removed changes from tests. Please review.

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.

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.

ranjith_kumar_k_u’s picture

StatusFileSize
new10.38 KB

Rerolled #23

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Seems there still two instances of
\Drupal::destination()->getAsArray
That should be replaced.

mrinalini9’s picture

Status: Needs work » Needs review
StatusFileSize
new11.79 KB
new2.34 KB

Updated patch #29 by addressing #30, please review it.

Thanks!

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems all instances have been replaced.

jungle’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Render/Element/SystemCompactLink.php
@@ -66,7 +66,7 @@ public static function preRenderCompactLink($element) {
-        'query' => \Drupal::destination()->getAsArray(),
+        'query' => \Drupal::service('redirect.destination')->getAsArray(),

+++ b/core/modules/workspaces/workspaces.module
@@ -227,7 +227,7 @@ function workspaces_toolbar() {
-      '#url' => Url::fromRoute('entity.workspace.collection', [], ['query' => \Drupal::destination()->getAsArray()]),
+      '#url' => Url::fromRoute('entity.workspace.collection', [], ['query' => \Drupal::service('redirect.destination')->getAsArray()]),

All changes should be reverted and are invalid. Replacing \Drupal::destination() with \Drupal::service('redirect.destination') does not in scope here and makes no sense. They are almost the same. And it's not using IoC injection.

An example coming in the next comment.

jungle’s picture

Taking core/modules/views/src/Plugin/views/field/Links.php as an example.

diff --git a/core/modules/views/src/Plugin/views/field/Links.php b/core/modules/views/src/Plugin/views/field/Links.php
index 899d082504..2f4d975218 100644
--- a/core/modules/views/src/Plugin/views/field/Links.php
+++ b/core/modules/views/src/Plugin/views/field/Links.php
@@ -4,7 +4,9 @@
 
 use Drupal\Component\Utility\Html;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Routing\RedirectDestinationInterface;
 use Drupal\Core\Url as UrlObject;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * An abstract handler which provides a collection of links.
@@ -13,6 +15,26 @@
  */
 abstract class Links extends FieldPluginBase {
 
+  /**
+   * Constructs a Links object.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\Core\Routing\RedirectDestinationInterface|null $redirectDestination
+   *   The redirect destination service.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, protected ?RedirectDestinationInterface $redirectDestination = NULL) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    if ($redirectDestination === NULL) {
+      $this->redirectDestination = \Drupal::destination();
+      @trigger_error('Calling ' .  __METHOD__ . '() without the $redirectDestination argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/CHANGE-RECORD-NODE-ID', E_USER_DEPRECATED);
+    }
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -20,6 +42,18 @@ public function usesGroupBy() {
     return FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      $container->get('redirect.destination')
+    );
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -84,7 +118,7 @@ protected function getLinks() {
         'title' => $title,
       ];
       if (!empty($this->options['destination'])) {
-        $links[$field]['query'] = \Drupal::destination()->getAsArray();
+        $links[$field]['query'] = $this->redirectDestination->getAsArray();
       }
     }

Note:

1. I might pick the wrong class, as the example. It's an abstract class, not a normal one. Not sure if abstract classes should be handled specially. Please just treat it as a normal class.

+      @trigger_error('Calling ' .  __METHOD__ . '() without the $redirectDestination argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/CHANGE-RECORD-NODE-ID', E_USER_DEPRECATED);

2. A change record is required, and please update the placeholder CHANGE-RECORD-NODE-ID with its node ID.

3. IoC is class specific, do not touch .module files etc.

jungle’s picture

See parent issue for more details.

jungle’s picture

Continue with #34

 public function __construct(array $configuration, $plugin_id, $plugin_definition, protected ?RedirectDestinationInterface $redirectDestination = NULL) {

4. Constructor property promotion is used. see https://stitcher.io/blog/constructor-promotion-in-php-8

jungle’s picture

Title: Replace non-test usages of \Drupal::destination() with IoC injection » Inject redirect.destination service instead of using \Drupal:: destination() in non-tests
Issue summary: View changes

Switching to MR and re-wording the title without the term IoC which some contributors might be unfamiliar with. And updating the issue summary to add a fixed issue and its commit as an example.

jungle’s picture

jungle’s picture

Issue summary: View changes
jungle’s picture

Issue summary: View changes

Add Command to get classes to work with to IS

jungle’s picture

Issue summary: View changes
Status: Needs work » Needs review
$ grep -nR '\\Drupal::destination()' . | grep -v Test | grep -v "\.module" | grep -v '\.inc' | grep -v 'Trait\.php' | grep -v 'api.php' | grep -v SystemCompactLink | grep -v RedirectDestinationInterface | grep -v UserLoginBlock

The command is used to find the target classes.

Note: Usages in SystemCompactLink and UserLoginBlock are inside static methods. And usage in RedirectDestinationInterface is in the comment.

jungle’s picture

Issue summary: View changes

CR updated

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @jungle!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 9ed7086 and pushed to 10.1.x. Thanks!

  • alexpott committed 9ed7086e on 10.1.x
    Issue #3123214 by nitesh624, jungle, mrinalini9, Suresh Prabhu Parkala,...

Status: Fixed » Closed (fixed)

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

amateescu’s picture

FWIW, the changes to the workspace forms from this issue were not correct, see #3376293-12: WorkspacePublishForm $redirectDestination parameter appears not to be used.

quietone’s picture

Published the CR.