This is a rather long and complicated answer but you'll need a custom script to do that, so this is essentially what you are trying to do:
1. Load Joomla framework: The script begins by loading the Joomla framework to get access to the Joomla API.
2. Define group IDs: The group IDs you want to add (13 and 16) are specified in an array.
3. Fetch current user groups: It retrieves the current groups the user is a member of.
4. Merge groups: Combines the current groups with the new ones, ensuring no duplicates.
5. Update database: It deletes the user's current group mappings and inserts the new group mappings.
But you'll need to break this into two parts, the first when the form loads and the second to update the user when the form is submitted. This is written for Joomla 4, if you are still on J3 it's slightly different.
Here's part 1which you put in PHP scripts - pre processing/script called before form is generated. You'll need to change the value of the $targetGroupName variable for yuor own group and later comment out the final echo line.
// Load the Joomla framework
use Joomla\CMS\Factory;
use Joomla\CMS\Session\Session;
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
// Instantiate the application
$app = Factory::getApplication('site');
// Get the currently logged-in user
$user = Factory::getUser();
// Check if the user is logged in
if ($user->guest) {
die("Error: No user is currently logged in.");
}
// Get the user ID of the currently logged-in user
$userId = $user->id;
// Define the target group name
$targetGroupName = 'Super Users'; // Change this to the desired group name
// Get the database connection
$db = Factory::getDbo();
// Fetch the ID of the target user group
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('title') . ' = ' . $db->quote($targetGroupName));
$db->setQuery($query);
$targetGroupId = $db->loadResult();
// Check if the target group was found
if (!$targetGroupId) {
die("Error: '$targetGroupName' user group not found.");
}
// Fetch the current user groups
$query = $db->getQuery(true)
->select($db->quoteName('group_id'))
->from($db->quoteName('#__user_usergroup_map'))
->where($db->quoteName('user_id') . ' = ' . $db->quote($userId));
$db->setQuery($query);
$currentGroups = $db->loadColumn();
// Store the necessary data in the session for later use
$app->setUserState('com_custom.form.userId', $userId);
$app->setUserState('com_custom.form.currentGroups', $currentGroups);
echo "Form loaded. User ID: $userId, Target Group ID: $targetGroupId";
Here's part two which goes in PHscripts - script called on form process, again comment out the last echo statement
// Load the Joomla framework
use Joomla\CMS\Factory;
use Joomla\CMS\Session\Session;
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
// Instantiate the application
$app = Factory::getApplication('site');
// Get the data from the session
$userId = $app->getUserState('com_custom.form.userId');
$currentGroups = $app->getUserState('com_custom.form.currentGroups');
// Define the group IDs to add
$groupIdsToAdd = [13, 16];
// Merge the current groups with the new group IDs, ensuring no duplicates
$newGroups = array_unique(array_merge($currentGroups, $groupIdsToAdd));
// Get the database connection
$db = Factory::getDbo();
// Remove all current group mappings for the user
$query = $db->getQuery(true)
->delete($db->quoteName('#__user_usergroup_map'))
->where($db->quoteName('user_id') . ' = ' . $db->quote($userId));
$db->setQuery($query);
$db->execute();
// Insert the new group mappings
$query = $db->getQuery(true)
->insert($db->quoteName('#__user_usergroup_map'))
->columns([$db->quoteName('user_id'), $db->quoteName('group_id')]);
foreach ($newGroups as $groupId) {
$query->values($db->quote($userId) . ', ' . $db->quote($groupId));
}
$db->setQuery($query);
$db->execute();
echo "User ID $userId has been updated with the new groups: " . implode(', ', $newGroups);
That should do it or at least give you somewhere to start from.