There is no helper function for this but there are a few places in core extensions where this is done. The first part is getting the user groups with super admin permissions:
use Joomla\CMS\Access\Access;
use Joomla\CMS\Table\Table;
// Get root asset ID
$rootId = Table::getInstance('Asset')->getRootId();
// Get root asset rules.
$rules = Access::getAssetRules($rootId)->getData();
// Get root asset user groups.
$groups = $rules['core.admin'] ? $rules['core.admin']->getData() : [];
// Filter out disallowed groups.
$superUserGroups = array_filter($groups);
Once you have these, make a database query to get the emails:
use Joomla\CMS\Factory;
if ($superUserGroups)
{
$db = Factory::getDbo(true);
$query = $db->getQuery(true)
->select('u.email')
->from('#__users AS u')
->join(
'INNER',
'#__user_usergroup_map AS g',
'u.id = g.user_id',
)
->whereIn('g.group_id', $superUserGroups);
// Load the email addresses and remove duplicates.
$superUserEmails = array_unique($db->setQuery($query)->loadColumn());
}
You can, of course, modify the above query with additional conditions to ensure only unblocked users and/or only users with "Receive System Email" option are selected:
$query->where('u.block = 0')
->where('u.sendEmail = 1');
Also instead of removing duplicates with array_unique()
, the query can be modified to use DISTINCT
or GROUP
clause to retrieve a unique list in the first place. But this would probably be slower in most cases.