Show column for admin

How to hide a table column from guests and show it only to administrators in Joomla.

Overview

The issue is that older Joomla code for checking user groups does not work in Joomla 2.5 and 3.0, where users can belong to multiple groups.
Check the user's group IDs using the current Joomla method, then conditionally display the column only if the user belongs to the administrator group, such as Super Users.

Answered
fl flyboeing 09 Jan, 2015
Hello all,

I have a custom list with a html table for showing the data. The guests can see the contents of the table, but I would like to hide the column "edit" for them, so that column is only visible for administrators.

Back in the days that I used Joomla 1.6 I had the following code:

<?php
$user =& JFactory::getUser();
if ( $user->gid ) {
  $edit_record = "
<td'><b>Edit</b></td>"; 
} else {
  $edit_record = "";
}
?>
<?php echo $edit_record; ?>


But this doesn't seem to work in Joomla 2.5 and 3.0.

Can someone help me with this?

Regards,
Ruud
Gr GreyHead 10 Jan, 2015
Hi Ruud,

The problem is that Joomla! 2.5 and 3 allow the user to belong to more than one User group.

You can use $user->getUserGroups(); which will return an array of Group IDs. You can then use in_array() to see if there is a particular group in the array.

Bob
fl flyboeing 13 Jan, 2015
Answer
Hi Bob,

Thank you for your reply.

I found a solution:

<?php
    $user =& JFactory::getUser();
    foreach ($user->groups as $key => $value){      
    }
// 8 is the ID for super users
if ($key == "8") {
     echo "You are a super user!";
 } else {
     echo "Too bad, you are no super user!";
 }
 ?>


This is working for me, hope this will help others!🙂

Regards,
Ruud
This topic is locked and no more replies can be posted.