1

Is it possible to edit/create a config variable of a module in a controller created in the same module?

For example: module_name/src/Controller/MyController.php:

class MyController extends ControllerBase {

    public function x($y) {
        $config = \Drupal::config('module_name.settings');
        $config->set('var1', $y)->save();
    }
}

Update:

module_name.module:

function module_name_page_attachments(array &$page) {
  $config = \Drupal::config('module_name.settings');
  var_dump($config->get('var1'));
}

module_name/config/install/module_name.settings.yml:

var1: 0

module_name/src/Form/moduleNameSettings.php:

class moduleNameSettings extends ConfigFormBase {

    protected function getEditableConfigNames() {
        return [
            'module_name.settings',
        ];
    }


    public function submitForm(array &$form, FormStateInterface $form_state) {
        parent::submitForm($form, $form_state);
        $config = \Drupal::config('module_name.settings');

        $this->config('module_name.settings')
            ->set('var1', $config->get('var1'))
            ->save();
    }
  • I'd say: Sure! Have you tried it already? – leymannx Jul 26 '18 at 11:50
  • yes I did but it doesn't work I still can't get the new value of "var1" in module_name.module file! (Note: calling the controller is fine no problem with controller invoke) – MiharbKH Jul 26 '18 at 11:52
  • 2
    In the submitForm method you seem to be setting the variable to its existing value rather than updating it. – Alfred Armstrong Jul 26 '18 at 13:12
  • I think that's the way to update it! what should I do instead? – MiharbKH Jul 26 '18 at 13:39
  • See ExampleSettingsForm::submitForm() from Working with Configuration Forms. – Andy Jul 26 '18 at 16:43
2

With the ConfigFormBase, you need to implement the editableConfigNames() method. E.g.

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'example.settings',
    ];
  }

In general, you need to get the editable version of the config file to change it. ConfigFormBase (well actually, ConfigFormBaseTraits) will only return the editable config is you implement the above method.

For reference, here's how you could do this without the ConfForm logic.

$config = \Drupal::service('config.factory')->getEditable('example.settings');
$config->set('message', 'Hi')->save();

See the documents for details.

Since you are expanding ConfigFormBase, you should be able to use:

$config = $this->configFactory()->getEditable('example.settings');

To get the editable version.

  • IIUC they're already relying on the base class and using ConfigFormBaseTrait::config() (which does return the editable settings, assuming getEditableConfigNames() is set up correctly, which it seems to be). – Andy Jul 26 '18 at 18:09
  • D'oh.. look at my own code first.. editing answer. – CG Monroe Jul 26 '18 at 18:11
  • getEditableConfigNames() has already implemented I just didn't mention it in the question trying to focus on submitForm() method.., and it works for me I use $config = \Drupal::service('config.factory')->getEditable('example.settings'); $config->set('message', 'Hi')->save(); in the controller body. thank's a lot! :) – MiharbKH Jul 26 '18 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.