XF 2.0 What user data is required to show an avatar?

abdfahim

Well-known member
If I want to show an avatar with the following code, what data is required inside the $user variable? For information, I have a PHP callback function which returns the $user variable.

Code:
<xf:avatar user="$user" size="xs">
 
Just to add, in XF1, I used to return the following fields, but the same query is not fetching the avatar anymore with XF2
xf_user_field_value.user_id
xf_user.avatar_date
xf_user.username
 
Can you please elaborate? I use the following SQL, what extra fields I should add to my query
SQL:
$db = \XF::db();
$db->fetchAll("SELECT
            xf_user_field_value.user_id,
            xf_user.avatar_date,
            xf_user.username,
            max(.........) as event_date,
            substring_index(............................) as event_name
            FROM xf_user_field_value
            LEFT JOIN xf_user ON xf_user_field_value.user_id = xf_user.user_id
            WHERE xf_user_field_value.field_id IN (..................) AND
            xf_user_field_value.field_value > DATE_SUB(........................)
            GROUP BY xf_user_field_value.user_id
            ORDER BY event_date DESC
            LIMIT 12");
 
Should I just use xf_user.*? I was only returning the required columns thinking it would have some performance benefits.
 
No, that won't work.

Bear in mind we generally don't work with bare arrays anymore, the avatar helper literally requires a full user entity to be passed in. The query you're working with is clearly quite nuanced so I don't necessarily disagree with the approach of performing it as a raw query, though I think you will need to perform an additional query to get the user records.

PHP:
$db = \XF::db();
$values = $db->fetchAllKeyed("
    SELECT xf_user_field_value.user_id,
        max(.........) as event_date,
        substring_index(............................) as event_name
    FROM xf_user_field_value
    LEFT JOIN xf_user ON xf_user_field_value.user_id = xf_user.user_id
    WHERE xf_user_field_value.field_id IN (..................)
        AND xf_user_field_value.field_value > DATE_SUB(........................)
    GROUP BY xf_user_field_value.user_id
    ORDER BY event_date DESC
    LIMIT 12
", "user_id");

$users = \XF::em()->findByIds('XF:User', array_keys($values));

If this is all passed to the template, you could use it similar to this:
HTML:
<xf:foreach loop="$users" value="$user" key="$userId">
    <xf:avatar user="$user" size="xs" />
    <xf:username user="$user" />
    Event date = {$values.{$userId}.event_date}
    Event name = {$values.{$userId}.event_name}
</xf:foreach>
 
Oh, that's a significant change in the XF2 approach for avatars, as my query works fine for XF1. Thanks for explaining in details!

And yes, I wanted to use the "Finder" system for my query, but it is, as you said, a bit of nuanced and I think Finder can't do something like
Code:
substring_index(group_concat(xf_user_field_value.field_id order by xf_user_field_value.field_value desc), ',', 1) as event_name

Still, I'll think about ways to simplify the query and use "Finder" system later sometime, but for the moment I am very happy as the Avatars (and a stable release of XF2) are the only remainings before I convert my 20k-post forum to XF2 :).

I'll try this avatar thing tonight and let you know the result. Once again, thanks a ton for awesome support in your busy schedule!
 
Top Bottom