Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a difference between doing ?

\Drupal::service('config.factory')

and

 \Drupal::configFactory()

Is it better to use the second one ?

Can \Drupal be used to do smthg else than call services ?

the drupal doc 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 case there would not be any dedicated method ? and how to easy list then all the retrievable services?

https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/class/Drupal/8.2.x

https://www.drupal.org/docs/8/api/services-and-dependency-injection/services-and-dependency-injection-in-drupal-8

share|improve this question

\Drupal::service('config.factory')

and

\Drupal::configFactory()

Is it better to use the second one ?

They are equivalent, the second one just has more brevity.

in which case there would not be any dedicated method ?

Lots. 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).

Here's a slideshow I used when doing a presentation on dependency injection at Drupal Camp Tokyo this month: http://www.slideshare.net/JayFriendly/services-and-dependency-injection-in-drupal-8

share|improve this answer
    
thanks. what would be the dependy injection alternative to , say, $my_config = \Drupal::service('config.factory')->getEditable('my_config')‌​; ? – Matoeil 1 hour ago
    
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 1 hour ago
    
If you use drupal console you can debug services with command drupal container:debug – Jonh 1 hour ago
    
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 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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