• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Xen code for showing group labels?

dmnkhhn

Active member
#2
Inside a model you need to get the UserGroup model first.
PHP:
    /**
     * @return XenForo_Model_UserGroup
     */
    protected function _getUserGroupModel() {

        return $this->getModelFromCache('XenForo_Model_UserGroup');
    }
and inside your method you can then use:

PHP:
$myId = 10;
$userGroup = $this->_getUserGroupModel()->getUserGroupById($myId);
This will get you all information about a usergroup with a certain Id. :)

You probably want to check what information it contains by doing
PHP:
print_r ($userGroup);
 

dutchbb

Well-known member
#3
Wow I'm no coder :p Is all that needed? I need the equivalent of this to use in message_user_info template:

Code:
<if condition="is_member_of($post,6)">
<img src="images/groups/admin.gif" width="128" height="25" border="0" alt="Administrator" class="grouptag" />
</if>
 

dmnkhhn

Active member
#4
Oh I see. :)

That should be easier to accomplish, hold on. ;)

Edit:
It seems to be more complicated than I thought.

Here is how I would do it (theoretically):

Extend the XenForo_Template_Helper_Core class.
This class contains an array ($helperCallbacks) that maps a template helper to a callback (callback = method like helperUserGroup or whatever you call it)
This array needs a new key that maps the (newly created) usergroup helper to a callback. This callback takes the $user and the usergroup in question as an argument and returns 1 if the user is in this usergroup and a 0 if he is not.
To get that information we need to use a the UserGroup model and the getUserIdsInUserGroup() method.


If that works you can use {xen:helper userGroup, $user, 6} to find out wether this user belongs to group 6 or not.

I have not tried it and I would love to hear what the experts say. :)
 

Shadab

Well-known member
#5
Code:
<xen:if is="{$visitor.user_group_id} == 3">
	Hi Administrator.
</xen:if>
Note that user_group_id only stores the primary usergroup id of a user; so you are only comparing the primary usergroup in the code above. Secondary usergroup id's are stored as a comma separated list in the secondary_group_ids field; but I haven't come across any native template helper that we can use here.

@dmnkhhn:
Interesting approach. Off to try it right now. :)

Edit:
Wait. Just checked and found my primary usergroup is "Registered", and "Administrative" + "Moderating" are set as my secondary groups, by default. That's strange. The code given above wouldn't work.
 

dutchbb

Well-known member
#7
Code:
<xen:if is="{$visitor.user_group_id} == 3">
Hi Administrator.
</xen:if>
Note that user_group_id only stores the primary usergroup id of a user; so you are only comparing the primary usergroup in the code above. Secondary usergroup id's are stored as a comma separated list in the secondary_group_ids field; but I haven't come across any native template helper that we can use here.

@dmnkhhn:
Interesting approach. Off to try it right now.
Going to try this now. Thanks.

Edit:
Wait. Just checked and found my primary usergroup is "Registered", and "Administrative" + "Moderating" are set as my secondary groups, by default. That's strange. The code given above wouldn't work.
Yes it seems we can only use a rank image for primary groups then? I know by default the Admin account is in the Registered group, you have to change that after installation.