Run function from plugin admin only once, when saved

Hi. I’m new to Grav and making my first plugin. I’m trying create a ‘run once’ type of function from it’s admin page to pull data from another websevice into a yaml. I can’t figure out how to make my function run only when a button or toggle is selected, and then the admin page saved. My attempts:

  1. I can’t figure out if I can add a custom submit button on the plugin’s admin page and catch that– I can’t see other plugins do this, so I guess not.

  2. Using onAdminSave(Event $event){… I can get the value of a toggle (set in blueprints.yaml) and run my update event. But I can’t figure out how to modify the toggle’s event value… ie before its saved - ie put it back to the default value. Is this possible?

  3. Using onAdminAfterSave(Event $event){… I can’t figure out how to modify the saved toggle value. I’ve tried $this->grav['config']->set('plugins.shortcode-mbg.do_updated', '0); with no success

Are these approaches workable or is there a better approach?

thanks!

@mikeb77, There are two approaches that come to mind:

Dirty:

  • Load js (see Asset Manager) when Admin runs and route is ‘admin/plugins/myplugin’
  • Js adds a Button to the DOM of the plugin page
  • Button submits async call to server
  • Plugin:
    • catches async call,
    • calls webservice,
    • executes some logic to save the data
    • sends response back to front end

Cleaner (see plugin Comments):

  • Add extra menu item in sitebar of Admin
  • Add custom page in Admin (called by menu)
    • Define page and template to show page
    • Template can contain anything you like
  • Button on page calls server asynchronously
  • Plugin:
    • See above…

thanks pamtbaau. i see how that may work.

more simply, given i can write to the config file, i wonder if there is some way to delete the current cache so the config is reloaded when the amdin page is reloaded? if i manually delete the cache/compiled config/master-myplugin.php then the admin panel will reload with the updated values. but if I unlnk() that same file (at the end of onAdminAfterSave) i still get the admin saved values rather than my updated config values.

also, on the admin plugin if i turn off Enable Admin Caching the cache is still set in my plguin. ie I don’t know how to disable caching for my plugin’s admnin page

thanks

@mikeb77, Not sure if I understand your point about cache and reloading the config…

In the two options I suggested, there is no writing to the config file, because the plugin config page is not being saved. Hence there is also no onAdminAfterSave event being raised.

Dirty:
When adding a Button to the DOM of the plugin config page, that button will submit an async call to the server. The server will call the webservice and process the data. There is no saving of the config file.

Cleaner:
The plugin config file in Admin is not being used. A separate page is being used. A button on that page submits an async call… etc. see above.

hi pamtbaau
i was contnuing with my idea of being able to ‘reset’ a toggle after saving. ie not having to create a button/ajax solution, ‘clean’ or ‘dirty’. stragely i’ve now got my idea working, simply:

public function onAdminAfterSave(Event $event) { 
    $plugin_name = "myplug";
    $refresh_from_zotero = $event['object']->refresh_from_zotero ?? 0;

    if($refresh_from_zotero){
      $this->config->set('plugins.myplug.zotero_last_updated', date('d-m-Y G:i:s'));//. ' ' . $refresh_from_zotero); 
      $this->config->set('plugins.myplug.refresh_from_zotero', "0"); 
      $this->saveConfig($plugin_name);// use plugin name
     // do the fancy function here....
    }

and blueprints.yaml

form:
  validation: strict
  fields:
    enabled:
      type: toggle
      label: Plugin status
      highlight: 1
      default: 0
      options:
        1: Enabled
        0: Disabled
      validate:
        type: bool
    
   # ... other...
  
    refresh_from_zotero:
      type: toggle
      label: Refresh references from zotero DB
      help: refresh all zotero reference using the user account details above. Enable this and click 'save' above
      highlight: 1
      default: 0
      options:
        0: dont refresh
        1: refresh data when 'save'
      validate:
        type: bool

    zotero_last_updated:
      type: text
      label: Last date Zotero db downloaded
      disabled: disabled

the toggle resets after saves and the disabled field shows the last date/time run.

i spent ages on this and couldn’t figuring how to stop just reloading the old cached values. ie turning off site caching, admin pugin caching and trying cache_enable: false in blueprints and even unlnking files did nothing. then i deleted the old cache (on dashboard) and it simply worked. I have seen posts from other newbies struggling with this scenario so hopfully this also works for others.

as a newbie, there i something i still don’t understand about caching in grav that makes getting started complicated (yes i read the docs). and dump() not working in onAdminSave is very confusing. but slowly…

thanks