Hey!
I am new to the ‘page model’ and have now used it for the first time to create user pages from user profiles for a website. It works great!
At the same time, I have now thought of adding a link to the user page to create a link between the user profile and the page.
The problem is that when I’m not logged in, I get an error saying “You are not allowed to modify/edit account XYZ”.
So I was wondering if it’s a bad idea to do this in a page model?
If anyone has any advice I would really appreciate it!
My code looks like this:
<?php
use Kirby\Cms\Pages;
use Kirby\Cms\Page;
class personsdatabasePage extends Page
{
static $subpages = null;
public function subpages()
{
return static::$subpages = Pages::factory($this->inventory()['children'], $this);
}
public function children(): Pages
{
if ($this->children instanceof Pages) {
return $this->children;
}
$userPages = [];
$users = kirby()->users();
foreach ($users as $key => $user) {
$slug = Str::slug($user->id());
$userPages[] = [
'slug' => $slug, // or username if unique
'num' => $user->indexOf($users),
'template' => 'person',
'model' => 'person',
'content' => [
'title' => $user->username(),
'personImage' => $user->image() ? '- ' . $user->image()->uuid() : '',
'personName' => $user->personName(),
'personSurname' => $user->personSurname(),
'personBirthday' => $user->personBirthday(),
'personGender' => $user->personGender(),
'personTitle' => $user->personTitle(),
'personBiography' => $user->personBiography(),
'personSelectJob' => $user->personSelectJob(),
'personSelectDepartment' => $user->personSelectDepartment(),
'personPanelLink' => $user->panel()->url(),
]
];
// here we create the reference for the user profile link to the user page
// get the page
$page = $this->subpages()->find($slug);
$user = $user->update([
'personUserPageLink' => $page->url() ?? 'not found',
]);
}
return $this->children = Pages::factory($userPages, $this);
}
}```