Buy Now
Sign in

Dropdown dynamically populated with all Users from a specific Group

fasenderos , August 16 2017
Answered
F
fasenderos
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
Attachments
Dropdown.PNG
Dropdown.PNG
(21.09 KiB)
230 Downloads/Views
Read_1.PNG
Read_1.PNG
(19.7 KiB)
223 Downloads/Views
Read_2.PNG
Read_2.PNG
(20.88 KiB)
232 Downloads/Views
Read_3.PNG
Read_3.PNG
(24.88 KiB)
228 Downloads/Views
Log.PNG
Log.PNG
(13.24 KiB)
202 Downloads/Views
F
fasenderos
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'
Attachments
Query.PNG
Query.PNG
(22.61 KiB)
219 Downloads/Views
F
fasenderos
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
J
jcm69
Thanks for posting the response. I try to learn CF6. I appreciate ! although not tested ;-)