Hello,
I looked at this page as well as forum posts and other cookbooks.
I would just like to list the users in alphabetical order of with “user.name” (I think it currently takes the id).
Is it possible or not?
I tested several things and I could see that it is possible to modify the data display. But I don’t understand how to do it.
Thanks
You have to over load the panels UsersView.vue
component in a plugin. I think i did something like this on the PHP side…
<?php
Kirby::plugin('custom/users', [
'areas' => [
'users' => function () {
return [
'views' => [
'users' => [
'pattern' => 'users',
'action' => function () {
$kirby = kirby();
$role = get('role');
$roles = $kirby->roles()->toArray(fn ($role) => [
'id' => $role->id(),
'title' => $role->title(),
]);
return [
'component' => 'k-users-view',
'props' => [
'role' => function () use ($kirby, $roles, $role) {
if ($role) {
return $roles[$role] ?? null;
}
},
'roles' => array_values($roles),
'users' => function () use ($kirby, $role) {
$users = $kirby->users();
if (empty($role) === false) {
$users = $users->role($role);
}
$users = $users->sortBy('name', 'desc', 1);
$users = $users->paginate([
'limit' => 20,
'page' => get('page')
]);
return [
'data' => $users->values(fn ($user) => [
'id' => $user->id(),
'image' => $user->panel()->image(),
'info' => Escape::html($user->role()->title()),
'link' => $user->panel()->url(true),
'text' => Escape::html($user->username())
]),
'pagination' => $users->pagination()->toArray()
];
},
]
];
}
],
]
];
}
]
]);
Which is similar to the way the panel does it with the slight tweak…
$users = $users->sortBy('name', 'desc', 1);
1 Like
Thank you very much @jimbobrjames , so much code for if possible. I wouldn’t have succeeded on my own!
To make the asc order work I modified just this line:
$users = $users->sortBy('name', 'asc');
le , 1
is not necessary.
your sample code would be great in the kirby documentation as it is something useful when dealing with a large number of users.
1 Like
The 1 makes it sort by natural order rather than computer order, as far as i know. Without it you can end up with freak occurances that are sorted as computers sort things, rather than as natural sorting would.
Like this:
Standard sorting
Array
(
[3] => img1.png
[1] => img10.png
[0] => img12.png
[2] => img2.png
)
Natural order sorting
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
Your call Probably less chance of it happening with alpha text rather than numbers.