I am currently trying to let the admin group signed-up users by a custom attribute called personal_id in the panel. e.g. users from personal_id 4 to personal_id 5 should be added to the group. I got this working by using a hook that reads the from and the to field in the panel and when the page is updated set the group attribute of the according users to the title of the group page. See the screenshots:
However this group attribute is just a string and I would like for it to be a link to the according group page that also updates when the page name changes (I already have a hook for titleChange, but still only changes the string). In WordPress with ACF I would do that with a relationship field. I tried this plugin:
but I don’t think it’s exactly what I need.
On the other side I want the group page to display the related users that have been assigned to the group. I figured I can use the standard users field for that, but I can’t seem to figure out how to programatically add users to the field. I tried using the page update function like so but it didn’t work:
$users = $this->users();
$users->filterBy('personalid', '>=', $from)->filterBy('personalid', '<=', $to);
'test3' => $users
Thank you very much for your help or advise!
It would be enough to have the group assigned without explicitly storing the title of the page in the user file and then filter based on that condition.
Even if you really wanted to store this information in the user file, it would make more sense to store an ID that doesn’t change, the title is not a good idea.
Thank you sincerely for your answer. It certainly makes sense to store the group id instead of the title as a string. I however thought it would be helpful for tracking to have the group attribute stored in the user file as well. But I will probably have to start looking into matomo first and see what exactly I need for that.
The other question still remains. I still can’t figure out how to programatically add users to the group and then display them in the users field.
Again, this is not really needed, because you could fetch the relevant users in your templates etc. by filtering the users by your range of user IDs, as in the code below.
If you really want, you can then use this data to update the page with the user data in a page.update:after hook, i.e. filter all users by the range of user IDs and then update the field.
//Create an array of persona ids from from and to values
$rangeOfIDs = range($page->from()->toInt(), $page->to()->toInt());
// get an array of user IDs
$users = $kirby->users()->filterBy('personal_id', 'in', $rangeOfIDs)->pluck('id', ',');
//update the users field with the yaml encoded array of user ids
$page->update(['users' => data::encode($users, 'yaml')]);