2

I have a config.ini.php file as follows :

;<?php
;die(); // For further security
; // These php lines are used to hide the data from the browser
; // They are both valid php and valid ini files
;/*
; Don't forget to disable and reenable module, after adjusting the values
[folders]
var_1= "value1"
var_2 = "value2"

And in module_name.install file, I am using it as follows.

/**
 * Implementation of hook_enable().
 */
function module_name_enable() {
  $config_file = 'config.ini.php';
  $parameters = parse_ini_file($config_file);
  foreach ($parameters as $key => $value) {
    \Drupal::state()->set($key, $value);
    \Drupal::config('module_name.settings')
      ->set($key, $value)
      ->save();
  }
}

I then try to use a variable with the following code.

$getvar = \Drupal::config('module_name.settings')
      ->get('var_1'); 
\Drupal::state()->get('var_1', '');

I am not getting the value. What am I doing wrong?

2 Answers 2

4

As far as I know, you shouldn't be using a "config.ini.php" file for configs in your custom module.

You should have a /examplemodule/config/install/examplemodule.settings.yml file like this:

var_1: "value1"
var_2: "value2"

Then when you reinstall your module, it adds the values. There's no need to run code in hook_install.

To access the values, use:

\Drupal::config('examplemodule.settings')->get('var_1');

and to edit:

\Drupal::configFactory()
  ->getEditable('examplemodule.settings')
  ->set('var_1', $value)
  ->save();

You're using state correctly, I tested this out:

\Drupal::state()->set('var_1', $value);

$var_1 = \Drupal::state()->get('var_1');
2
  • thanks for your insight , i was wrongly given hook_enable instead of hook_install and done by the usual way of setting in install file \Drupal::state()->set('var_1', $value); and used in module file as $var_1 = \Drupal::state()->get('var_1');
    – KTM
    Commented Jun 7, 2016 at 15:05
  • Your solutions works too ,and I think its the drupal 8 way of doing it :)
    – KTM
    Commented Jun 7, 2016 at 15:33
3

hook_enable doesn't exist any more. Your code should be in hook_install

Other than that, this will set a value:

\Drupal::configFactory()
  ->getEditable('module_name.settings')
  ->set('var_1', $value)
  ->save();

And this will retrieve it:

\Drupal::config('module_name.settings')
  ->get('var_1'); 

Just tried it, confirmed to be working. If you're still not seeing the value, there's something wrong with your non-Drupal code (the ini file parsing), or the context in which you're trying to retrieve it.

4
  • btw i have no settings file in the module , and it through me error like : Drupal\Core\Config\ImmutableConfigException: Can not set values on immutable configuration module_name.settings:var_1. Use \Drupal\Core\Config\ConfigFactoryInterface::getEditable() to retrieve a mutable configuration object in Drupal\Core\Config\ImmutableConfig->set() (line 32 of core/lib/Drupal/Core/Config/ImmutableConfig.php).
    – KTM
    Commented Jun 7, 2016 at 12:06
  • Please ask a new question if you have a new problem...
    – Clive
    Commented Jun 7, 2016 at 12:17
  • Actually don't, that's the same problem isn't it! I was trying this against an old version of D8, give me a minute and I'll update with the proper code
    – Clive
    Commented Jun 7, 2016 at 12:20
  • Could you please explain how to get/set array variables in the same file? Commented May 17, 2018 at 10:29

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.