I have a module configuration.
modulename.config.yml
modulename: core_address: 107.101.xx.yy core_port: 153759 core_user: superuser core_pass: superpass
I have a service.
modulename.service.yml
services: modulename.my_service: class: DrupalmodulenamegroupMyService
MyService.php
/** * @file * Contains DrupalmodulenamegroupMyService. */ namespace Drupalmodulenamegroup; /** * MyService. */ class MyService{ public function __construct() { } public function getDemoValue() { $config = Drupal::config('modulename.my_service'); $core_port = $config->get('modulename.core_port'); return $core_port . 'asdf'; } }
$core_port
is always empty.
How can I access a module configuration from a service it implements?
I am not sure this is relevant, but getDemoValue()
is called from different a module.
/** * @file * Contains DrupalanothermodulenameControllerMyForm. */ namespace DrupalanothermodulenameController; use DrupalCoreFormFormBase; use DrupalCoreFormFormStateInterface; use DrupalComponentUtilityUrlHelper; use SymfonyComponentDependencyInjectionContainerInterface; /** * MyForm. */ class MyForm extends FormBase { protected $demoService; /** * Class constructor. */ public function __construct($demoService) { $this->demoService = $demoService; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('modulename.my_service') ); } /** * {@inheritdoc} */ public function getFormId() { /** nothing interesting here **/ } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { /** nothing interesting here **/ return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state){ /** nothing interesting here **/ } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Display result. foreach ($form_state->getValues() as $key => $value) { drupal_set_message($key . ': ' . $value . ' ' . $this->demoService->getDemoValue()); // i try to print here but didn't happend } } }
Sponsored by SupremePR