Use dependency injection for redirect classes

Created on 19 November 2018, about 6 years ago
Updated 8 August 2023, over 1 year ago

Problem/Motivation

\Drupal calls should be avoided in classes, and we should use dependency injection instead.

Currently, redirect's classes use \Drupal style calls in the following places:

  1. src/Entity/Redirect.php: return array(\Drupal::currentUser()->id());
  2. src/EventSubscriber/RedirectRequestSubscriber.php: \Drupal::logger('redirect')->warning($e->getMessage());
  3. src/Form/RedirectForm.php: foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE) as $langcode => $language) {
  4. src/Form/RedirectForm.php: $default_code = $redirect->getStatusCode() ? $redirect->getStatusCode() : \Drupal::config('redirect.settings')->get('default_status_code');
  5. src/Form/RedirectForm.php: $redirects = \Drupal::entityManager()
  6. src/Form/RedirectSettingsForm.php: '#disabled' => !\Drupal::moduleHandler()->moduleExists('path'),
  7. src/Plugin/Field/FieldWidget/RedirectSourceWidget.php: \Drupal::service('router')->match('/' . $form_state->getValue(array('redirect_source', 0, 'path')));
  8. src/Plugin/Field/FieldWidget/RedirectSourceWidget.php: $repository = \Drupal::service('redirect.repository');
  9. src/RedirectRepository.php: $base_url = \Drupal::request()->getBaseUrl();

Proposed resolution

Define constructors and create() methods to properly inject all dependencies.

Remaining tasks

  • All in one.
  • Not needed.
  • Fix the patch to deal with all of them.
  • Reviews + testing

User interface changes

None.

API changes

Defining constructors and ::create() methods for various redirect-related classes.

Data model changes

None.

๐Ÿ“Œ Task
Status

RTBC

Version

1.0

Component

Code

Created by

๐Ÿ‡ง๐Ÿ‡ทBrazil thalles Teรณfilo Otoni - MG

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States Kristen Pol Santa Cruz, CA, USA

    Assigning to myself as I'm triaging all RTBC issues.

  • Issue was unassigned.
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States Kristen Pol Santa Cruz, CA, USA

    Thanks, everyone, for the hard work for some time to get this patch in order.

    1. Regarding the t() in #52, we can create a new issue for that. Also, coding standard changes that are unrelated to this issue can be left out. I didn't see any obvious ones in this patch but did in the one in 2 below.

    2. This related patch should be included here IMO (see comment #6 for an explanation):

    ๐Ÿ“Œ Use dependency injection on redirect\Form\RedirectForm Postponed

    3. The patch needs a reroll:

    Kristens-MacBook-Pro:redirect kristenpol$ patch -p1 < 22.diff 
    patching file modules/redirect_404/redirect_404.services.yml
    patching file modules/redirect_404/src/SqlRedirectNotFoundStorage.php
    patching file modules/redirect_404/tests/src/Unit/SqlRedirectNotFoundStorageTest.php
    Hunk #3 FAILED at 44.
    Hunk #4 succeeded at 51 (offset -2 lines).
    Hunk #5 succeeded at 61 (offset -2 lines).
    Hunk #6 succeeded at 76 (offset -2 lines).
    1 out of 6 hunks FAILED -- saving rejects to file modules/redirect_404/tests/src/Unit/SqlRedirectNotFoundStorageTest.php.rej
    patching file redirect.services.yml
    patching file src/EventSubscriber/RedirectRequestSubscriber.php
    patching file src/Form/RedirectForm.php
    patching file src/Form/RedirectSettingsForm.php
    patching file src/Plugin/Field/FieldWidget/RedirectSourceWidget.php
    Hunk #1 succeeded at 3 with fuzz 2 (offset -1 lines).
    Hunk #2 succeeded at 33 (offset -1 lines).
    Hunk #3 succeeded at 122 (offset -1 lines).
    Hunk #4 succeeded at 138 (offset -1 lines).
    patching file src/RedirectChecker.php
    Hunk #1 FAILED at 4.
    1 out of 2 hunks FAILED -- saving rejects to file src/RedirectChecker.php.rej
    patching file src/RedirectRepository.php
    patching file tests/src/Unit/RedirectRequestSubscriberTest.php
    Hunk #1 succeeded at 14 with fuzz 2 (offset -1 lines).
    Hunk #2 succeeded at 155 (offset -9 lines).
    Hunk #3 succeeded at 166 (offset -9 lines).
    

    4. Please don't add company specific tags unless it's around a public event.

    5. Hiding patches since we are using an MR now.

  • First commit to issue fork.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    63 pass
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil elber Brazil

    Hi I rerrolled please revise

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    timestamp is an unusual name for the time server, core uses just $time.

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    63 pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    63 pass
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil elber Brazil
  • First commit to issue fork.
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    57 pass, 6 fail
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    55 pass, 9 fail
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    60 pass, 5 fail
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    60 pass, 5 fail
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    63 pass
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    52 pass, 4 fail
  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 10 months ago
    63 pass
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rahulrasgon

    MR is ready to review.
    Thanks!!

  • Status changed to RTBC 6 months ago
  • gaurav gupta Jaipur, Rajasthsan

    Hello
    I have tested the MR and the issue as been solved but there are still phpcs error and remaining dependency injection error for which we can create a new issue in D.O.
    Thanks

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    This has conflicts now.

  • Assigned to zniki.ru
  • ๐Ÿ‡ท๐Ÿ‡บRussia zniki.ru
  • Pipeline finished with Success
    15 days ago
    Total: 182s
    #358000
  • Pipeline finished with Success
    15 days ago
    Total: 178s
    #358014
  • Pipeline finished with Success
    15 days ago
    Total: 230s
    #358194
  • Pipeline finished with Success
    15 days ago
    Total: 201s
    #358200
  • Pipeline finished with Failed
    15 days ago
    Total: 184s
    #358230
  • Pipeline finished with Failed
    15 days ago
    Total: 181s
    #358231
  • Pipeline finished with Success
    15 days ago
    Total: 196s
    #358237
  • ๐Ÿ‡ท๐Ÿ‡บRussia zniki.ru

    Conflicts fixed.
    Remove unwanted changes from MR, such as comments for properties.
    Made changes for RedirectSettingsForm constructor.
    Looks like it's ready for review.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    Nice work @nikolay shapovalov! Just had a look at the code and LGTM!

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    I'm wondering if we want to use some more modern features at this point, like constructor property promotion, which requires more modern PHP versions. And maybe autowire, but I think that doesn't work yet for plugins and forms, just controllers.

    I'd be OK with either requiring a 10.x version at this point (e.g. 10.1 or 10.2 depending on the features we want), that also implies PHP 8.1+. That should probably be done in a separate issue, first.

  • ๐Ÿ‡ท๐Ÿ‡บRussia zniki.ru

    Using new features is great idea.
    There are 19 matches for __construct methods in code base now, and we are going to change 9 of them.
    It will be much easy to review, if property promotions will be made in separate issue.

Production build 0.71.5 2024