Retrieve file path through Kirby API

Hi there,
I’m trying to use Kirby with Vue.js, as a headless CMS if understand well the concept. My problem is that I lose some great Kirby PHP methods like toBlocks() and the unavoidable toFile() which call the media file generated by Kirby. Through the API I only succeeded to retrieve the file name. Is there any way to get the path to the file through the API ? Do you see any other approach for solving the problem ?
Thanks

Is there any way to get the path to the file through the API ?

$file->realpath()

I misspoke. I’m fetching the data through the API in AJAX so I can’t use PHP methods. By the way, ideally I want to fetch the path to the file generated by Kirby. Is there any hook to the json documents ?

You might be interested in the KQL plugin, which makes things like this easier.

@rasteiner came up with a solution when I had a similar problem with using kirby as a headless CMS


use Kirby\Cms\App as Kirby;

Kirby::plugin('minml/myFieldMethod', [
    'fieldMethods' => [
        'myFieldMethod' => function($field) {
            $model = $field->parent();
            $blocks = $field->toBlocks()->toArray();

            if (class_uses($model, 'Kirby\Cms\HasFiles')) {
                array_walk_recursive($blocks, function (&$value, $key) use ($model) {
                    if (is_int($key) && is_string($value)) {
                        if($file = $model->file($value)) {
                            $value = $file->url();
                        }
                    }
                });
            }

            return $blocks;
        }
    ],
]);

This solution returns the full image path when using toBlocks

Interesting approach, thank you @MinmlCo

I’ll explore that, thanks @jimbobrjames