Page Model, User Profile Content Update

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);

  }
}```

It’s in so far a bad idea that this code will update all the subpages each time e.g. a page gets loaded (each time the children are generated).

I would probably look into creating a user model an move personUserPageLink there as a method. Something like

public function personUserPageLink(): \Kirby\Cms\Page|null
{
  return page('path/too/persoonsdatabase')->children()->find($this->id());
}

sweet!
Thank you for you’re reply :balloon:

That works! Thank you @distantnative.

Maybe one last question
my code looks now like this:

<?php 

class EditorUser extends User
{
    public function personUserPageLink(): String|null
    {
      return site()->getPersonDatabase()->children()->find($this->id())->url();
    }
}

class EmployeeUser extends User
{
    public function personUserPageLink(): String|null
    {
      return site()->getPersonDatabase()->children()->find($this->id())->url();
    }
}

class PersonUser extends User
{
    public function personUserPageLink(): String|null
    {
      return site()->getPersonDatabase()->children()->find($this->id())->url();
    }
}

Is it possible to summarize those classes to one?

Thank you already! :slight_smile:

In that case, maybe you’re better suited with adding it as a custom user method - this will then be available on any user, no matter the role (and thus no matter the specific user model).

1 Like

works perfectly.
Thanks!

'userMethods' => [
    'personUserPageLink' => function () {
        return site()->getPersonDatabase()->children()->find($this->id())->url();
    }
]

# use 

$user->personUserPageLink();