Replace \Drupal calls with proper IoC in form elements

Created on 21 February 2024, 7 months ago
Updated 23 February 2024, 7 months ago

Problem/Motivation

Child classes of \Drupal\Core\Render\Element\FormElement often use \Drupal calls to obtain services like the Access Manager (access_manager) or the Entity Type Manager (entity_type.manager). These calls should be rewritten to use proper dependency injection.

Proposed resolution

  1. Locate all sub-classes of \Drupal\Core\Render\Element\FormElement in core.
  2. Identify usages of \Drupal in those classes.
  3. Rewrite the form elements to inject their dependencies.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Needs work

Version

11.0 🔥

Component
Form 

Last updated 1 minute ago

Created by

🇺🇸United States GuyPaddock

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

Merge Requests

Comments & Activities

  • Issue created by @GuyPaddock
  • First commit to issue fork.
  • Assigned to himanshu_jhaloya
  • Issue was unassigned.
  • Status changed to Needs review 7 months ago
  • Pipeline finished with Failed
    7 months ago
    Total: 1041s
    #102069
  • Status changed to Needs work 7 months ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    7 months ago
    Total: 1450s
    #102106
  • 🇺🇸United States TR Cascadia

    Well, from the test results, you can see why the AccessManager isn't currently injected. That is because it is used inside of public static function processAutocomplete(). You can't access instance variables using $this inside a static function, because there is no instance of the class.

    Additionally, there are a lot of WRONG coding standards changes made in the patch. For example:

    -   * #element_validate callback for #pattern form element property.
    +   * Element_validate callback for #pattern form element property.

    This change is wrong because, like the original comment says, the method is the callback used in #element_validate. While it could be worded differently so that #element_validate doesn't appear at the beginning of the comment, changing that to Element_validate destroys the meaning of the comment. The purpose of coding standards changes should be to improve the content, and they should NOT be done just to make a warning go away.

    Regardless, coding standards fixes are out-of-scope for this issue and shouldn't be mixed in with unrelated changes.

    I think this should be a "Colsed (works as designed)" because the static \Drupal call is the correct way to access a service inside a static function.

Production build 0.71.5 2024