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

#1

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!

#2

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

#3

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

#4

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
#5

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

#6

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

#7

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

#8

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 …

#9

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

#10

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:

#11

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 …

#12

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

#13

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

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

#14

What package is that?

#15
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:

#16

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

#17

@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?

#18

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

1 Like
#19

Validator ? How do you mean?

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

#20

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?