Problem/Motivation
The core/modules/taxonomy/src/Hook/TaxonomyHooks.php class currently accesses the database connection using the global service locator:
\Drupal::database();
This approach is discouraged in modern Drupal code, especially within classes.
Since TaxonomyHooks is already a class based, it should use DI for including the database connection.
Steps to reproduce
Proposed resolution
Refactor TaxonomyHooks to use dependency injection for the database connection.
- Inject
databaseservice (Drupal\Core\Database\Connection) via the constructor - Store it as a class property
- Replace all usages of
\Drupal::database()with$this->database - Update the service definition in
taxonomy.services.ymlto include the new argument
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Issue fork drupal-3581563
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
Comment #2
sivaji_ganesh_jojodae commentedComment #3
sourav_paulWorking on it.
Comment #5
sourav_paulRefactor whole class with DI for services.
Comment #6
longwaveGiven this needs to happen for many *Hooks classes, is there a meta issue where the scope for this was discussed first?
Comment #7
nicxvan commentedThere is, and this specific issue is a duplicate.
Meta #3493453: [meta] Standardize and clean up hook classes in core
Taxonomy issue #3502014: Clean up hook implementations in the Taxonomy module
I think this should be closed as a duplicate and the other issue should be redone, there was a looot of discussion around how to break it up that happened and those users should get credit for it.
Comment #8
sivaji_ganesh_jojodae commentedBased on #7, changing the status to Closed.