When I start writing a class that has a dependency injection, I have a clear idea about which services it needs. I generate it -- the plugin, form, controller, or service -- and specify those services.
Then nearly always, unless it's something really very simple, I find that no matter how much I thought about it and planned it, I need to add more services. Maybe remove some too.
Fortunately, because Module Builder saves the configuration of the module code you've generated, it's easy to go back to it and edit it to add more services:
- Edit your module in Module Builder
- Add to the injected services for your component
- Ensure your code file is committed to version control
- Generate the code, and write the updated version of the code file
- Add and commit the new DI code, while discarding the changes that remove your code. (I find it helps to use a git GUI for things like this, though
git add -p
works too.)
But I tend to find that I make this mistake several times as the class develops, and so I adopt the approach of using the \Drupal::service()
function to get my services, and only when I'm fairly confident I'm not going to need to make any more changes to DI, I update the injected services in one go, converting all the service calls to use the service properties.
I was asked yesterday at Drupal Drinks about how to do that, and it occurred to me that there's a way of doing this so after you've updated the dependency injection with Module Builder, it's a simple find and replace to update your code.
If you write your code like this whenever you need a service:
$service_entityTypeManager = \Drupal::service('entity_type.manager');
$stuff = $service_entityTypeManager->doSomething();
Then you need to do only two find and replace operations to convert this to DI:
- Replace
'^.+Drupal::service.+\n'
with''
. This removes all the lines where you get the service from the Drupal class. - Replace
'\$service_(\w+)'
with'$this->$1'
. This replaces all the service variables with the class property.
Up until now I'd been calling the service variables something like $entityTypeManager
so that I could easily change that to $this->entityTypeManager
manually, but prefixing the variable name with a camel case 'service_'
gives you something to find with a regular expression.
If you want to be really fancy, you can use a regular expression like '(?<=::service..)[\w.]+'
(using dots to avoid having to escape the open bracket and the quote mark) to find all the services that you need to add to the class's dependency injection.
Something like this:
$ ag -G MyClass.php '(?<=::service..)[\w.]+' -o --nonumbers --nofilename | sort | uniq | tr "\n" ", "
will give you a list of service names that you can copy-paste into the Module Builder form. This is probably overkill for something you can do pretty quickly with the search in a text editor or IDE, but it's a nice illustration of the power of unix tools: ag
has options to output just the found text, then sort
and uniq
eliminate duplicates, and finally tr
turns it into a comma-separated list.