$page->move() not working

Hey there! : )

This is probably a very basic question, but I can’t seem to get the $page->move() function to work and the official Guide doesn’t have any usage examples (https://getkirby.com/docs/reference/objects/cms/page/move).

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.

$article->move($article->parent()->find("archive"));

What am I missing? Thanks a bunch! : )

Edit: I’ve also tried DIR::move(), with the same unsuccessful outcome. : /

$archive = ($site->url()."/articles/archive");
Dir::move($article->url(), $archive."/".$article->slug());

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?

See also: Can't move some pages - #6 by steveforest

Thank you for the reply! : )

This is the full code including authentication:

//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 articles
$articles = $site->index()->filterBy("intendedTemplate", "article");

//Move old articles to archive subfolder.
$currentTime = time();
foreach ($articles as $article) {
    if ($currentTime - $article->date()->toDate() >= 31536000) {
        $article->move($article->parent()->find("archive"));
    }
}

And the archive blueprint includes a section allowing the article template:

title: Archive

columns:
  - width: 1/3
    sections:
      articles:
        headline: Archived articles (by date)
        type: pages
        templates:
          - article
        sortBy: date desc
        search: true
        limit: 5

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 tested your code in a 4.6.1 Starterkit.

  1. Added a movestuff.php next to index.php with your code from above

  2. Replaced articles with notes in this line

    $articlesFolder = $site->find('articles');
    
  3. Replaced article with note in this line:

    $articles = $articlesFolder->children()->filterBy("intendedTemplate", "article");
    
  4. Created a folder archive inside the notes folder, and an archive.text inside it

  5. Created a blueprint archive.yml in /site/blueprints/pages with the content from above, again changing the template from article to note.

  6. Ran php movestuff.php on the command line.

Maybe you can test the same in a new Starterkit.
For me, this worked as expected and I could find the pages correctly moved to /content/notes/archive.

1 Like

You’re absolutely right!

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! : )

Haha, yes, thought something like that was the case…