There is a bug in that code.
If the user is a superuser (group 8), but is also in another group, then the above code will turn on, and off, the section 2. The end result is that it
may be on, or it
may be off, depending upon the order that the groups were listed in.
Example. I am in group 6 and 8. If Joomla returns "6,8", then the first time through the foreach it will be 6 and turn section 2 OFF. Then it will go through the foreach with the value of 8 and turn section 2 ON. This gives the expected result of ON for group 8.
But if Joomla returns the groups as "8,6" then the first time through with value 8 turns section 2 ON, but then going through the foreach with a value of 6 would turn it OFF. The end result is that it would be OFF for me, even though I am in group 8 and should see it.
The following code clears that issue up by first setting a variable to FALSE, then turns it to TRUE if
any of my groups is an 8. It doesn't matter what order the groups are returned in, if it foreach ever gets an 8, it will set the variable to TRUE. After all the groups have been checked we then turn ON or OFF sections based upon that variable.
$user = JFactory::getUser();
$groups = $user->get('groups');
$super = FALSE;
foreach($groups as $group)
{
if ($group=='8') $super = TRUE;
}
if ($super)
{
echo "<script>
jQuery( document ).ready(function() {
bfToggleFields('on','section','section2',bfDeactivateField);
bfToggleFields('on','section','section1',bfDeactivateField);
});
</script>";
} else {
echo "<script>
jQuery( document ).ready(function() {
bfToggleFields('off','section','section2',bfDeactivateField);
bfToggleFields('on','section','section1',bfDeactivateField);
});
</script>";
}