Showing pages created by a user in the panel account page for admins

The pages I want to show are all of the same template. That template has a field user, that only contains one user.
Can I show all the pages related to a user in /panel/users/XYZabcde?

Here’s what I have in the user blueprint so far (showing all titel pages for that one specific user “GtpY9e9K” in all user accounts, of course):

sections:
  pages:
    label: Anzeigen
    type: pages
    create: false
    parent: site.find("titel")
    query: site.find("titel").childrenAndDrafts.filterBy('user', '==', '- GtpY9e9K')

How can I filter/query for the user, who’s account page I am currently viewing?
Is it at all possible?

You’ll probably have to move this to a user method or site method or so as the query language doesn’t support string concating. But I think user gives you the current user, while model gives you the user that you’re viewing in the query language.

So maybe something like

query: model.titelPages

and


Kirby::plugin('my/plugin', [
    'userMethods' => [
        'titelPages' => function () {
            return $this->site()->find('titel')->childrenAndDrafts()->filterBy('user', '==', '- ' . $this->uuid()->toString());
        }
    ]
]);

(untested)

I’d implement the method a bit differently, trying to avoid the hack with the prepended dash:

Kirby::plugin('my/plugin', [
    'userMethods' => [
        'titelPages' => function () {
            return $this->site()->find('titel')->childrenAndDrafts()->filter(fn($p) => $p->user()->toUser() === $this);
        }
    ]
]);
1 Like

Thank you, Nico.
query: model.titelPages
doesn’t seem to return anything, though. Even if I delete the filterBy() part (or filter in Sonja’s suggestion), no pages show in the section.

(If I use query: user.titelPages, it indeed returns the pages of the user viewing the page. As you suspected.)

I tried as a user method and as a site method.
For the latter the problem seems to be getting the currently viewed user into the method dynamically, because if I hardcode a user ID into the site method (or into the call of the method), I get to see that specific user’s pages.
query: site.titelPages(user.id) doesn’t work, as well as query: site.titelPages("{{ user.id }}").

How does it fail? The method just doesn’t receive anything? Something wrong?

For all I know, in the context of a pages section, model refers to the parent page object of the pages section, not to the user. So wondering if getting the currently selected user is even possible without some custom section

Sorry, that one does return the pages of the user viewing the account.
Getting dizzy here testing different combinations.

I don’t know, how to debug, what the method receives (other than seeing no pages returned).
What I do know is, that
query: site.titelPages("XaYbZc") does work, but of course the section then shows user XaYbZc’s pages on every account page.
The method being:

Kirby::plugin('frwssr/site-methods', [
        'titelPages' => function ($userViewed) {
            return $this->site()->find('titel')->childrenAndDrafts()->filterBy('user', '==', '- ' . $userViewed);
        }
    ]
]);

Funny thing is, this field

test:
  type: info
  text: "{{ user.id }}"

gives me the ID of the user, whose account is being viewed.
This on the pages section

query: site.titelPages(user.id)

gives me pages of the user viewing the page.

A viable workaround for me would be, if I could add a kirby-panel-button (GitHub - moritzebeling/kirby-panel-button: Custom Kirby Panel button field to open or trigger URLs), that opened the titel page, with a search result on pages of the user.
Is there a way to trigger a panel page with search parameters, that are being applied to a section?
Like /pages/titel?section=list&q={{ user.id }}?
(That’s another unmanageable hurdle, right there, I guess?)
The query part does return the desired ID in this case. I tested.

You’re probably right here. I remembered another case where it was model = user viewed, user = current user. But might be different here in the pages section.

I’ll try to look closer into how one could get the user viewed (not current signed-in user).

Maybe try this hack:

	'siteMethods' => [
		'titelPages' => function () {
			return site()->find('titel')->childrenAndDrafts()->filter(fn($p) => $p->author()->toUser() === kirby()->user(basename($_SERVER['HTTP_REFERER'])));
		}
	]

That looks spectacular, Sonja. :clap:
Had to combine it with Nico’s syntax, though, to make it work:

'titelPages' => function () {
  return $this->site()->find('titel')->childrenAndDrafts()->filterBy('user', '==', '- ' . kirby()->user(basename($_SERVER['HTTP_REFERER'])));
},

Don’t know, why the filter(fn()) bit doesn’t work.
But with the above I get the desired pages on the user account pages!
(Downsides being: it looks a bit hacky—and it doesn’t work for the account page of the logged in admin, as that URL doesn’t contain the user ID. That I can totally live with, though.)

You guys are awesome.
Thank you for persisting and for bearing with me!

Sure it was a syntax issue and not the wrong field name in my code above (author instead of user)?

We can also use a fallback like this:

Kirby::plugin('your/plugin', [
	'siteMethods' => [
		'titelPages' => function () {
			$user = basename($_SERVER['HTTP_REFERER']) === 'account' ? kirby()->user() : kirby()->user(basename($_SERVER['HTTP_REFERER']));
			return site()->find('notes')->childrenAndDrafts()->filter(fn($p) => ($p->user()->isNotEmpty() && $p->user()->toUser() === $user));
		}
	]
]);
2 Likes

You are correct about it not being a syntax issue, but the wrong field.

And your fallback solution works like a charm—for the logged-in admin’s account page, too. Brilliant!