Hi there,
I’m trying to develop a plugin, that basically fetches data from an API and saves this data as pages within a configurable folder. So far I’m doing the most of that in plain php and I’m just wondering if there is a better way using the giving Grav API. So I thought I just can create a “$page = new Page();”, adding content and header data, and just save it via “$page->save()”. But it seems, that I do not get that right. So, this is what I am doing so far:
// First check if folder an pages exist
$pagesPath = $locator->findResource('user://pages', true);
$dynPath = $this->config->get("plugins.voffice.dynPath"); // e.g. /dynpages
$dynFolder = $page->find($dynPath);
$existing = $page->evaluate(['@page.children'=>$dynPath]);
if (!$dynFolder) {
Folder::create($pagesPath . $dynPath);
}
foreach($existing as $e) {
Folder::delete($e->path());
}
$dynContent = getFromApi();
foreach($dynContent as $c) {
$page = new Page();
$page->title($c['name']);
$page->path($pagesPath . $dynPath . "/" . $c['pathname'] . "/" . "template.md");
$page->setContent($c['content']);
$page->header($c['props']);
$page->save(); // Does not write the file/folder
// manually do file operations
Folder::create(dirname($page->path()));
file_put_contents($page->path(), $page->raw());
}
Is this the right way to go or is there a “better” way for my approach?
And, btw. all my created md fies are empty so far, so the file is created, but the content is is not set…