[solved] Use a dropdown element to choose Joomla user groups

wagner.nardin

New Member
Hi

I would like to use a dropdown element to choose Joomla user groups
I believe the best way would be to create a dropdown element and add PHP code to read the mysql table 'jos_usergroups', so I'm using the following steps.

  1. I'm creating a new element with the 'dropdown' option for the plugin field.
  2. On the Data tab I have empty sub-options and multiple selections marked as 'no'
  3. On the front end additions tab I have the fields: Enable, Only add label and Save new additions, marked as 'no'.
  4. On the Advanced tab, I have the separator field 'empty' and the Eval populate field with the code:
    PHP:
    $db = JFactory::getDbo();
    $db->setQuery('SELECT id, text FROM jos_usergroups');
    $rows = $db->loadObjectList();
    foreach ($rows as $row) {
        $options[] = JHTML::_('select.option', $row->id, $row->text);
    }
    return $options;
  5. On the formatting tab I have the Enhanced dropdown field marked as 'no' and the Bootstrap class with 'input-medium'.
  6. On the Options tab I have the Default option 'empty' and the Eval option checked as 'no'.
Are there any further steps to follow?
Thanks.
 
I just figured out how to do it.
I used the plugin 'databasejoin' instead of the 'dropdown' plugin with the following steps.
  1. I selected the 'databasejoin' plugin.
  2. I just changed the 'Data' tab where
    • I marked the Render-as field with the Drop-down option.
    • I marked the Connection field with the site database option.
    • I marked the Table field with my user table, which in this case is 'jos_usergroups'.
    • I marked the value field with the table column 'id'.
    • And finally I marked the Label field with the table column 'title'.
  3. Now the user registration form is already working well.
if you have a better way to solve this problem, please write in this post.
 
Last edited:
Yes, that's the way to do it.

There is actually a 'usergroups' element, but that presents them checkbox style, like the group selection in the backend J! user manager, and stores multiple values. It also has the option to sync with a 'user' element on the form, so it shows the groups that user is a member of.

So if you just want a simple dropdown to select a single group, yes - a join to the #__usergroups table is what you want.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top