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

how to make Avatar visible to registered members only?

Discussion in 'XenForo Questions and Support' started by erich37, May 12, 2012.

  1. erich37

    erich37 Well-Known Member

    I run a private forum and would like to show Avatars (thread creator avatar) to members only.

    So literally, visitors who are looking at a thread (thread-content is already made visible to registered members only) should not be able to see the custom avatar-image of the user. There should be just the default avatar visible for guests.

    The real avatar-image of the user should be visible to registered members only.


    Is there a way to achieve this ?


    Appreciate your help!
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Requires code edit:

    library/XenForo/Template/Helper/Core.php

    Add the red code:

    Code:
    	public static function helperAvatarHtml(array $user, $img, array $attributes = array(), $content = '')
    	{
    		if (!empty($attributes['size']))
    		{
    			$size = strtolower($attributes['size']);
    
    			switch ($size)
    			{
    				case 'l':
    				case 'm':
    				case 's':
    					break;
    
    				default:
    					$size = 'm';
    			}
    		}
    		else
    		{
    			$size = 'm';
    		}
    
    		$forceType = (isset($attributes['forcetype']) ? $attributes['forcetype'] : null);
    
    		$canonical = (isset($attributes['canonical']) && self::attributeTrue($attributes['canonical']));
    
    		$src = call_user_func(self::$helperCallbacks['avatar'], $user, $size, $forceType, $canonical);
    
    		$src = (XenForo_Visitor::getInstance()->user_id ? $src : call_user_func(self::$helperCallbacks['avatar'], $user, $size, 'default', $canonical));
    
    		$href = self::getUserHref($user, $attributes);
    		unset($attributes['href']);
    
    		if ($img)
    		{
    			$username = htmlspecialchars($user['username']);
    			$dimension = XenForo_Model_Avatar::getSizeFromCode($size);
    
    			$image = "<img src=\"{$src}\" width=\"{$dimension}\" height=\"{$dimension}\" alt=\"{$username}\" />";
    		}
    		else
    		{
    			$text = (empty($attributes['text']) ? '' : htmlspecialchars($attributes['text']));
    
    			$image = "<span class=\"img {$size}\" style=\"background-image: url('{$src}')\">{$text}</span>";
    		}
    
    		$class = (empty($attributes['class']) ? '' : ' ' . htmlspecialchars($attributes['class']));
    
    		unset($attributes['user'], $attributes['size'], $attributes['img'], $attributes['text'], $attributes['class']);
    
    		$attribs = self::getAttributes($attributes);
    
    		if ($content !== '')
    		{
    			$content = " {$content}";
    		}
    
    		return "<a{$href} class=\"avatar Av{$user['user_id']}{$size}{$class}\"{$attribs} data-avatarHtml=\"true\">{$image}{$content}</a>";
    	}
    
    That will make it so guests only see default avatars for everyone.
     
    Panupat and erich37 like this.
  3. erich37

    erich37 Well-Known Member

    Thanks a lot for this Jake!

    I just applied this code, but unfortunately the personal User-Photo (uploaded Avatar) is still showing at the Membercard and also at the User-Profile-Page.

    Is there a way to also hide the User-Photo and only show the default-avatar at the Membercard and at the User-Profile-Page (for guests) ?


    Appreciate your help!
    Many thanks!

    :)
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Those areas call on the helperAvatarUrl function in the same file. You need to make a similar change to that function.
     
  5. erich37

    erich37 Well-Known Member

    hmmm..... yeah, but how ? :D;)

    I am a noob :cautious::cool:


    Could you please help me out ?
     
  6. erich37

    erich37 Well-Known Member

    I would like to show the "Default Avatar" to guests at the "Membercard" and also at the "User-Profile Page".

    Is there anyone who could help me with the code-edit in order to achieve this ?



    Appreciate your help!
     
  7. bart

    bart Active Member

    I have a question that's in the same realm.

    I would not like guests to see the memberlist at all. Nor the profiles, nor the membercard. How do I do that?
     
  8. James

    James Well-Known Member

    You could do template conditionals to ensure a user is logged in, or alternatively you could make code edits or create an add-on to disallow access to guests.
     
  9. Brogan

    Brogan XenForo Moderator Staff Member

    bart likes this.
  10. bart

    bart Active Member

    Works!

    I use

    Code:
    <xen:if is="{$visitor.user_group_id}==2">
    so only usergroup 2 (members) can view the memberlist and not member awaiting approval or being banned.

    In the member_card-template you have to put the xen:if in between the main div.

    I did:
    member_list
    member_view
    member_card

    I think that's all...
     
  11. James

    James Well-Known Member

    Using that particular condition means that only people whose primary usergroup is members can see the memberlist. You should change it to use {xen:helper ismemberof, $visitor, 2}.

    Registered should be the primary usergroup anyway (as per the "official" way of using permissions and inheritance), but this is just in case it isn't.
     
  12. bart

    bart Active Member

    Thanks, that's better!

    One last question:

    - how do i disable the membercard completely?




    Sorry Erich37 for taking over your thread. But I think this might interest you too.
     
  13. erich37

    erich37 Well-Known Member

    no problem.


    1)
    Actually, this Addon from Chris works perfect:
    http://xenforo.com/community/resources/member-card-view-permissions.1193/

    2)
    Then I have added the code from Jake (as per his advice in this thread) and that code also works perfect.

    3)
    The only additional thing I would need is the code in order to show the default Avatar at the Membercard and also at the User-Profile (for guests).


    :)
     
  14. James

    James Well-Known Member

    bart likes this.
  15. bart

    bart Active Member

    Great, thanks!
     
  16. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Undo the previous edit and do this instead (add the red):

    Code:
    	public static function getAvatarUrl(array $user, $size, $forceType = '')
    	{
    		if (XenForo_Visitor::getInstance()->user_id && !empty($user['user_id']) && $forceType != 'default')
    		{
    			if ($user['gravatar'] && $forceType != 'custom')
    			{
    				return self::_getGravatarUrl($user, $size);
    			}
    			else if (!empty($user['avatar_date']))
    			{
    				return self::_getCustomAvatarUrl($user, $size);
    			}
    		}
    
    		return self::_getDefaultAvatarUrl($user, $size);
    	}
    
    That will make it so guests only see default avatars for everyone.
     
    erich37 likes this.
  17. erich37

    erich37 Well-Known Member

  18. dawg

    dawg Well-Known Member

    Hi Jake. Is this edit still valid? This is exactly what i need. I did the edit but got errors about a white syntax or something like that. Maybe i did it wrong, just checking since we are way beyond the version you suggested this for.
     
  19. RoldanLT

    RoldanLT Well-Known Member

    Working great for me, only thing it didn't work on notable pages :).

    Edit: Never mind, it was just my cache, it works great hehe.
     
    Last edited: Oct 11, 2014

Share This Page