Have some progress with this.
Based on Virtual Pages Cookbook I create this:
On settings page I create field that is looking for json data
ce_import_settings:
label: Chose crypto exchanges for import
type: multiselect
options:
type: api
url: "{{ site.url }}/crypto-exchanges.json"
query:
text: "{{ item.content.title }}"
value: "{{ item.slug }}"
Then made little modifications with virtual page model
<?php
class CryptoExchangesPage extends Page
{
static $subpages = null;
public function subpages()
{
if (static::$subpages) {
return static::$subpages;
}
return static::$subpages = Pages::factory($this->inventory()['children'], $this);
}
public function children()
{
if ($this->children instanceof Pages) {
return $this->children;
}
$csv = csv($this->root() . '/crpyto-exchanges.csv', ';');
$children = array_map(function ($crypto_exchange) {
$slug = Str::slug($crypto_exchange['Scientific Name']);
$page = $this->subpages()->find($slug);
return [
'slug' => $slug,
'template' => 'crypto-exchange',
'model' => 'crypto-exchange',
'num' => 0,
'files' => $page ? $page->files()->toArray() : null,
'content' => [
'title' => $crypto_exchange['Scientific Name'],
'commonName' => $crypto_exchange['Common Name'],
'description' => $crypto_exchange['Description'],
'name' => $page ? $page->name()->value() : null
]
];
}, $csv);
file_put_contents(kirby()->root() . '/crypto-exchanges.json', json_encode($children));
$json = file_get_contents(kirby()->root() . '/crypto-exchanges.json');
$json_data = json_decode($json,true);
$ce_import = site()->ce_import_settings()->split(',');
$filtered = array_intersect_key($json_data, $ce_import);
return $this->children = Pages::factory($filtered, $this);
}
}
In my template still preset csv import but it will be changed on API import
the main solution is generate json that is based on $children function
file_put_contents(kirby()->root() . '/crypto-exchanges.json', json_encode($children));
then I get data from generated json and also get field values from settings that I’m checked to be generated as virtual pages
$json = file_get_contents(kirby()->root() . '/crypto-exchanges.json');
$json_data = json_decode($json,true);
$ce_import = site()->ce_import_settings()->split(',');
and the last thing is to check arrays and create virtual pages
$filtered = array_intersect_key($json_data, $ce_import);
return $this->children = Pages::factory($filtered, $this);
On demo data from csv all is works
I don’t know how this will be work on big data where will be 1500 items, and also I still didn’t understand how array_intersect_key check slugs with different structure of arrays, I think it may be problem if in json will be similar to slug data in Content field for example.
Maybe there is the way to optimise this code a little to be more correct @texnixe ?