Skip to content

Instantly share code, notes, and snippets.

@davidjguru
Forked from JeffTomlinson/MyService.php
Created October 26, 2019 19:29
Show Gist options
  • Save davidjguru/b7b77f90d558087f8cdf4ea681eae964 to your computer and use it in GitHub Desktop.
Save davidjguru/b7b77f90d558087f8cdf4ea681eae964 to your computer and use it in GitHub Desktop.
Drupal 8 Configuration Dependency Injection
<?php
/**
* Get my setting.
*/
function get_my_setting() {
return /Drupal::service('my_module.my_service')->getMySetting();
}
services:
my_module.my_service:
class: Drupal\my_module\Services\MyService
arguments:
- '@config.factory'
# /my_module/config/install/
my_setting: "Foo"
<?php
namespace Drupal\my_module\Services;
use Drupal\Core\Config\ConfigFactory;
/**
* Class MyService.
*
* @package Drupal\my_module\Services
*/
class MyService {
/**
* Configuration Factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Constructor.
*/
public function __construct(ConfigFactory $configFactory) {
$this->configFactory = $configFactory;
}
/**
* Gets my setting.
*/
public function getMySetting() {
$config = $this->configFactory->get('my_module.settings');
return $config->get('my_setting');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment