Possible Fix for Kirby-Builder Bug with Image Picker? (Invalid file model type: kirby-builder/pages/mypage/fields)

Hey Everybody!

I’ve been using timoetting/kirby-builder for almost every project now, because of its flexibility and modularity. It has been great except for one bug which i’d like to help fix, but i’m not capable enough.

The Bug has been discussed in #110 and #148 in the kirby-builder repo and #175 in the kirby-editor repo

There was a fix/hack by alancwoo wich worked but is rather convoluted and prone to breaking when changing things.

Two weeks ago Jonathan Reisdorf found the culprit, described here:
There is a route in kirby core /kirby/config/api/routes/files.php on Line 27 with a pattern (:all)/files
Commenting out the route, the image picker in kirby-builder works again, but the single pages for files break. It seems this gets picked over the route in kirby builder for some reason.

I tried changing some stuff and saw, in the route above, the pattern (:all?), with the question mark added. Changing the route in kirby core on Line 27 to

    [
        'pattern' => '(:all?)/files',
        'method'  => 'GET',
        'action'  => function (string $path) {
            return $this->parent($path)->files()->sortBy('sort', 'asc', 'filename', 'asc');
        }
    ],

seems to fix the bug in kirby builder and doesnt break the single pages for files.

I dont know what that does though, as i could not find anything concerning the questionmark inside dynamic placeholders in patterns. Can someone explain what this does and if this could be a viable fix to get around the bugs?

I would love to dig deeper myself, but my understanding of php and kirby isn’t there yet. Maybe this is a starting point for someone so i can pick it up again or something!

Thanks for any help in advance!

1 Like

this is also discussed here:

if this post is more fitting as a comment to the existing thread, i’ll gladly move it!

bump

could someone from the core team or with sufficient knowledge tell me what the difference the question mark actually makes when adding it to L27 in the file /kirby/config/api/routes/files.php as in

'pattern' => '(:all?)/files',

maybe this could be patched into kirby-core because it doesnt break anything else?

thanks in advance!

1 Like

Normal regex patterns also work in routes. ? is the lazy quantifier, meaning “zero or more times”. It’s used to match as few characters as possible. I don’t know how the Kirby patterns work under the hood (in my understanding the ? shouldn’t make a difference here) but it seems to make the Builder route more specific.

1 Like

thanks for the reply! i made a pull request with the needed change (https://github.com/mourique/kirby/pull/1) It’s my first one ever, so i probably messed something up. But i am willing to learn!

not sure if this is the right way to go about it, but i would love to see this fixed. Maybe someone smarter can tell me why this is a bad or good idea. :slight_smile:

1 Like

As fate would have it I just ran into this issue myself so I will try to dig deeper later.

1 Like