Hi,
I am working on a custom-color-field and experimenting with fetching data from a settings page.
As far as I understand there are two ways to do this:
- fetching the entire page via the api in javascript
methods: {
async getColors() {
let response = await this.$api.get('settings/colors');
this.colors = response.content.section_block_colors
},
}
- Fetching the data with a custom route
'api' => [
'routes' => function ($kirby) {
return [
[
'pattern' => 'section-block-colors',
'action' => function () use ($kirby) {
return [
'colors' => $kirby->pages('settings/colors')->section_block_colors()
];
}
]
];
}
],
Unfortunately i get an error when trying to access the color-field via ->section_block_colors() .
I only can pass the full page object.
Why can´t I pass the field-values?
In the cookbook-recipe »my first panel area« it is possible to pass data directly via a prop:
// https://getkirby.com/docs/cookbook/panel/first-panel-area#setting-up-the-php-part
'views' => [
[
'pattern' => 'moviereviews',
'action' => function () {
// get movie reviews from API
$reviews = [];
$apikey = 'your API key';
$request = Remote::get('https://api.nytimes.com/svc/movies/v2/reviews/picks.json?api-key=' . $apikey);
if ($request->code() === 200) {
$reviews = $request->json(false)->results;
}
return [
'component' => 'moviereviews',
'props' => [
'reviews' => $reviews,
'size' => 'small',
'layout' => 'cards',
],
];
}
]
]
Is there a way to pass data to a block like this?
Which is the best way of doing this?
Best
Peter