- Joomla Version
- 5.3
- PHP Version
- PHP 8.4.x
- Hoster
- netzone.ch
ich möchte die Benutzergruppe des eingeloggten Users im Inhalt ausgeben. Gibt es einen einfachen weg?
Code
probiert habe ich es mit :
// Import Joomla's database classes
use Joomla\CMS\Factory;
// Get the database connection
$db = Factory::getDbo();
// Retrieve the current user ID
$user = Factory::getUser();
$userID = $user->id;
// 🔹 Fetch standard user profile data
$profileTable = '#__user_profiles';
$queryProfile = $db->getQuery(true)
->select('profile_key, profile_value')
->from($db->quoteName($profileTable))
->where($db->quoteName('user_id') . ' = ' . (int) $userID);
$db->setQuery($queryProfile);
$profileData = $db->loadAssocList();
if (!empty($profileData)) {
foreach ($profileData as $profile) {
$key = str_replace('profile.', '', $profile['profile_key']); // Clean field name
$this->set($key, trim($profile['profile_value'], '"')); // Store cleaned value
}
}
// 🔹 Fetch custom fields from `#__fields` and corresponding values from `#__fields_values`
$queryCustomFields = $db->getQuery(true)
->select('f.title, fv.value')
->from($db->quoteName('#__fields', 'f'))
->join('LEFT', $db->quoteName('#__fields_values', 'fv') . ' ON f.id = fv.field_id')
->where($db->quoteName('fv.item_id') . ' = ' . (int) $userID); // Match user ID
$db->setQuery($queryCustomFields);
$customFields = $db->loadAssocList();
if (!empty($customFields)) {
foreach ($customFields as $field) {
$key = strtolower(str_replace(' ', '_', $field['title'])); // Format field name
$this->set($key, trim($field['value'], '"')); // Store cleaned value
}
}
// 🔹 Debug Output for Standard & Custom Profile Fields
$outputProfile = [];
foreach ($profileData as $profile) {
$outputProfile[] = "<strong>{$profile['profile_key']}:</strong> " . htmlspecialchars(trim($profile['profile_value'], '"'));
}
$outputCustomFields = [];
foreach ($customFields as $field) {
$outputCustomFields[] = "<strong>{$field['title']}:</strong> " . htmlspecialchars(trim($field['value'], '"'));
}
$this->set("all_profile_fields", implode('<br>', $outputProfile));
$this->set("all_custom_fields", implode('<br>', $outputCustomFields));
Alles anzeigen
weiss aber nicht weiter...