Create a page in a plugin

Hi,

I would like to create a plugin that add a News section to the panel, so I want to create a page named News that will contain all the individual New.

Kirby::plugin(kirby-news, [
    'pages' => [
        'news' => new Page([
            'slug' => 'news',
            'template' => 'news',
        ])
    ],
]);

I did this and it create the page like I want but when I then create an individual new, the page switch to the default blueprint.
Moreover, while there is no individual new, the page isn’t created in content and when I create an individual new, the page news is created but its name is default.txt
Is there a way to create the /content/news/news.txt file automatically when the site is started ?

This creates a virtual page. It is not useful if you want the page to have “real” children in the content folder.

Why are you doing it this way?

I have tried other methods but they didn’t work.
I would like to create the page on website initialization or when the plugin is import but I don’t figure out how to do it.

Maybe use a route to create the page when the website is opened the first time, but make sure this happens only once.

I tried to create a hooks in my plugin:


Kirby::plugin('kirby-news', [
    'hooks' => [
        [
            'system.loadPlugins:after' => function () {
                Page::create([
                   'slug' => 'news',
                   'template' => 'news',
                   'content' => [
                       'title' => 'News'
                   ]
                ]);
            }
        ]
    ],
]);

But, it seems to have no effect.

Don’t you have debug enabled?

  1. Your plugin name is not valid it must be in the format xxx/yyy, see docs.
  2. You need to authenticate when trying to create a page, see Permissions | Kirby CMS

I’m sorry, I miss copied my code, the name is ‘hugo/news’.
For the permission, I run the site locally and I’m logged with an admin account.
‘debug’ is set to 'true in my config file.

Ah, there’s also a syntax issue, because your hook is wrapped in an extra array, which is not correct.

It seems to work, I added a condition to create the page only if it doesn’t already exist.
Thank you for you help