$page->createFile() not from a route

UPDATE : I tried the ‘$page->createFile()’ method inside the page template and it works, but it doesn’t within the route. The $page object is retrieved with : $page = $site->index(true)->findBy('uri', $data->pageUri)(where $data comes from the request body).


Hi there,

I’m updating a website from Kirby version 3.6.2 to last version (4.1.2). I replaced the kirby directory, updated the composer and trigerred a composer update. The panel is correctly updated, the front-end is rendered correctly. But…

In a route which creates blocks programmatically, the $page->createChild() method keeps throwing an error : Invalid page number: <Page title> (from /Users/adrienpayet/code/culturesvisuelles/kirby/src/Toolkit/Pagination.php, line 372).

Updating the blocks globally works, except in case of block type image because the createChild method throws the error. See the context bellow.

$fileName = $incomingBlock->content->image[0]->filename;

try {
    $tempFilePath = createTempFile($fileName, $incomingBlock->b64);
} catch (\Throwable $th) {
    return json_encode(
        [
          'error' => 'Cannote create temp file: ' . $th->getMessage()
        ]
    );
}

if ($page->files()->has(strtolower($data->page) . '/' . $fileName)) {
    return json_encode(['error' => 'Files already exists']);
} else {
    // CREATE FILE IN PAGE FOLDER
    try {
        $kirbyFile = $page->createFile(
            [
                'source' => $tempFilePath,
                'filename' => $fileName,
                'parent'   => $page,
            ],
            true
        );
    } catch (Exception $e) {
        return json_encode(
            [
              'error' => 'Failed to create file in page folder: ' . $e->getMessage(),
              'file' => $e->getFile(),
              'line' => $e->getLine(),
              'trace' => $e->getTrace()
            ]
        );
    }

    // CREATE BLOCK
    $preparedBlock = new Kirby\Cms\Block(
        [
            'content' => [
                'location' => 'kirby',
                'image' => $kirbyFile,
                'width' => $incomingBlock->content->width,
                'height' => $incomingBlock->content->height,
                'transform' => $incomingBlock->content->transform,
                'zindex' => (int)$incomingBlock->content->zindex,
                'color' => $incomingBlock->content->color,
                'isCover' => $incomingBlock->content->iscover
            ],
            'type' => 'image'
        ]
    );

    if ($incomingBlock->content->iscover == 'true') {
        $coverThumb = (string)$kirbyFile->url();
    }

    $newBlocks[] = $preparedBlock->toArray();
}

Any idea ?

Thanks a lot !

Do you define $site within your route?

The error message related to the pagination object. So what is the stack trace here? And from where are you calling the route?

I’m not really sure how but it works now. Thank you @texnixe !