Skip to content

Instantly share code, notes, and snippets.

@oksana-c
Forked from JeffTomlinson/MyService.php
Created October 29, 2018 22:47
Show Gist options
  • Save oksana-c/3a9cee7b9f405317f4c5efc45c10d1f6 to your computer and use it in GitHub Desktop.
Save oksana-c/3a9cee7b9f405317f4c5efc45c10d1f6 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