Move Pages Plugin

Move Pages Plugin

Missing move pages feature for Kirby 3. You can move the pages under the parent pages you specify with this plugin.

Installation

  1. Download the latest release
  2. Unzip downloaded file
  3. Copy/paste unzipped folder in your /site/plugins folder

Usage

Don’t forget! To move a page under the another page, the parent page must support the page template you are moving. This option must be a query language string or an array of templates and must be a collection of pages, not a single page.

Templates

Simply specify under which parent templates a page can be moved.

# /site/blueprints/pages/project.yml
title: Project

options:
    # parent page templates where you can move this page
    move:
        - projects
        - portfolio

Query language

If you need a more complex logic or dynamic parent templates, you can use the query language.

# /site/blueprints/pages/project.yml
title: Project

options:
    # parent pages collection where you can move this page
    move: site.children.filterBy('template', 'in', ['projects', 'portfolio'])

Hooks

Available hooks for this plugin: page.move:before and page.move:after.

// /site/config/config.php
return [
    'hooks' => [
        'page.move:before' => function (Kirby\Cms\Page $page, ?Kirby\Cms\Page $parent = null) {
            // your code goes here
        },
        'page.move:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage, ?Kirby\Cms\Page $parent = null) {
            // your code goes here
        }
    ]
];

Sample use case: shop

For example, let’s assume that there is a multi-category structure on a shop site. You want to move a product to another category.

Content directory

  • /home
  • /about
  • /shop
    • /category-a
      • Product A
    • /category-b
      • Product B
    • /category-c
      • Product C
  • /contact

You can set move option with category template:

# /site/blueprints/pages/product.yml
title: Product item

options:
    move:
        - category

Or you can use query language:

# /site/blueprints/pages/product.yml
title: Product item

options:
    move: kirby.page('shop').children
1 Like

Version 2.1.0

:tada: Features

  • Added new hooks for the plugin: page.move:before and page.move:after.
1 Like

Love the hooks feature! :slight_smile:

Version 2.2.0

:sparkles: Enhancements

  • Supports moving pages to or from to root (site blueprint)

:bug: Fixes

  • Fixed moving top root pages that have no parent

:warning: Breaking Changes

  • parent hook argument can be null (nullable) when page model have no parent

Newbie question : how to handle a case where the catalog have multiple categories > sub categories > sub sub categories ? Currently the plugin only scan children at one level deep.

The plugin;

  • If you define pages templates it will scan the whole site.
  • If you define a query, it will only return the response of your query.

Please let me know your use case to help you.

Given the following document tree,



/home
/about
/documents
    /category-a
        Product A
        /category-a-a
            Product PAA
            /category-a-a-a
                Product PAAA
    /category-b
        Product B
        /category-b-b
            Product BB
            ..
    /category-c
        Product C
/contact


How to allow page Product C to be moved in the same category as product PAA ?

As far I understand your use case, you can try like following. Just add your category blueprint name into your product blueprint options.

# /site/blueprints/pages/product.yml
title: Product

options:
    # parent page templates where you can move this page
    move:
        - category

Thanks for your help. Setting the move options to the template name did the trick.

Quick question: Is there a way to put back a page in the root folder of content?

Yes. You need to type site blueprint name like that

options:
    move:
        - site
1 Like

Thank you!

Is there a reason why this isn’t being offered via composer while it is a free plugin? I saw that it has the custom free license from owebstudios but I think this doesn’t prevent publishing it in a registry. You could even create a custom registry that then would work for the paid plugins as well.

Hi, i have an issue with the hook: it doesn’t seem to be fired.

Here is the code inside the config file:

//Aggiorna il path della pagina quando viene spostata
'page.move:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage, ?Kirby\Cms\Page $parent = null) { 

      hntData_updatePagePath($newPage);
    },

Of course I’ve already checked that the function hntData_updatePagePath works correctly.

What can I check?

Hi!

I have few questions:

  1. Version of your plugin?
  2. Where and how do you define hook?
  3. Is the page moved smoothly?
  4. Have you tried the before hook to see if it works?

Hi

  1. 2.2.0
  2. Config file
  3. Yes
  4. Yes, before hook works.

I’ve tested with debugging like following and works great:

return [
    'debug' => true,
    'hooks' => [
        'page.move:before' => function (Kirby\Cms\Page $page, ?Kirby\Cms\Page $parent = null) {
            file_put_contents('page.move.before.log', json_encode($page->toArray()) . PHP_EOL . PHP_EOL, FILE_APPEND);
            file_put_contents('page.move.before.log', json_encode($parent?->toArray() ?? []) . PHP_EOL . PHP_EOL, FILE_APPEND);
        },
        'page.move:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage, ?Kirby\Cms\Page $parent = null) {
            file_put_contents('page.move.after.log', json_encode($newPage->toArray()) . PHP_EOL . PHP_EOL, FILE_APPEND);
            file_put_contents('page.move.after.log', json_encode($oldPage->toArray()) . PHP_EOL . PHP_EOL, FILE_APPEND);
            file_put_contents('page.move.after.log', json_encode($parent?->toArray() ?? []) . PHP_EOL . PHP_EOL, FILE_APPEND);
        }
    ]
];

I’m not sure what the problem is. What is your Kirby version?

Ok thanks, I will check again my code. Just a question: is the new path available somehow with the before hook?

Not available yet but I’ve noted this idea.

1 Like

Maybe I got it - it seems that not all the properties of the page are available through newPage. Just URI, Title and a few others. Isn’t it?