I have a function in a plugin that updates a field on a page. I have a weird behaviour.
After I update my page in this function and I dump it, it shows that the value is at the right spot and correct, but if I have a look in the panel, the field is empty.
<?php
use Kirby\Http\Request as req;
use Kirby\Cms\Page as page;
use Kirby\Cms\Site as site;
use Kirby\Toolkit\Date as date;
function parcel($uuid){
$order = page($uuid);
/*
I do a few things here.
*/
kirby()->impersonate('kirby');
if ($order->paymentComplete()->toBool()) {
$order = $order->update([
'parcel_id' => 'asdfgh' //$result->parcel->id
], 'en');
}
dump($order);
return;
}
Thank you. I put the function in a plugin and call the function
<?php
use Kirby\Http\Request as req;
use Kirby\Cms\Page as page;
use Kirby\Cms\Site as site;
use Kirby\Toolkit\Date as date;
function parcel($uuid){
$order = page($uuid);
/*
I do a few things here.
*/
kirby()->impersonate('kirby');
if ($order->paymentComplete()->toBool()) {
$order = $order->update([
'parcel_id' => 'asdfgh' //$result->parcel->id
], 'en');
}
dump($order);
return;
}
Kirby::plugin('hi/sendcloud',[
]);
This is my plugin and I just call the function with parcel from the config within a hook.
You are right. Sorry about that.
The problem is that my hook in the config is super slow because it has probably too many functions and it has an impact on all users visiting the website in this moment of executing the hook.
So I was thinking to rewrite the function and to update from the function itself to optimise. So in my config it would only be
parcel($uuid);
This was my message in the merx plugin to see how I can optimise the hook. My own approach was to optimise my functions so I have less things within the hook.
This is the Link to the Problem:
The problem of the page update appeared after putting it in the function. That’s why I asked for help in a separate topic.
While moving the stuff into functions is generally a good idea to keep your code clean, it won’t help speeding up the process.
It would be better to execute this stuff in the background, via cron jobs that take care of all this.
But apart from this, you actually slow down things, for example, you already have an $orderPage page object in your hook, but instead of passing this object to your function, you get the UUID, pass it to the function, just to try and get a page object from the UUID again. So an unnecessary page lookup.
Also, in your hook, you update the page twice (parcel_id and user_id, if not more in your other functions), it would make more sense to collect all the data your want to update the page with and then do this only once.
Thank you. This helped and makes absolute sense.
I think I managed to get most things worked out, but I had to keep the page update function in the config hook. In the Plugin function it did not work.