Difference between revisions of "Add the custom element based on the Joomla User Group"

(Template Override Is The Way To Go)
Line 7: Line 7:
  
 
==Template Override Is The Way To Go==
 
==Template Override Is The Way To Go==
While Joomla users are accustomed to use plugins to change the certain behavior of Joomla functionality, in this particular case it would be very hard to do it with a plugin because every Joomla site have different user groups, with the different unique ID which in turn would cause plugin to display nothing and give impression that it "don't work".
+
While Joomla users are accustomed to use plugins to change the certain behavior of Joomla functionality, in this particular case it would be very hard to do it with a plugin because every Joomla site have different user groups, with the different unique ID which in turn would cause plugin to display nothing and give impression that it "don't work".<br/>
 +
That said, we have to create template override for a place where we want our badge to show up.<br/>
 +
We suggest you to read the [https://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core Joomla documentation article] about template overrides to get better understanding of this feature provided with every Joomla installation.
 +
 
 +
==The Code You Need==
 +
Here is the block of code you need to use in your override
 +
<syntaxhighlight lang="php">
 +
<?php
 +
// Lets obtain the user object first
 +
$juser = CFactory::getUser(); // Get the user object you want to display the badge for. On profile page you can use $juser = CFactory::getRequestUser(); to obtain the user object of the user we are looking at.
 +
$juserid = $juser->id; // extract the user ID
 +
 
 +
// Now, lets get the array of all Joomla User Groups user is member of
 +
jimport( 'joomla.user.helper' );
 +
$groups = JUserHelper::getUserGroups($juserid);
 +
?>
 +
 
 +
// Display the badge if user is member of the Joomla User Group with ID 8
 +
<?php if(in_array(8, $groups)): ?>
 +
<img src="/path/to/badge.jpg"/>
 +
<?php endif ?>
 +
</syntaxhighlight>
 +
<br/>

Revision as of 04:35, 16 June 2016

Overview

On our community website we are placing the mini badge in every activity stream item which help us distinguish between users with active, expired, or no license at all.

Badge-activity.png

In our case, the little green badge in lower right-hand corner JS Active help us determine that user is having an active license, so our support team can react accordingly.
In other use cases, you might want to have a VIP users, or simply "tag" the people that belong to a different Joomla User Group like this (notice the Staff.png badge under avatar)

Staff badge.png

Template Override Is The Way To Go

While Joomla users are accustomed to use plugins to change the certain behavior of Joomla functionality, in this particular case it would be very hard to do it with a plugin because every Joomla site have different user groups, with the different unique ID which in turn would cause plugin to display nothing and give impression that it "don't work".
That said, we have to create template override for a place where we want our badge to show up.
We suggest you to read the Joomla documentation article about template overrides to get better understanding of this feature provided with every Joomla installation.

The Code You Need

Here is the block of code you need to use in your override

<?php 
// Lets obtain the user object first
$juser = CFactory::getUser(); // Get the user object you want to display the badge for. On profile page you can use $juser = CFactory::getRequestUser(); to obtain the user object of the user we are looking at.
$juserid = $juser->id; // extract the user ID
 
// Now, lets get the array of all Joomla User Groups user is member of 
jimport( 'joomla.user.helper' );
$groups = JUserHelper::getUserGroups($juserid);
?>
 
// Display the badge if user is member of the Joomla User Group with ID 8
<?php if(in_array(8, $groups)): ?>
	<img src="/path/to/badge.jpg"/>
<?php endif ?>