Filter a user in a structure list

filter a user in a structure list

I created a small form which allows to add people to a list of events (3 fields: user, email, text).

These people can register only if they are logged into their account (so I can know who is registering and retrieve their user ID).

It works, everything is stored in a list structure, like this:

Registration-members:

- 
  members:
    - user://YptNkRHn
  email: mickael@yahoo.fr
  text: TTTTTTTT
- 
  members:
    - user://LVtYKrq4
  email: test@yahoo.com
  text: AAAAA

I would like to create a 2nd form to update the information.

The problem I’m having is that I’m looping through the whole thing and so I have 2 forms displayed.

I would like to display only the form of the user who is logged into his account and not all of them.

<?php 
//print_r($page->registration_members()->toStructure()->findBy('user', 'user://LVtYKrq4'));die;
//print_r($page->registration_members()->toStructure()->toArray());die;

    $items = $page->registration_members()->toStructure()->findBy('text', 'TTTTTTTT')->toArray();

    //print_r($items); echo "dans le foreach";
    foreach ($items as $item): 

    if ($item):
    print_r($item);


    endif;
    endforeach
?>


I think there are 2 things I can’t do:
Go look in the structure for the “members” with its identifier (it creates an array, which I can’t reach).
Once I have retrieved the “members” of the user, I would have to be able to compare the identifier of the person connected (I suppose with kirby->user()? )

Do you have any ideas ?

I don’t understand what you want to achieve.

Where does this come from, can’t see a loop that shows multiple forms.

To get the structure item with the current user:

$item = $page->registration_members()->toStructure()->filter(fn($item) => $item->member()->toUser() === $kirby->user())->first();

Note that this returns a single item, not a collection to loop through.

1 Like

Hello Sonja,

It’s exactly what I needed. Thanks you !

I have only one form that is displayed, the one that corresponds to my logged in user.

<?php if ($item = $page->registration_members()->toStructure()->filter(fn($item) => $item->members()->toUser() === $kirby-> user())->first()): ?>

// CODE

<?php endif; ?>

I will look tonight at the conditions for removing the ADD form (because only one request cannot be made).

And a DELETE button to delete the event registration.

If the person deletes his registration, he should be able to register again if he wishes.

Finally, I have to put a condition in case the date of the event has expired.

I’ll come back to you if I don’t understand how to do something.

Thanks again !