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

XF 1.2 Conditional to say "if default avatar"

Discussion in 'XenForo Questions and Support' started by jdeg, Jan 25, 2014.

  1. jdeg

    jdeg Active Member

    Is there a conditional that I can use everywhere an avatar is displayed to say if default (or if custom) avatar, do this....
     
  2. Syndol

    Syndol Guest

    To find out more on how avatars are being called take a look at the avatar functions in file XenForo/Template/Helper/Core.php

    You may check to see if someone has a custom avatar by checking for their 'avatar_date' or 'gravatar' variables.
    <xen:if is="{$user.avatar_date} OR {$user.gravatar}">

    Of course the opposite applied for checking whether they are using the default avatar:
    <xen:if is="!{$user.avatar_date} AND !{$user.gravatar}">

    However not all templates provide this information in an array named $user. Some templates use $message for example.

    Additionally, please note that some avatar calls can be forced to display as default even though the user has a custom avatar, so you may need to check for that as well (see $forceType parameter/attribute in the Core.php file).
     
    jdeg likes this.
  3. jdeg

    jdeg Active Member

    Thanks so much for the advice! Do you know which templates use the $message variable vs $user?

    To be more specific I'm trying to write some logic around custom user fields. So I'd like to replace default avatars with other ones. I realize that I'll have to find a way to get $user.customFields in other templates as well (right?). Is $message the same with with customFields? ($message.customFields)
     
  4. Syndol

    Syndol Guest

    It was just an example as I have no idea which templates you are interested in. Template message uses $message while template message_user_info uses $user.
    But if all you want to do is replace the default avatar then you are probably better off editing the function _getDefaultAvatarUrl() in XenForo/Template/Helper/Core.php
    Simply check for your custom filed:
    if (isset($user['customFields']['your_custom_field']))

    then change the path to the avatar image accordingly.
     
  5. jdeg

    jdeg Active Member

    Ah okay, thanks again. That may be a better solution than modifying all the templates. Is there a hook I can use for that function?
     
  6. Syndol

    Syndol Guest

    Hooks are for templates and besides it is now better to use a template modification for that.
    As far as I know you cannot extend the _getDefaultAvatarUrl() function as it is a static one. I do believe you will have to actually edit it.
     
  7. jdeg

    jdeg Active Member

    Right. Mixing up my vbulletin terminology. Hrm, editing files when updating is never fun, but if that's the only way...
     
  8. jdeg

    jdeg Active Member

    Actually, that wasn't too hard to code in at all to code in.

    I'm seeing something strange though. The avatar is not updating in thread_list_item. I'm guessing "customFields" isn't available in this template?

    When looking at the output I see this:
    HTML:
    <span class="avatarContainer">
                <a href="index.php?members/admin.1/" class="avatar Av1s" data-avatarhtml="true"><img src="styles/default/xenforo/avatars/avatar_s.png" width="48" height="48" alt="Admin"></a>
                <a href="index.php?members/admin.1/" class="avatar Av1s miniMe" title="You have posted 1 message(s) in this thread" data-avatarhtml="true"><img src="styles/default/xenforo/avatars/avatar_other_s.png" width="48" height="48" alt="Admin"></a>
            </span>
    The small image "avatar_other_s.png" is correct, but the other one should also be this image.
     
    Last edited: Jan 25, 2014
  9. Syndol

    Syndol Guest

    Which route did you take? Template modification or function edit?
    If function edit then there may be situations when the custom fields are not present in the $user array and so you will need to fetch them yourself.
    If you post your code I will try to help.
     
  10. jdeg

    jdeg Active Member

    Function edit. This is what I have:

    PHP:
            if (!isset($user['customFields']['gender']))
            {
                
    $user['customFields']['gender'] = '';
            }
           
            switch (
    $user['customFields']['gender'])
            {
                case 
    'field1':
                case 
    'field2':
                case 
    'field3':
                    
    $gender $user['customFields']['gender'] . '_';
                    break;
                   
                default:
                    
    $gender '';
                    break;
            }
    Seriously, thanks so much for the help!
     
  11. Syndol

    Syndol Guest

    'gender' is already a column in the user table.
    Did you create a custom field called 'gender' as well?
     
  12. jdeg

    jdeg Active Member

    Yes, in this case the custom field is also gender. Everything works in threads and member pages but not member list and thread list
     
  13. Syndol

    Syndol Guest

    Try this:
    Code:
    if (!$imagePath = self::styleProperty('imagePath'))
    {
        $imagePath = 'styles/default';
    }
    
    if (!isset($user['customFields']['gender']))
    {
        $user = self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
        $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
    }
    
    $gender = '';
    $customFields = $user['customFields'];
    if (key_exists('gender', $customFields))
            $gender = strtolower($customFields['gender']) . '_';
    
    return "{$imagePath}/xenforo/avatars/avatar_{$gender}{$size}.png";
     
  14. jdeg

    jdeg Active Member

    Thanks, but I'm seeing the same result with that code. Although I see that is a better solution if the custom field is added to or changed.
     
    Last edited: Jan 25, 2014
  15. Syndol

    Syndol Guest

    The function we are editing is for users who do not have a custom avatar.
    The avatar of the thread starter in the thread list - is that a default one or a custom one?
    It would seem I'm no longer sure what it is you are after.
     
  16. jdeg

    jdeg Active Member

    Yes, in this case the user has a default avatar (male or female was not chosen so it is jus the ? one)

    All templates are default as well. Running XF 1.2.4
     
  17. Syndol

    Syndol Guest

    Well I don't see why it would not work.
    I'm still confused as to why you have a custom field for gender when that is already available.
    Perhaps if you tell me exactly how you created the custom field I could try duplicating it all for testing.
     
  18. Syndol

    Syndol Guest

    If the custom field is not mandatory then perhaps you need to have the default avatar and thus gender as 'other'?
    $gender = 'other_';
    and then change it only if the field is set?!
     
  19. jdeg

    jdeg Active Member

    The site I'm working on has specific gender requirements, but this could be applied to any custom field to change the avatar, I'm sure.

    Just to check, this is how I have things inserted - I didn't miss anything, did I?

    PHP:
        protected static function _getDefaultAvatarUrl(array $user$size)
        {
            if (!isset(
    $user['gender']))
            {
                
    $user['gender'] = '';
            }

            switch (
    $user['gender'])
            {
                case 
    'male':
                case 
    'female':
                    
    $gender $user['gender'] . '_';
                    break;

                default:
                    
    $gender '';
                    break;
            }
           
            if (!
    $imagePath self::styleProperty('imagePath'))
            {
                
    $imagePath 'styles/default';
            }
           
        
    //added
            
    if (!isset($user['customFields']['gender']))
            {
                
    $user self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
                
    $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
            }

            
    $gender '';
            
    $customFields $user['customFields'];
            if (
    key_exists('gender'$customFields))
                    
    $gender strtolower($customFields['gender']) . '_';
        
    //end added

            
    return "{$imagePath}/xenforo/avatars/avatar_{$gender}{$size}.png";
        }
     
  20. jdeg

    jdeg Active Member

    actually in this case it is mandatory and other is just one of the choices.
     

Share This Page