First time hook

hey… it seems i’m totaly lost…
i made an event page with events as subpages.
works great

now, when i create or update an event, i want to generate an ICS file inside same folder…

so, for testing i tried in config.php:

    'hooks' => [
        'page.*:after' => function ($event, $page) {
            // check if we want to handle the action
            if (in_array($event->action(), ['create', 'update']) !== true) {
                // no, return early
                return;
            }

            // do something after a page gets created or updated
            $hfile = 'test.txt';
            F::write($hfile, "testtext");
        }
    ],

how to get the path of the current page where the hook was triggert? i want to store in there, not in root folder
how to get data of fields of this page?

thx a lot

You can use $page->root() that the absolute root to the page directory

And of course you can access all other page properties/fields via the $page variable.

yeah… would be nice… but:

Exception: undefined
Call to a member function root() on null

   'hooks' => [
        'page.*:after' => function ($event, $page) {
            // check if we want to handle the action
            if (in_array($event->action(), ['create', 'update']) !== true) {
                // no, return early
                return;
            }

            $hfile = "test.txt";
            F::write($hfile, $page->root());

        }
    ],      

or

   'hooks' => [
        'page.*:after' => function ($event, $page) {
            // check if we want to handle the action
            if (in_array($event->action(), ['create', 'update']) !== true) {
                // no, return early
                return;
            }

            $hfile = $page->root() . "/test.txt";
            F::write($hfile, "testtext");

        }
    ],      

if it where that easy… i would not fail =)

$page is available on before hooks… but it seems to be empty on after hooks.
but before does not include latest changes…


Edit: ok… it seems it’s not possible to mix create and update on after hooks

this works:

    'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            $hfile = $newPage->root() . "/test.txt";
            F::write($hfile, "testtext");

        },
        'page.create:after' => function ($page) {
            $hfile = $page->root() . "/test.txt";
            F::write($hfile, "testtext");
        },
    ],