Forums

Dropdown dynamically populated with all Users from a specific Group

fasenderos 16 Aug, 2017
Hi All,
I'm trying to dynamically populate a dropdown with all users of a predefined Group.
I have followed this tutorial and this post.
I set dropdown "options" to {var:read_data4}.
Then added 1 Read Data with two models: main model "Groups" (#__user_usergroup_map) and associated model "Users" (#__users) with "One matching record, foreign key at the related table" -> user_id.
In the main model Groups the "Fields to retrieve" are
Groups.user_id
Users.username

The result is a dropdown with value/label equals to "user_id" instead of "id" and "name".
I tried many combinations of relations, foreign key and fields to be retrieved without success, any suggestions will be much appreciated.
Thanks in advance
Andrea
fasenderos 16 Aug, 2017
I tried to run the query taken from the debugger and the result seems to be right (two colums: ID | NAME)
SELECT `Groups`.`user_id` AS `Groups.user_id`, `Users`.`name` AS `Users.name` FROM `y5xsn_user_usergroup_map` AS `Groups` LEFT JOIN `y5xsn_users` AS `Users` ON `Groups`.`user_id` = `Users`.`id` WHERE `Groups`.`group_id` = '10'
fasenderos 17 Aug, 2017
Answer
Finally I found a solution, although without a Read Data.
Set the dropdown "options" to {var:userlist} and add the following php code to a "Custom Code"

<?php
jimport('joomla.access.access');
jimport('joomla.user.user');
$users = JAccess::getUsersByGroup(10); // <----- replace 10 with the id of the desired usergroup
$userlist = array();
foreach ($users as $user_id) {
    $user = JFactory::getUser($user_id);
    $userlist[$user->id] = $user->name;
}
$this->set("userlist", $userlist);
?>


Andrea
jcm69 02 Nov, 2017
Thanks for posting the response. I try to learn CF6. I appreciate ! although not tested ;-)
This topic is locked and no more replies can be posted.