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 database service (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.yml to include the new argument

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork drupal-3581563

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

sivaji_ganesh_jojodae’s picture

Issue summary: View changes
sourav_paul’s picture

Working on it.

sourav_paul’s picture

Status: Active » Needs review

Refactor whole class with DI for services.

longwave’s picture

Given this needs to happen for many *Hooks classes, is there a meta issue where the scope for this was discussed first?

nicxvan’s picture

There 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.

sivaji_ganesh_jojodae’s picture

Status: Needs review » Closed (duplicate)

Based on #7, changing the status to Closed.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.