Hello there
I am trying to write a selfmade for the first time. And I get very confused and after hours it still seems I won’t get. So that’s why I am asking here.
Goal: I want a Plugin, which calls a script somewhere in the container. The Script should always be called when blueprint values are changed (configuration of the plugin). And that’s it… As simple as that. Would also prefer an individual button to “synchronize/call the script”…
Where am I: So I have created a basic plugin via devtools plugin. I have my basic fields, where data can be changed/altered. But now I need those values to execute the script after the new values are saved. In the file s3-content-manager.php I have the following lines (needed for plugin):
public function onBlueprintCreated(Event $event)
{
// Values
$strategie = $this->grav['config']->get('plugins.s3-content-manager.type');
$archiv = $this->grav['config']->get('plugins.s3-content-manager.source');
// Fire Script
**shell_exec('/var/www/scripts/s3-auto-update.sh "' . $strategie . '" "' . $archiv . '"');**
}
The script never gets triggered. As I understand I am using an wrong event. Does somebody know, what the suitable event for this goal would be? Am I doing something completely wrong?
I think I don’t understand the functionality of plugins yet.
Would be happy if someone could help me or give me some hints…
@oliverbaehler
I too am getting grips with GRAV. Here’s some things to check about the events:
- Look at the GRAV lifecycle https://learn.getgrav.org/plugins/grav-lifecycle
- Find a suitable event to hook into. (There isn’t an Event like the one you named)
- Make sure that you enable / subscribe to the event. If you look at the new plugin skeleton, you will find ‘onPluginsInitialized’ is in
getSubscribedEvents
, and then another Event is referenced in the $this->enable parameters. The name of your function in the plugin does not matter, you can call it as above, but it will not be triggered unless you register / enable it.
Other plugins, such as Form and Login, fire Events, which can then be picked by your plugin.
Thanks @finanalyst for your reply.
Now I am starting to understand the structur. I really didn’t se that before. THank you very much.
So the final question would be, were do I find then suitable events? I always looked up this page: https://learn.getgrav.org/plugins/event-hooks
The most suitable I have found would be ‘onFormProcessed’.
Now I am having this:
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onFormProcessed' => ['s3sync', 0]
];
}
// Trigger script
public function s3sync($event)
{
// Values
$strategie = $this->grav['config']->get('plugins.s3-content-manager.type');
$archiv = $this->grav['config']->get('plugins.s3-content-manager.source');
// Fire Script
$sync = shell_exec('/bin/bash /var/www/scripts/s3-auto-update.sh "' . $strategie . '" "' . $archiv . '"');
var_dump($sync);
}
But there is still nothing happening after I saved the values. Might just still be the wrong event…
1 Like
@oliverbaehler
a) OnFormProcessed is fired by the Form plugin, not by Grav core. So you have to have a form that is submitted. I do not see you submitting a form.
b) Look at my published plugin ‘persistent-data’. (on the Grav plugins download page). I use the Form plugin and save user data to disk, both things you want to do. For ‘persistent-data’ I used ideas taken from the ‘comments’ plugin. Also I have tried to give a working example in the README.md documentation.
Richard
@finanalyst
Thanks for your example. As I understood the from lays in each *.md file? So the Event ‘onFormProcessed’ occurs, when a form in the specific page is processed.
I need an event, which only occurs, when I am saving the ne configurations for the plugin. Because my script in the Background checks for a new archive (with new pages, system.yaml) and will replace those with the current ones. So do I really need a form just to trigger my script? I would much more prefer it to just save the configurations and everything gets triggered that way. So still a wrong event I am picking there.
Very good documentation btw!
I will try to put a button there, as they have done it in git-sync plugin: https://github.com/trilbymedia/grav-plugin-git-sync