`page->update()` won't work inside hook

Hello fellow K3 users,
I need to fill a field with a name, then press a button (Vue component? Field Method? saving the page would be fine, too -> page.update.after or page.update.before?!) upon which the fields value (name) is given to an external API, whose response I’ll be using to fill some other fields on the page. Is there a tutorial on this already that I missed? Any experience with stuff like that?

thx!

You could probably use the Janitor plugin: https://github.com/bnomei/kirby3-janitor

I already got the API call, I’m just unsure where to put it / how to trigger it … as I edited, upon savng the page would be just fine

If on update is enough, a hook could do the job:

    'hooks' => [
        'page.update:after' => function ($page) {
            // get stuff from API and update page with result
            $page->update(['text' => 'lorem ipsum']);
        }
    ]
1 Like

This indeed will help, I’ll get back at you to let you know!

That would go only inside a plugin file or would controller be appropriate, too?

Hooks have to go either into the config or into a plugin, not into a controller, I think.

Anyway, I get this:

Fatal error: Cannot redeclare load() (previously declared in /app/vendor/getkirby/cms/config/helpers.php:481) in /app/vendor/getkirby/toolkit/bootstrap.php on line 21

Mhh, after deleting a composer package that requires Toolkit v2.5.12, it works again …

I’m a bit irritated. Are we talking about Kirby 2 or 3? What is the 2.5.12 toolkit doing there?

I’m using v3, the included package uses helper functions from the v2 toolkit :confused:
Can this be resolved somehow? Contacting the package maintainer is easy, as he’s a friend of mine :slight_smile:

I mean, K3 should prevail over external packages, no? I’m not that familiar with the inner workings of Composer et al … or namespacing it …

I don’t know what you mean with included package…

I included a composer package … which requires Toolkit v2.5.12:

a composer package that requires Toolkit v2.5.12
Post #8

What package is that?

Kirby::plugin('some/plugin', [
    'hooks' => [
        'page.update:after' => function ($page) {
            if ($page->intendedTemplate() == 'some-page') {
                try {
                    $page->update([
                        'text' => 'this is text',
                    ]);
                } catch(Exception $e) {
                    throw $e;
                }
            }
        }
    ]
]);

… doesn’t work, and I don’t get why … the text field simply doesn’t get updated :frowning:

Could you try to use $page->save() instead of the $page->update() method?

@lukaskleinschmidt Was there an issue with $page->update() in a hook (can’t find it)? Because the above code works for me as expected.

@daybugging Have you defined any validation settings for this field?

@texnixe You are right. I mixed it up with something else.

1 Like

Validator ? How do you mean?

Using Save worked one out of many times, but this one Time only After reverting changes, it appeared.

I meant if you have set any min or max characters/words for the field. Because if you use $page->update() the blueprint settings are taken into account.

Is the text really not saved to file or are changes just not reflected in the Panel?