In a custom Service class, I need to check a config setting.

I'm able to do this using \Drupal::config(static::SETTINGS) as documented here: https://www.drupal.org/docs/drupal-apis/configuration-api/working-with-configuration-forms

but Coder complains:

WARNING | \Drupal calls should be avoided in classes, use dependency injection instead

What service should I inject here?

I only need to see the immutable config value, I don't think I need a ConfigFactory.

More info:

When I try to inject the ConfigFactoryInterface, using the pattern I have followed for many other services, I get this error:

Error: Call to undefined method Drupal\my_custom_module\Api\DataPartner::config() in Drupal\my_custom_module\Api\DataPartner->hasDebugPermission()

The code in the hasDebugPermission() method looks like this:

   * Private function to control whether displaying debug info is permitted.
   * @return bool
   *   TRUE if debugging is permitted for current user in current environment.
  private function hasDebugPermission() {
    $config = $this->config(static::SETTINGS);
      && $config->get('display_debugging_info')
      && $this->account->hasPermission('debug forms');
    return $result;

And I have declared static::SETTINGS like this:

   * Config settings.
   * @var string
  private const SETTINGS = 'my_custom_module.settings';

The call to $this->account->hasPermission() works fine after injecting \Drupal\Core\Session\AccountInterface, but the call to $this->config('my_custom_module.settings')->get('display_debugging_info') does not work after injecting \Drupal\Core\Config\ConfigFactoryInterface.

1 Answer 1


Make sure you inject config.factory and then you need to use:


You can see that Drupal::config() is:

public static function config($name) {
  return static::getContainer()

So after injecting your service, the equivalent of \Drupal::config('my_custom_module.settings') is:

  • Perfect answer! You even ended on exactly the line where I forgot the ->get after $this->config. Thank you. Aug 23, 2020 at 23:01
  • By the way, the bug in my code came straight from Drupal community documentation: drupal.org/docs/drupal-apis/configuration-api/… Aug 24, 2020 at 2:59
  • 2
    Also note that the ConfigFormBase class uses the ConfigFormBaseTrait which defines a method config() which is not the same as simply naming your injected config factory service $config. You can see that method is a shortcut to the above, it's similar to calling \Drupal::config().
    – sonfd
    Aug 24, 2020 at 13:10
  • 1
    @leymannx I fixed the line in the docs: drupal.org/node/2206607/revisions/view/11783167/12036988 Aug 24, 2020 at 15:54
  • 2
    The docs were actually correct to begin with. As I tried to explain in the comment, ConfigFormBase has a config() method.
    – sonfd
    Aug 24, 2020 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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