I’m currently using KQL to return an array of blocks from the blocks field. I have a custom block inside this which contains a files field to output an image.
I looked at this thread before posting, I think this differs slightly as that is overwriting the image field type. I’m looking at accessing a files field within a custom block it’s literally text and and image field so relatively simple.
I have no experience whatsoever with KQL. But I think you either have to make your custom blocks return the correct image urls from the media folder in the first place, or you deliver your images from the content folder instead of from the media folder. For this to work, you would have to change the .htaccess to allow accessing images from the media folder (blocked by default) and create a file::url component that does the job, for this see Protecting files behind a firewall | Kirby CMS
How could i ensure my custom block delivers the correct url from the files field though? would this be in a hacky way like a hidden field updated on a hook? or is there another way i could change the output without messing with the core?
The custom block I’m referring too is just a blueprint with a few standard fields, there is zero output on the kirby front end - everything is being accessed by KQL from another server.
But what I don’t really understand is why the image URLs are not resolved when you call toBlocks() and pass that to KQL. Like the same way it is resolved when you call toBlocks() in a template. Excuse my ignorance, as I said, never used KQL.
This is what is returned, wondering if its easier to just use content representations instead of KQL. It works brilliant for every other bit of data I need but only on blocks it seems a bit restrictive.
I can query the images of the page fine when they arent in blocks and get them into the front-end but would have to then match them based on the filename in the front-end which seems a bit hacky.
"code":200,
"result":{
"title":"Home",
"blocks":[
{
...
"content":{
"title":"this is a title",
"text":"this is some text",
"image":[
"image.jpg"
],
}
},
...
],
in an ideal world, what i’d like to output would be the following:
"code":200,
"result":{
"title":"Home",
"blocks":[
{
...
"content":{
"title":"this is a title",
"text":"this is some text",
"image":[
"https://mydomian.com/media/pages/somepage/image.jpg"
],
}
},
...
],
or for it to return the file object rather than just a filename.
The field method would then just call toBlocks, convert the result to an array, do the necessary edits and then return the array. I’d expect that to work.
It’s essentially the same work you would do in the content representation, but it’s accessible from KQL, and it could even allow you to parametrize the thing. Like, as an example, myFieldMethod("xs") could not only transform the blocks image names to files, but could also create thumbnails for them…
@rasteiner, That sounds like a good shout, I can find the image filename in the array but guess i’d have to find it by the blueprint key, meaning any blueprints where I add an image field, these would have to be called by name, right?
since the field only outputs an array of image filenames, there isnt any other way of searching? unless you could search purely for an array with certain content?
Hey @rasteiner, I made a working version which does exactly what I’m looking for but have hit an error now where I get the following The method “Kirby\Cms\Field::toblockshelper()” is not allowed in the API context, I have only just got this error so currently having a look to see if I can get around it.