Problem/Motivation

Follow up #2729597: [meta] Replace \Drupal with injected services where appropriate in core

Some classes does not inject form builder service via DI

Proposed resolution

Replace all of them with IoC injection where possible

Specifically UserLoginBlock and ActionListBuilder

Following should stay as is for BC

core/lib/Drupal/Core/Config/Entity/DraggableListBuilder.php:25:    $this->formBuilder = \Drupal::formBuilder();
core/modules/views/src/Plugin/views/display/DisplayPluginBase.php:2263:      $form = \Drupal::formBuilder()->getForm($form_object, $view, $output);
core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php:134:    $form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state);

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3123209

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

@jungle In core i have seen that many place \Drupal::formBuilder() has been used in comments. so do we consider changing there also.

nitesh624’s picture

StatusFileSize
new33.99 KB
nitesh624’s picture

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

StatusFileSize
new84.36 KB
new46.1 KB

Replace occurance of \Drupal::formBuilder() with IoC injection in comments also

nitesh624’s picture

StatusFileSize
new52.76 KB
new17.37 KB
new28.8 KB

Removing the unnecessary changes from #7

nitesh624’s picture

jungle’s picture

Status: Needs review » Needs work

Thanks for working on this.

+++ b/core/authorize.php
@@ -175,7 +175,7 @@ function authorize_access_allowed(Request $request) {
-        $content = \Drupal::formBuilder()->getForm('Drupal\Core\FileTransfer\Form\FileTransferAuthorizeForm');
+        $content = \Drupal::service('form_builder')->getForm('Drupal\Core\FileTransfer\Form\FileTransferAuthorizeForm');

It's not just replacing \Drupal::formBuilder() with \Drupal::service('form_builder')

It's to replace \Drupal::formBuilder() and \Drupal::service('form_builder') 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
new35.64 KB
new18.18 KB
nitesh624’s picture

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

StatusFileSize
new1.89 KB

@nitesh624 , thankyou for working on this. Kindly see comment mentioned at #10 It's to replace \Drupal::formBuilder() and \Drupal::service('form_builder') with IoC injection where possible.

Kindly review a new patch.

msuthars’s picture

Assigned: Unassigned » msuthars
msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs review » Needs work

@Hardik_Patel_12 I reviewed the patch and found some changes:
1. You forgot to use that injected service in UserLoginBlock, you only inject the service not replace it with \Drupal::formBuilder().
2. ActionListBuilder is pending for Ioc service injection.

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new2.35 KB
new658 bytes

Please review!

hardik_patel_12’s picture

StatusFileSize
new2.66 KB
new1.12 KB

Adding Change Record and deprecation error for UserLoginBlock file. Kindly review a new patch.

Checking remaining replacement files.

grep -nri '\Drupal::formBuilder()' core | grep -v .module | grep -vi test | grep -v .api.php | grep -v authorize.php | grep -v .inc
core/lib/Drupal/Core/Form/FormCache.php:180:    // \Drupal::formBuilder()->getCache() and \Drupal::formBuilder()->setCache()
core/lib/Drupal/Core/Form/FormBuilderInterface.php:149:   * \Drupal::formBuilder()->submitForm('user_register_form', $form_state);
core/lib/Drupal/Core/Form/FormState.php:50:   *     automatically loaded by \Drupal::formBuilder()->getCache(). By default
core/lib/Drupal/Core/Form/FormState.php:1202:    // \Drupal::formBuilder()->doBuildForm() collects all button elements in a
core/lib/Drupal/Core/Config/Entity/DraggableListBuilder.php:174:      $this->formBuilder = \Drupal::formBuilder();
core/lib/Drupal/Core/Render/Element/RenderElement.php:138:    // \Drupal::formBuilder()->doBuildForm().

All calls are commented except below one.

core/lib/Drupal/Core/Config/Entity/DraggableListBuilder.php:174:      $this->formBuilder = \Drupal::formBuilder();

Not sure why it was done this way and can we replace this with injected service or not.

@msuthars, for ActionListBuilder i am not sure about this we can inject service in it or not.

jungle’s picture

Title: Replace usages of \Drupal::formBuilder() with IoC injection » Replace non-test usages of \Drupal::formBuilder() with IoC injection

We do this for non-test code under this issue.

Per the parent issue, rescoping this to do it for non-test code.

Reviewing

jungle’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/user/src/Plugin/Block/UserLoginBlock.php
    @@ -47,11 +55,18 @@ class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterfac
    +      @trigger_error('Calling ' . __METHOD__ . ' without the $form_builder argument is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3136769', E_USER_DEPRECATED);
    

    CR link should be changed to https://www.drupal.org/node/3159776

  2. And the error message, would be better adding __CLASS__, for example:

    'Calling ' . __CLASS__ . '::' . __METHOD__ . '() without the ...

  3. $ git grep -nri '\Drupal::formBuilder()' core | grep -v .module | grep -iv test.php | grep -iv .inc | grep -iv .api.php | grep -v authorize.php | grep -iv testbase.php | grep -v '//' | grep -v '*'
    core/lib/Drupal/Core/Config/Entity/DraggableListBuilder.php:174:      $this->formBuilder = \Drupal::formBuilder();
    

    grep -v '//' | grep -v '*' -- ignore occurrences in comments. For the one in \Drupal\Core\Config\Entity\DraggableListBuilder::formBuilder()

      protected function formBuilder() {
        if (!$this->formBuilder) {
          $this->formBuilder = \Drupal::formBuilder();
        }
        return $this->formBuilder;
      }

    I think it can be ignored.

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new2.71 KB
new1.2 KB

Kindly review a new patch , it is covering points as suggested at #20 and changed the use statements to be alphabetical.

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.

smustgrave’s picture

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

This issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.

@trigger_error('Calling ' . __CLASS__ . '::' . __METHOD__ . '() without the $form_builder argument is deprecated in drupal:9.1.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3159776', E_USER_DEPRECATED);

Unfortunately this missed the D10 boat so this will have to be updated for deprecated in 10.1.x and required in 11.

_utsavsharma’s picture

Status: Needs work » Needs review
StatusFileSize
new1022 bytes
new2.7 KB

Addressed the point in #27.
Please review.

_utsavsharma’s picture

StatusFileSize
new1023 bytes
new2.71 KB

Updated for deprecated in 10.1.x and required in 11 to 10.1.0 and 11.0.0.
Please review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for updating that

After applying the patch and doing a search for \Drupal::formBuilder() in my phpstorm. Everything I found was in a module, inc, comment, test.

Only one I have a question on is ActionListBuilder but will let the committer decide if that needs to be replaced.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Had a look at ActionListBuilder and to me that looks in scope - class already has a constructor and ::createInstance() method, so let's do that one here. Patch looks fine for the instance it changes.

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.

joachim’s picture

DraggableListBuilder should also be replaced.

andypost’s picture

Related issues: +#2989889: Extract DraggableListBuilderTrait from DraggableListBuilder for use with content entities

DraggableListBuilder should also be replaced.

it has changes as trait extracted already via #2989889: Extract DraggableListBuilderTrait from DraggableListBuilder for use with content entities

btw Action module is going to be deprecated, see #3343369: [meta] Tasks to deprecate Actions UI module

andypost’s picture

Issue summary: View changes
Status: Needs work » Needs review

Rebased and updated deprecation for 10.3

The \Drupal\Core\Config\Entity\DraggableListBuilder::__construct() has following comment so should stay as is

    // Do not inject the form builder for backwards-compatibility.
    $this->formBuilder = \Drupal::formBuilder();

Also updated IS

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record updates

Can the CR be updated please.

andypost’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record updates
joachim’s picture

Does this need a CR? Entity handlers and plugin classes are internal.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

CR reads well.

  • catch committed 6d135570 on 10.3.x
    Issue #3123209 by Hardik_Patel_12, nitesh624, _utsavsharma, Suresh...

  • catch committed 9357f733 on 11.x
    Issue #3123209 by Hardik_Patel_12, nitesh624, _utsavsharma, Suresh...
catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Does this need a CR? Entity handlers and plugin classes are internal.

IMO we should not add CRs for internal code, especially not constructor changes, and just link to the issue itself - however the extra work's already been done here and the docs for when to add a CR aren't conclusive either way, so committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

quietone’s picture

The change record here should include a before and after example to help the reader. And I think it needs rewording to be easier to read.

andypost’s picture

Re #43 sometimes we add like https://www.drupal.org/node/3395991

@quietone I think CR can add, that classes extending this ones should be updated to pass form_builder service to parent

Status: Fixed » Closed (fixed)

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

quietone’s picture