I’m trying to move a page to a subfolder of its parent. The current storage location of the article contains a subfolder called “archive”. However, the following code doesn’t to anything.
Do you authenticate the action ($kirby->impersonate('kirby) or with a real user)?
Does the target page blueprint allow the template of the page you are trying to move, i.e. it has a section with that template enabled?
Do you get an error message at all? Is debugging enabled in your config.php? If not, enable it and try to run the code again. The code itself should work as expected.
Hey & sorry for the delayed response! Had to jump to another project for a couple days.
No, I’m not getting any error messages and yes, debug mode is on.
In fact, I’m getting confirmation echos that the articles are being moved, so no exception is being thrown - but the articles just aren’t moved.
I checked the permissions in my local environment, which seemed fine, and then still switched to the online webhost - with the same result.
Here is the current code:
//Loading Kirby
require __DIR__ . '/kirby/bootstrap.php';
$props = [
'roots' => [
'index' => __DIR__ . '/',
'content' => __DIR__ . '/content',
'site' => __DIR__ . '/site',
],
];
//Initialize Kirby and site.
echo "Loading kirby...<br/><br/>";
$kirby = new Kirby($props);
$kirby->impersonate('kirby');
$site = kirby()->site();
//Get the articles folder
$articlesFolder = $site->find('articles');
if (!$articlesFolder) {
die("Articles folder not found!");
}
//Get the archive folder
$archiveFolder = $articlesFolder->find('archive');
if (!$archiveFolder) {
die("Archive folder not found!");
}
//Get articles
$articles = $articlesFolder->children()->filterBy("intendedTemplate", "article");
echo (count($articles)." articles found.<br/><br/>");
//Move old articles to archive subfolder.
$currentTime = time();
foreach ($articles as $article) {
$articleDate = $article->date()->toDate();
if ($currentTime - $articleDate >= 31536000) {
echo "Article " . $article->name() . " (Date: " . date('Y-m-d', $articleDate) . ") is older than 1 year.<br/>";
//Move the article to the archive folder
try {
$article->move($archiveFolder);
echo "Article " . $article->name() . " was successfully moved to the archive.<br/><br/>";
} catch (Exception $e) {
echo "Failed to move article " . $article->name() . ": " . $e->getMessage() . "<br/><br/>";
}
}
}
As mentioned: No exception is being thrown, so I’m not getting the “Failed…” echo. The articles to be moved are being identified correctly and the confirmation echo is displayed for every moved article.
I messed up and had a deprecated Kirby version in my local environment which I then unknowingly copied over to the online environment when I switched to testing online. I hadn’t noticed, since the frontend ran fine.
I’m so sorry for wasting your time - as you can imagine I feel quite stupid now. -.-
Thank you for all the effort you went through in trying to recreate the issue! : )