Is it possible to change plugin config programmatically?

I’d like to implement a checkbox that the site admin can set in the plugin settings. And when he decides to store the settings I’d like to evaluate this checkbox and reset it afterwards. Is that possible?

Of course. The settings is in a YAML file and YAML files can be changed from a plugin. Just start with the Plugin Tutorial and hack your way through :wink:

Thanks for your reply. I already went through the tutorial and I tried it with the following code:

public function onAdminSave(Event $event)
    $config = (array) $this->config->get('');
    // shows the non-modified config: "icalendar_update" => "1" as expected

    $this->config->set('', '0');
    // modify and store a setting: 'saveConfig' returns 'true' as expected

    $config = (array) $this->config->get('');
    // shows the modified config: "icalendar_update" => "0" as expected

The above code seems to work as expected, but the line in the according file in user/config is still:

icalendar_update: '1'

So storing the setting doesn’t seem to work. What am I doing wrong?

Just a guess: is the code modifying the user/plugins/events/events.yaml file instead?

No, this file is also not being modified. I have no idea what ‘saveConfig’ does.

It persists the changed configuration to disk. Config also has a setter, so you can change it on run-time without changing files:

$this->grav['config']->set('config.key', 'value');

I do want to change the config file. But it doesn’t work. It always remains unchanged.

After a long time I took up the work on this feature again.

In general I can change and save a setting in the ‘onAdminAfterSave’.

public function onAdminAfterSave(Event $event)
	$this->grav['config']->set('', '1');

always sets the flag to ‘1’.

But I’m not able to read the current state. I always receive the state from the last call as if the value wasn’t written, yet.

  1. check box in the plugin admin interface is unchecked and was saved before
  2. leave the check box unchecked and click on ‘save’
  3. the value in ‘onAdminAfterSave’ is null or 0 → as expected
  4. check the check box in the plugin admin interface and click save again
  5. the value in ‘onAdminAfterSave’ is null or 0 → unexpected
  6. the check box in the plugin admin interface is checked now, although it was read as null or 0 in step 5
  7. leave the check box checked and click on ‘save’
  8. the value in ‘onAdminAfterSave’ is 1 → as expected, but one cycle late

Finally I found the solution: I must use the $event argument to get the new value. E.g. $event[‘object’][“icalendar_update”].