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

XF 2.0 Pull dynamic avatars externally

LPH

Well-known member
#1
It's possible to pull whether or not dynamic avatars is enabled using $dynamicAvatarEnable = \XF::options()->dynamicAvatarEnable;; however, I tried to dump($user) to see if there was a way to determine a path or a way to get the dynamic avatar. I'm not seeing how to get the dynamic avatar to show up on an external site.

Currently, the following is used in a method:

PHP:
$finder = \XF::finder('XF:User');
$user = $finder->where( 'user_id', $id)->fetchOne();

\XF::dump($user);   // Dump to see what is inside $user

if ($user->avatar_date || $user->gravatar) {
   $xf_avatar_url = \XF::app()->request()->convertToAbsoluteUri( $user->getAvatarUrl( 'm', null, true ) );
}
This pulls the gravatar and the XenForo 2 uploaded avatar ... but not the dynamic avatar. Does anyone have any suggestions?
 

CyberAP

Well-known member
#2
Dynamic avatars are not stored anywhere and generated on the fly, so you'll have to manually call fnAvatar(...) function probably. Bear in mind it always returns a compiled string, so that might not be optimal if you want to get a particular set of data. In case all you want is color data you can call getDefaultAvatarStyling($user). The resulting array is this:
PHP:
[
    'bgColor' => $bgColor,
    'color' => $color,
    'innerContent' => $innerContent
]
 

Chris D

XenForo developer
Staff member
#3
Dynamic avatars are not from a URL, so what you will want to do is forget providing an avatar URL and instead fetch the standard XF HTML code to render it (which will include the avatar container, all the class names and either the image URL or the content which generates the dynamic avatar.

It would look something like this:
PHP:
$finder = \XF::finder('XF:User');
$user = $finder->where( 'user_id', $id)->fetchOne();

$avatarHtml = \XF::app()->templater()->fn('avatar', [$user, 's', true, []]);
Note that the 's' is the size code (so you can use s, m, and l etc.) true makes the link canonical (which you might need to ensure the URLs work externally) and the 4th argument there which I've added as an empty array can be used to add other attributes (it's optional, you might not need it).
 

LPH

Well-known member
#4
Thank you, both.

The HTML returned by XenForo 2 almost matches the WordPress information. WordPress has a get_avatar filter which allows my plugin to return

PHP:
$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
therefore, a return of $avatarHtml works after I adjust the CSS, except in the WordPress toolbar. I'm going to table this for later. There are more important features to complete.