-
-
Save oksana-c/3a9cee7b9f405317f4c5efc45c10d1f6 to your computer and use it in GitHub Desktop.
Drupal 8 Configuration Dependency Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get my setting. | |
*/ | |
function get_my_setting() { | |
return /Drupal::service('my_module.my_service')->getMySetting(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
my_module.my_service: | |
class: Drupal\my_module\Services\MyService | |
arguments: | |
- '@config.factory' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /my_module/config/install/ | |
my_setting: "Foo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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