Pass variable to Impersonate in a callback

Hi!
I’m trying to update content on a page with a route. I read in the docs that If want to only perform one action with admin privileges and not log out the current user I need to user $kirby->impersonate() with a callback. But I’m having issues passing variables to it.

$kirby = kirby();
$result = $kirby->impersonate('kirby', function () {
    page('myPage')->update([
        'myField' => $fieldData
    ]);
});

Here $fieldData is collecting user information and thats why I can not collect taht info after the impersonate.

Any help is really appreaciated, thanks!

So $fieldDate is defined outside of the function? Then you can pass it inside with a use statement

$kirby = kirby();
$result = $kirby->impersonate('kirby', function () use($fieldData) {
    page('myPage')->update([
        'myField' => $fieldData
    ]);
});
1 Like

Thank you! you are the best!

1 Like