Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Is it possible to change plugin config programmatically?

Solved by M View solution

Started by M 6 years ago · 8 replies · 833 views
6 years ago

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?

6 years ago

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 ;)

6 years ago

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

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

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

    $config = (array) $this->config->get('plugins.events');
    dump($config);
    // 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:

TXT
icalendar_update: '1'

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

6 years ago

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

last edited 02/23/20 by Ron Wardenier
6 years ago

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

6 years ago

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

last edited 02/23/20 by M
4 years ago

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'.

PHP
public function onAdminAfterSave(Event $event)
{
    $this->grav['config']->set('plugins.events.icalendar_update', '1');
    $this->saveConfig('events');
}

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
4 years ago Solution

Finally I found the solution: I must use the $event argument to get the new value. E.g. $event['object']["icalendar_update"].

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 44 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago