Users preview for page-table plugin

Hello Kirby communit,

This question is probably better answerd by @sylvainjule if you have a minute :slight_smile:

Is it possible to show user name or email instead of ID inside pagetable.

Right now I have this as a result, wish makes kind of sense since it’s what is stored in the content file

userpagetable

Maybe I’m missing something like :

author:
       width: 1/6
       label: Auteurs
       text: '{{ page.auteur.toUser }}' // toUser and something like "mail" or "name"
       type: users

I was expecting something like in this screencapture from another kirby user:

Thanks in advance,

All my best and good health to all.

The toUser method returns a User object. So, in order to output the name for example, you’d have to call '{{ page.auteur.toUser.name }}'

The issue here, is what’d happen if there is no user found, you’d call the name method on null.

Better wrap this in a custom Field method:

Kirby::plugin('my/field-methods', [
    'fieldMethods' => [
        'toUsername' => function($field) {
            $user = $field->toUser();
            return $user ? $user->name() : '';
        }
    ]
]);

And then call '{{ page.auteur.toUsername }}'

As for the preview on the screenshot above, you’d have to recreate it in the field methods because I haven’t found time to add the field previews in pagetable.

1 Like

Merci Sylvain ,

Thank for the input on using a field method I’ll do it that way.

All my best !

I wanted to modify this Field method for multiple users, using toUsers this time, but can’t get it to display more than one user:

Kirby::plugin('my/field-methods', [
    'fieldMethods' => [
        'toUsername' => function($field) {
            $users = $field->toUsers();
            foreach ($users as $user) {
              return $user ? $user->name() : '';
            }
        }
    ]
]);

@cadars If you use return within a loop, your function will stop at the first iteration. So you would have to create an array of usernames first inside your loop, then implode into a string.

I’m still learning PHP, and now I see it wasn’t a Kirby related question, so thank you for your time, as always!

No worries.