at the moment I am trying to convert the data of a structure to a json string.
Is there an easy way to do this, while also replacing all the strings in file/link fields into actual urls that I can link?
<?php
/** @var \Kirby\Cms\Block $block */
// Assuming $blocks is your Kirby blocks variable
$data = [];
foreach ($block->elements() as $elements) {
foreach($elements as $element) {
$element["portrait"] = page()->file($element["portrait"][0])->url();
array_push($data, $element);
}
}
?>
This is what I got so far. I do not understand why I have to double loop, but so be it.
What I am stuck with now is that for link fields, where I link to internal pages, which gives me a string like page://sO3r3W56B16Juh0S . So far, there is no way to make this to the actual url to put this into a href.
Is elements the structure field in question? If so, please see the structure field documentation how to convert a structure field to a structure object: Structure | Kirby CMS
Your assumption is correct, elements is the structure field in question.
This is the definition of the whole block:
fields:
elements:
label: Testimonials
type: structure
fields:
name:
label: Name
type: text
company:
label: Firma
type: text
quote:
label: Zitat
type: text
portrait:
label: Portrait
type: files
link:
label: Link
type: link
time:
label: Intervall (in ms)
type: number
default: 5000
I have tried to work with the toStructure() method that you linked and ended up with this code for a small tester (which is very close the the example provided):
This always leaves me with an empty array, even though the structure is definitely populated with data. I am using Kirby 4.3.0
Furthermore, my question was also, how can I convert an internal page id (or whatever this string is) like page://sO3r3W56B16Juh0S into a url or uri, so I can use it in an href. I couldnt find any method in the documentation for this yet.
Here, $item is a “row” in your structure, from here you have to access the individual fields with $item->fieldName(), e.g. $item->name();
If a field contains a reference to a page or file, you use the field converter methods intended for this purpose, so for a files field with a single file reference:
$fileUrl = $item->portrait()->toFile()?->url();
(all within the loop…)
You find example of using/converting field values in the documentation for each field (there’s almost always a section “How to use in your templates”, particularly for fields that need to be converted. And an overview of all available field methods here: Field methods | Kirby CMS.
This is no different whether these fields are used directly on the page level, or if fields are nested without an object field, a structure field or block types.