4

Is there a difference between calling \Drupal::service('config.factory') and \Drupal::configFactory()?

Is it better to use the second one?

Can \Drupal be used to do something else than calling services?

The documentation says:

Accessing services in global functions

The global Drupal class provides static methods to access several of the most common services. For example, Drupal::moduleHandler() will return the module handler service or Drupal::translation() will return the string translation service. If there is no dedicated method for the service you want to use, you can use the Drupal::service() method to retrieve any defined service.

In which cases isn't there any dedicated method? How do I easily list all the retrievable services?

1

\Drupal::configFactory() is just shorter than \Drupal::service('config.factory'). The fact that with \Drupal::configFactory() you aren't hard-coding the service name in your module is not probably a reason to choose it, since you would still hard-code most of the service names (the dedicated methods don't even cover a quarter of all the available services), and the chances that a service name is changed are probably low now that Drupal 8 has a stable release.
When there is a dedicated method for a service I am using in my module, I much prefer using that dedicated method, and I prefer using dependency injection over using the static methods \Drupal has.

The purpose of the \Drupal class is giving access to the services provided from Drupal or third-party modules; even \Drupal::request() is calling a service's method. So, no, you don't use \Drupal for things that aren't services; even in this case, you should not depend from that class, if it is possible to use dependency injection.

For a list of the services implemented by modules, you need to look at the .services.yml files provided with the modules, including the core modules. Those files give you a list of services implemented by modules; it's impossible to give an exhaustive list since every module can implement all the services it needs.

  • thanks ,what would be the dependy injection alternative to , say, $my_config = \Drupal::service('config.factory')->getEditable('my_config')‌​ in a submitForm method context? – Matoeil Feb 2 '17 at 16:20
  • It depends in which class you would use that code. There isn't an answer that is valid for all the cases. – kiamlaluno Feb 3 '17 at 12:33
6

\Drupal::service('config.factory') and \Drupal::configFactory() are equivalent; the second is just shorter.

There are many, many services provided by core, and only a few of them have a dedicated method. You can get a list of core services on the core.services.yml page. Note that this is not all services that core offers, for example, there are more services in user.services.yml.

Also note that when working with classes, it's better to use dependency injection rather than calling static methods. Static methods should be called in procedural code (in D8, this basically means hooks, or API functions).

This is a slideshow I used when doing a presentation on dependency injection at Drupal Camp Tokyo this month.

  • thanks. what would be the dependy injection alternative to , say, $my_config = \Drupal::service('config.factory')->getEditable('my_config'); ? – Matoeil Feb 2 '17 at 10:53
  • would drupal console container:debug list all services present in an application code (core + contrib modules +custom modules ) or only the ones well defined in the core ? – Matoeil Feb 2 '17 at 10:56
  • 1
    If you use drupal console you can debug services with command drupal container:debug – Jonh Feb 2 '17 at 11:01
  • 1
    The dependency injection inserts the service into the class in the __construct() method. Not really something that can be shown in these comments though. See the slideshow I linked to for some examples. as for the Drupal console, I don't know. Edit - though Jonh just gave a method. – Jaypan Feb 2 '17 at 11:01
  • 1
    The shortcut methods on Drupal are typehinted, so when they are provided and you have an IDE, you get the advantage of autocompletion, etc. – mpdonadio Feb 2 '17 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.